MySQL ALTER TABLE – Add Multiple Columns

I recently needed to add multiple columns to an existing table to store summary data calculations and wondered if I could do it in one MySQL ALTER TABLE statement. Turns out you can. And, it’s super simple. Convenient too. Continue reading and learn what I learned…

Image by Gerd Altmann from Pixabay 
The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

MySQL ALTER TABLE

In MySQL, to add a new column to an existing table, the ALTER TABLE syntax would look like this:

ALTER TABLE table_name ADD COLUMN column_name column_definition;

Let’s try adding multiple columns in one command.

I have this arbitrary table with 2 columns:

DESC a_tab;

☕☕☕

If you found value in this post, you can show your appreciation and buy me a coffee. It is my favorite drink!

☕☕☕


We start with ALTER TABLE table_name ADD COLUMN like normal. Then specify however many columns we want, with the required definition for each, enclosed in parentheses.

ALTER TABLE a_tab
ADD COLUMN (
name_col VARCHAR(30) DEFAULT 'not sure',
age_col INTEGER DEFAULT NULL,
another_col TEXT
);

Checking the table definition again, we can see the 3 new columns have been added, all with one ALTER TABLE command:

DESC a_tab;

Viola. So easy, right?


Discover premium articles, in-depth guides, instructional videos, and much more by joining the “MySQL Learning Tier” membership. You have access to exclusive content unpublished anywhere else with this membership. With new content being added regularly, continue learning MySQL at any level.


Thank you for reading this post. Please share it with someone else who would enjoy it as well.


Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.

Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.


Check out this article from the OpenLampTech publication page, Book Recommendation – SQL Antipatterns.


More ways I can help

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.


Support my content with a Tip. Thank you so much!!!


The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.