Name

ALTER/MODIFY

Synopsis

ALTER [IGNORE] TABLE table ADD [COLUMN] create_clause
ALTER [IGNORE] TABLE table ADD INDEX [name] (column, . . .)
ALTER [IGNORE] TABLE table ADD UNIQUE [name] (column, . . .)
ALTER [IGNORE] TABLE table ALTER [COLUMN] column SET DEFAULT value
ALTER [IGNORE] TABLE table ALTER [COLUMN] column DROP DEFAULT
ALTER [IGNORE] TABLE table CHANGE [COLUMN] columncreate_clause
ALTER [IGNORE] TABLE table DROP [COLUMN] column
ALTER [IGNORE] TABLE table DROP FOREIGN KEY key
ALTER [IGNORE] TABLE table DROP INDEX key
ALTER [IGNORE] TABLE table DROP PRIMARY KEY
ALTER [IGNORE] TABLE table MODIFY [COLUMN] create_clause
ALTER [IGNORE] TABLE> table RENAME [AS] new_name

The ALTER statement covers a wide range of actions that modify the structure of a table. This statement is used to add, change, or remove columns from an existing table as well as to remove indexes. Multiple ALTER statements may be combined into one using commas as in the following example:

ALTER TABLE mytable DROP myoldcolumn, ADD mynewcolumn INT

To perform modifications on the table, MySQL creates a copy of the table and changes it, meanwhile queuing all table altering queries. When the change is done, the old table is removed and the new table put it its place. At this point the queued queries are performed. As a safety precaution, if any of the queued queries create duplicate keys that should be unique, the ALTER statement is rolled back and cancelled. If the IGNORE keyword is present in the statement, duplicate ...

Get MySQL and mSQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.