From: http://waryist.javaeye.com/blog/104739 Waryist blog
Alter [ignore] Table tbl_name alter_spec [, alter_spec...] Alter_specification: Add [column] create_definition [first | after column_name] Or add index [index_name] (index_col_name ,...) Or add primary key (index_col_name ,...) Or add unique [index_name] (index_col_name ,...) Or alter [column] col_name {set default literal | drop default} Or change [column] old_col_name create_definition Or modify [column] create_definition Or drop [column] col_name Or drop primary key Or drop index index_name Or RENAME [as] new_tbl_name Or table_options Alter table allows you to modify the structure of an existing table. For example, you can add or delete columns, create or delete indexes, change existing column types, or rename columns or tables. You can also change the table annotations and table types. See 7.7 create table syntax. If you use alter table to modify a column description but describe tbl_name shows that your column is not modified, this may be one of the reasons why MySQL described in the implicit column description in 7.7.1 ignores your modifications. For example, if you try to change a varchar to Char, MySQL will still use varchar if the table package contains other variable-length columns. Alter table works by creating a temporary copy of the original table. The modification takes effect on the copy, and the original table is deleted and a new one is renamed. In this way, all the modifications are automatically redirected to the new table without any failed modifications. When alter table is being executed, the original table can be read by other customers. Updating and writing tables is delayed until the new table is ready. To use alter table, you need the select, insert, delete, update, create, and drop permissions on the table. Ignore is an extension of MySQL to ANSI sql92. If there are duplicate unique keys in the new table, it controls how alter table works. If ignore is not specified, the copy is abandoned and restored to its original state. If ignore is specified, only the first row is used for the unique key, and the rest are deleted. You can issue multiple add, alter, drop, and change clauses in a single alter table statement. This is an extension of ANSI sql92 by MySQL. sql92 allows only one clause in each alter table statement. Change col_name, drop col_name, and drop index are MySQL extensions to ANSI sql92. Modify is an oracle extension to alter table. The optional word column is purely noisy and can be omitted. If you use alter table tbl_name Rename as new_name without any other options, MySQL simply rename the file corresponding to the table tbl_name. There is no need to create a temporary table. The create_definition clause uses the same add and change syntax as create table. Note that the syntax includes the column name, not only the column type. See 7.7 create table syntax. You can use the change old_col_name create_definition clause to rename a column. To do this, specify the old and new column names and the current type of the column. For example, to rename an integer column from A to B, you can do this: Mysql> alter table T1 change a B integer; If you want to change the column type rather than the name, even if they are the same, the change syntax still requires two column names. For example: Mysql> alter table T1 change B bigint not null; However, in mysql3.22.16a, you can also use modify to change the column type rather than rename it: Mysql> alter table T1 modify B bigint not null; If you use change or modify to shorten a column, an index exists in the column section (for example, if you have an index with the first 10 characters of a varchar column ), you cannot make a column shorter than the number of indexed characters. When you use change or modify to change the column type, MySQL tries to convert the data to the new type as much as possible. After mysql3.22 or later, you can use first or add... after col_name to add columns at a specific position in a table row. By default, it is added to the last column. Alter column specifies a new default value for the column or deletes the old default value. If the old default value is deleted and the column can be null, the new default value is null. If the column cannot be null, MySQL assigns a default value. The default value is described in the 7.7 create table syntax. Drop index: delete an index. This is an extension of ANSI sql92 by MySQL. If a column is discarded from a table, the column is also deleted from any index where they are components. If all the columns that make up an index are discarded, the index is also discarded. Drop primary key discards the primary index. If such an index does not exist, it discards the first unique index in the table. (If primary key is not explicitly specified, MySQL marks the first unique key as primary key .) Using the c api function mysql_info (), you can find out how many records are copied, and (when ignore is used) How many records are deleted due to unique key values. The foreign key, check, and references clauses do nothing. Their syntax only provides compatibility, making it easier to port code from other SQL servers and run applications that use references to create tables. For more information, see 5.4mysql. Here is an example showing some alter table usage. We start with a table T1 created as follows: Mysql> Create Table T1 (a integer, B char (10 )); Rename the table from T1 to t2: Mysql> alter table T1 rename T2; To change column A, change column B from integer to tinyint not null (with the same name), change column B from Char (10) To char (20), and rename it at the same time, change from B to C: Mysql> alter table T2 modify a tinyint not null, change B C char (20 ); Add a new timestamp column named D: Mysql> alter table T2 add D timestamp; Add an index on column D and set column A as the primary key: Mysql> alter table T2 add index (d), add primary key (); Delete column C: Mysql> alter table T2 drop column C; 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 have indexed C, because the auto_increment column must be indexed, and we declare C as not null, because the indexed Column cannot be null. When you add an auto_increment column, the column values are automatically entered with sequential numbers. |