Add a column to an existing MySQL table

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: MySQL  

MySQL tables are easy to extend with additional columns.


To add a column called email to the contacts table created in Create a basic MySQL table with a datatype of VARCHAR(80), use the following SQL statement:

ALTER TABLE contacts ADD email VARCHAR(60);

This first statement will add the email column to the end of the table. To insert the new column after a specific column, such as name, use this statement:

ALTER TABLE contacts ADD email VARCHAR(60) AFTER name;

If you want the new column to be first, use this statement:

ALTER TABLE contacts ADD email VARCHAR(60) FIRST;

 

79 Comments -


  1. nilesh said on November 18, 2008

    Thanks. it helped me.

  2. Philip said on November 18, 2008

    Wow, that’s kind of stupid that there’s no before.

  3. fubar said on December 12, 2008

    nice

  4. James said on December 14, 2008

    Why do you need a before? If its the first one, you use first, if its the second one, you use after first.

  5. Mike said on January 12, 2009

    Thus why philip will never design anything worth while. Positive thinking is needed to create great things …. :P

  6. Parkash said on January 16, 2009

    Wow what a solution!

  7. KG Old Wolf said on January 16, 2009

    Actually “HEAD FIRST SQL” (book by O’Rielly publications) states there is “BEFORE, AFTER, FIRST, SECOND, THIRD” but I can only get FIRST and AFTER to work using MySQL 5.1

    That’s why I came here for a different opinion.

    I think the book is wrong :(

  8. BaZ said on January 28, 2009

    Try looking at MySQL refman page:
    http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
    The location, if present, must be “FISRT” or “AFTER column”, there is no other option.

    If the book is about MySQL 5.1 its really wrong, but isn t it about ANSI SQL or anything else?

  9. bob said on March 1, 2009

    This is just perfect, I use it all the time … thx!

  10. gb said on March 20, 2009

    Thanks!

  11. ally said on April 16, 2009

    wow, you have no idea what you’re talking ’bout

  12. gnujack said on April 29, 2009

    Thanks, exactly what I was looking for!

  13. mike said on May 2, 2009

    “…with a datatype of VARCHAR(80), use the…” then you say “…ADD email VARCHAR(60)” what i’m getting at here is 80 then 60. small typo.

  14. andy said on May 23, 2009

    @KG – SQL comes in many flavors. PregreSQL and MySQL and MS SQLServer all use slightly different variations on SQL, with varying support for various language constructs. I can only assume they are all derived from some original SQL language specification.

  15. Anonymous said on July 23, 2009

    I want to learn about the space that data take in mysql

  16. English said on August 30, 2009

    Thanks for that info, that really helps me to create my first ALTER ADD COLUMN =)

  17. Anonymous said on September 8, 2009

    good example

  18. Anonymous said on October 26, 2009

    thanks

  19. sasimkumar said on November 16, 2009

    Thanks for the information..

  20. Anonymous said on December 8, 2009

    How do you add a value to the new column?

  21. Anonymous said on December 15, 2009

    First time in this site. It seems helpful

  22. Anonymous said on December 22, 2009

    This is helped me a lot!!!

  23. Anonymous said on December 22, 2009

    This site is helpful i like it!

  24. Anonymous said on December 22, 2009

    Does this site have a Facebook group i would to join it coz it helped me big tym!

  25. Anonymous said on December 27, 2009

    Can we then at least have a “between” if we can’t have a before … ;-) ;-), wow I like more prepositions …

    Quick ‘n easy explanation, qmchenry, thanks!

  26. Anonymous said on January 7, 2010

    Hey

    Any of you guys know how to change existing column with string type to binary format including data?

    Sam
    System Admin
    flip minohd camcorder

  27. dani said on February 3, 2010

    very useful article. Thanks for writing it.

  28. Danny said on February 3, 2010

    Very useful one! Thanks a lot

  29. Anonymous said on March 3, 2010

    how to add two colomns in a existing database table?
    For example i want to add first_name,last_name column after serial_no column. it should be added in a single script

  30. Anonymous said on March 3, 2010

    how to add multiple columns using single query to a existing databae table

  31. test said on March 30, 2010

    not getting

  32. Anonymous said on March 31, 2010

    good, i used and it’s correct

  33. Better Reader said on April 3, 2010

    Because it increases the inherent flexibility that (My)SQL is supposedly famous for, obviously.

  34. 3rlend said on April 30, 2010

    Helped me, but if you have a table with users, many users, how to chose the default value for all the users?
    For instance, I add a column “logged in”, but I want it to be “false” in every row.
    How do I do that?

  35. 3rlend said on April 30, 2010

    I just found it out.
    ALTER TABLE table1 ADD column4 Default ‘jeje’;

  36. Rein said on May 7, 2010

    thanks

  37. Chandru said on May 27, 2010

    please give the suggestion for upload a file into the mysql database

  38. yusuf said on May 27, 2010

    thanks for that recipe, you know what? the web doesn’t stink after all… By the way Philip you don’t need a before because you’ve got a first… Get it “first”.. thanks tech-recipes.com you’re not a life safer but you’re cool.

  39. Raj said on June 2, 2010

    Thanks it good for new person in mysql

  40. beyo said on June 4, 2010

    thanks a lot

  41. Kinathukadavuu said on July 7, 2010

    thanks

  42. Shariqnittt said on July 12, 2010

    Thanks

  43. Dheerajchouhan85 said on July 13, 2010

    thanks bro

  44. Jhanine_0692 said on July 19, 2010

    hauhuhu

  45. Mackensz1 said on July 29, 2010

    Thanks.. IT helps me

  46. Guest said on August 10, 2010

    Great, thank you

  47. Manoj said on August 31, 2010

    thanks alot

  48. Thomas said on September 2, 2010

    hello, thanks but how can I alter multiple tables at once using wildcard, tables ends with _test (001_test, 002_test, 003_test, ..).

    I trie this but doesnt work

    ALTER TABLE *_test ADD image VARCHAR(255)

  49. vik said on September 3, 2010

    thank, it helped me.

  50. chanderpal soni said on September 13, 2010

    its a realy good example for beginers…
    thanks a lot

  51. MMAMail said on September 27, 2010

    nice and quick to the point. thank

  52. silent_sinner said on October 3, 2010

    thank for that info..

  53. vishal said on October 7, 2010

    thanks for help

  54. Fernando Silva said on October 20, 2010

    Danke Schön!

  55. erald mariano said on October 23, 2010

    thanks! it helped!

  56. Vtmathi@googlemail.com said on November 4, 2010

    Fantastic explanation

  57. Marcus Longwell said on November 18, 2010

    Except there is no need for it anyways, obviously.

  58. Viji_jyotika said on December 15, 2010

    then go

  59. Binnu said on January 12, 2011

    Mei k aaj ko hu…Binnu..

  60. Manu_ash0147 said on January 12, 2011

    thanks

  61. Mcheche Mugune said on January 20, 2011

    How to add a column in the existing database table?

  62. Prasath said on January 27, 2011

    THANKS

  63. Aliraza4167 said on February 3, 2011

    Thanks alot. it was of great help

  64. James Saunderson said on February 8, 2011

    ALTER TABLE `mydb`.`mytable` ADD COLUMN `col2` TINYINT(1) DEFAULT ‘0′ AFTER `col1`

  65. James Saunderson said on February 8, 2011

    ALTER TABLE `mydb`.`mytable` ADD COLUMN `col2` VARCHAR(50) AFTER `col1`, ADD COLUMN `col3` TINYINT(1) DEFAULT ‘0′ AFTER `col2`;

  66. Randy said on February 15, 2011

    It would be helpful to show an MYSQL example along with the SQL example

  67. Spam Banjo said on February 18, 2011

    Um, yes there is.

  68. Karan Sachdeva86 said on March 9, 2011

    thank you

  69. Anonymous said on March 12, 2011

    Thank you for this.

  70. Lara Web Works said on April 7, 2011

    Thank you, son simple yet it looks complicated in the manual!

  71. supriya said on May 4, 2011

    thank you.. it helped me

  72. Pamidipadudravid said on May 21, 2011

    thanks  alot

  73. Sdfds said on May 23, 2011

    thanks, good info.

  74. liftari said on May 27, 2011

     I agree with you,  I’m not an expert in mysql, but I’m working on a database and needed to add a new column and “before” googling I tried with it xD

  75. Rizwan khan said on July 5, 2011

    so easy thnks.

  76. LAKS said on October 14, 2011

    its very simple… thanks….

  77. Yogesh said on October 17, 2011

    Thanks for help

  78. Shairyar said on December 26, 2011

    great thanks

  79. vickky said on December 28, 2011

    thank you very much

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -