Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > SQL Wish List | Report Bug | About Me ]

9.3.4     Making table changes

This is NOT the latest copy of this book; click here for the latest version.

Once you have got your table created, we can make changes to it using ALTER TABLE syntax. Basic usage of alter table is either ALTER TABLE some_table ADD field type , or ALTER TABLE some_table DROP field - the former adds new field some_field of type some_type , and the latter deletes the field some_field . Consider the following code:

CREATE TABLE usertable (ID INT, FirstName CHAR(255), LastName CHAR(255), Age INT);
ALTER TABLE usertable ADD MiddleName CHAR(255);
ALTER TABLE usertable DROP Age;

That will leave usertable containing four fields: ID, FirstName, LastName, and MiddleName. When you delete a column, there is no way to get it back - MySQL has no method for "undo", remember.

There are two key things to note about using ALTER TABLE:

  • As it would be massively inefficient for MySQL simply tack a new field on to the end of a table or equally to delete a field and leave big gaps in a table, the ALTER TABLE process nearly always involves creating a new table with your requested change, copying all the data from the old table to the new, then deleting the old table. During this time, all queries that come in asking to read data will be processed from the old table, and all queries asking to write data will be put on hold until the operation has completed. If you are altering a big table, this operation can take some time, so be sure you are willing to put your writes on hold before you execute this command.

  • MySQL will sometimes overrule your choice of column type if it thinks it knows better, and MySQL won't even tell you it has changed your decision for you - you will need to check your table using "DESC <your table>" to make sure it has changed. More on this later.

The ALTER TABLE command can also be used to rename tables, like this:

ALTER TABLE foo RENAME TO bar;

Note that this use of ALTER TABLE is the only situation where MySQL will not need to create a temporary table.





<< 9.3.3 Creating tables   9.3.5 Deleting tables >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow