Search code examples
mysqlalter

Trying to better understand MySQL ALTER syntax


I'm learning MySQL by reading the book "Head First SQL" but I've hit a brick wall. I can't seem to wrap my head around the code below. Specifically, I have a couple questions.

  • When I try running the entire code as it is on the console (MySQL on Wamp) I get multiple errors. But when I separate each and every ALTER command (e.g. ALTER table_name RENAME TO new_table_name;) most of the code goes through the console without any problems. Why is that? According to the book, the entire code should work.

  • One of the errors that I get when I copy/paste the code separately is about the ORDERing, such as "FIRST", "SECOND", "AFTER", etc. Why why could that be happening?

  • Lastly, why do some ALTER commands, such as RENAME and ADD COLUMN need the ALTER TABLE table_name, but others, such as CHANGE or MODIFY COLUMN don't? The book doesn't do a good job at explaining anything on this, so I'm curious as to why I'm allowed to do it with some but not all the code.

Could the errors that I'm getting be the result of outdated code? The book was published in 2007, so I'm not sure if the code is outdated or not.

All and any advice is greatly appreciated. Thank you for your time!

CODE: ALTER TABLE table_name RENAME TO new_table_name,

ALTER TABLE new_table_name
ADD COLUMN column1 INT NOT NULL AUTO_INCREMENT FIRST, 

ADD PRIMARY KEY(column1), 

ALTER TABLE new_table_name
ADD COLUMN column2 VARCHAR(16) SECOND, 

CHANGE COLUMN column3 new_column3 VARCHAR(20), 
MODIFY COLUMN column4 AFTER column3, 

MODIFY COLUMN column5 SIXTH, 

CHANGE COLUMN column6 DECIMAL(7,2);

Solution

  • The example code that you're working from is broken. The FIRST keyword for ADD COLUMN, CHANGE COLUMN, and MODIFY COLUMN is a special case; there is no similar SECOND keyword, for instance. Columns beyond the first must instead be described as belonging AFTER another_column.

    Additionally, a second ALTER TABLE has been slipped into the middle of this statement for some reason. There is no such inconsistency in the real syntax; this is simply an error in your book.

    Finally, the MODIFY COLUMN alteration must include the new type of the column. (If the goal of the ALTER is simply to move the column to another position in the table, the current type of the column can be specified.)

    The official documentation for the ALTER TABLE command is worth reading. It's long, but detailed. (And, better yet, it's correct.)

    Here is a corrected version of the query, with comments. Note that I haven't tested this query on a real table, so I'm not certain that it's completely correct. (In particular, I've left the types of column4 and column5 as SOME_TYPE because I do not know what their type is supposed to be.)

    ALTER TABLE new_table_name
        ADD COLUMN column1 INT NOT NULL AUTO_INCREMENT FIRST, 
        ADD PRIMARY KEY(column1), 
        ADD COLUMN column2 VARCHAR(16) AFTER column1,  -- no extra ALTER; not SECOND
        CHANGE COLUMN column3 new_column3 VARCHAR(20),
        MODIFY COLUMN column4 SOME_TYPE AFTER new_column3, -- need type, and must
                                                           -- use new column name
        MODIFY COLUMN column5 SOME_TYPE AFTER column4, -- not SIXTH
        CHANGE COLUMN column6 DECIMAL(7,2);