Search the MySQL manual:

6.5.4 ALTER TABLE Syntax

ALTER [IGNORE] TABLE tbl_name alter_specification [, alter_specification ...]

alter_specification:
    ADD [COLUMN] create_definition [FIRST | AFTER column_name ]
  | ADD [COLUMN] (create_definition, create_definition,...)
  | ADD INDEX [index_name] (index_col_name,...)
  | ADD PRIMARY KEY (index_col_name,...)
  | ADD UNIQUE [index_name] (index_col_name,...)
  | ADD FULLTEXT [index_name] (index_col_name,...)
  | ADD [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
           [reference_definition]
  | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
  | CHANGE [COLUMN] old_col_name create_definition
           [FIRST | AFTER column_name]
  | MODIFY [COLUMN] create_definition [FIRST | AFTER column_name]
  | DROP [COLUMN] col_name
  | DROP PRIMARY KEY
  | DROP INDEX index_name
  | DISABLE KEYS
  | ENABLE KEYS
  | RENAME [TO] new_tbl_name
  | ORDER BY col
  | table_options

ALTER TABLE allows you to change the structure of an existing table. For example, you can add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself. You can also change the comment for the table and type of the table. See section 6.5.3 CREATE TABLE Syntax.

If you use ALTER TABLE to change a column specification but DESCRIBE tbl_name indicates that your column was not changed, it is possible that MySQL ignored your modification for one of the reasons described in section 6.5.3.1 Silent Column Specification Changes. For example, if you try to change a VARCHAR column to CHAR, MySQL will still use VARCHAR if the table contains other variable-length columns.

ALTER TABLE works by making a temporary copy of the original table. The alteration is performed on the copy, then the original table is deleted and the new one is renamed. This is done in such a way that all updates are automatically redirected to the new table without any failed updates. While ALTER TABLE is executing, the original table is readable by other clients. Updates and writes to the table are stalled until the new table is ready.

Note that if you use any other option to ALTER TABLE than RENAME, MySQL will always create a temporary table, even if the data wouldn't strictly need to be copied (like when you change the name of a column). We plan to fix this in the future, but as one doesn't normally do ALTER TABLE that often this isn't that high on our TODO. For MyISAM tables, you can speed up the index recreation part (which is the slowest part of the recreation process) by setting the myisam_sort_buffer_size variable to a high value.

Here is an example that shows some of the uses of ALTER TABLE. We begin with a table t1 that is created as shown here:

mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));

To rename the table from t1 to t2:

mysql> ALTER TABLE t1 RENAME t2;

To change column a from INTEGER to TINYINT NOT NULL (leaving the name the same), and to change column b from CHAR(10) to CHAR(20) as well as renaming it from b to c:

mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);

To add a new TIMESTAMP column named d:

mysql> ALTER TABLE t2 ADD d TIMESTAMP;

To add an index on column d, and make column a the primary key:

mysql> ALTER TABLE t2 ADD INDEX (d), ADD PRIMARY KEY (a);

To remove column c:

mysql> ALTER TABLE t2 DROP COLUMN c;

To add a new AUTO_INCREMENT integer column named c:

mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
           ADD INDEX (c);

Note that we indexed c, because AUTO_INCREMENT columns must be indexed, and also that we declare c as NOT NULL, because indexed columns cannot be NULL.

When you add an AUTO_INCREMENT column, column values are filled in with sequence numbers for you automatically. You can set the first sequence number by executing SET INSERT_ID=# before ALTER TABLE or using the AUTO_INCREMENT = # table option. See section 5.5.6 SET Syntax.

With MyISAM tables, if you don't change the AUTO_INCREMENT column, the sequence number will not be affected. If you drop an AUTO_INCREMENT column and then add another AUTO_INCREMENT column, the numbers will start from 1 again.

See section A.7.1 Problems with ALTER TABLE..

User Comments

Posted by [name withheld] on Friday May 17 2002, @6:24am[Delete] [Edit]

Suggestion: I've found that you need to refresh
(meaning quit and restart) the MySQLManager
v1.0.2 in order to see ALTER changes.

Posted by Marian Vasile on Friday May 17 2002, @6:24am[Delete] [Edit]

I've found that on Windows somtimes Alter doesn't
work like it should... it gives errors...

I made it work, restarting the MySQL.
(Everything works fine with selects, uptes and
inserts... just this alter gives errors sometimes)

Posted by viorel on Friday May 17 2002, @6:24am[Delete] [Edit]

I made an ALTER TABLE __ MODIFY a column that
was part
of an INDEX and after the command was completed
my server daemon was stuck on 99% CPU used

Posted by Tom S on Wednesday December 18 2002, @5:27pm[Delete] [Edit]

IF you want to change a SET or ENUM column you may
not want to use the ALTER TABLE ... MODIFY
syntax.
It tries to keep the actual string values and not
the integer representation of the values, even
though they are stored as integers.
For example, if you just want to make a change in
spelling of the values in your enum column or your
set column, consider doing it like this:
ALTER TABLE table ADD new_column ...;
UPDATE table SET new_column = old_column + 0;
ALTER TABLE table DROP old_column;

Posted by John Simpson on Thursday September 12 2002, @8:10pm[Delete] [Edit]

The DISABLE KEYS and ENABLE KEYS statements
require the user to have the INDEX permission in
addition to the stated ALTER, INSERT, and CREATE.

Posted by Michael Mensik on Thursday March 27 2003, @6:03am[Delete] [Edit]

Oh, I've forgotten: Be Aware at the foreign Keys! The foreign keys can not be deleted by now. I hope MySQL developers will continue in development of that to. You can only completely drop the table where your foreign key is set, afterwards create the table again (with or without the foreign keys) and then (if not in CREATE TABLE statement) recreate your new foreign keys with the ALTER TABLE statement.

Posted by Akash Kava on Monday May 19 2003, @2:00am[Delete] [Edit]

ALTER TABLE on Windows Version of MySQL many times returns problem while the table might be open with some of client connections. But same on Linux Version runs properly. On Windows after receiving error like unable to rename or open TABLE.* file, MySQL has to be restarted, after restarting MySQL everything works well.

I received such problems with 3.23 final version frequently but on Linux it never gave such problem, I mostly used phpMyAdmin.

Hope the future versions will improve. Rest mySQL is great! pretty fast and perfect, after having mysql control center, its easy to work. I used to avoid mysql due to no good GUI for it !!

- Akash Kava
NeuroSpeech

Add your own comment.