SQL reference (MySQL and MariaDB dialects)

ALTER TABLE statement

The changes ALTER TABLE accepts, how the checks and the row rewrite work, and the changes that are refused.

ALTER TABLE changes the definition of a table. Several changes may be written in one statement, separated by commas. The changes are checked in order against a copy of the definition; a mistake changes nothing. If rows must change (a new layout, a new generated column), every row is rewritten, all new documents are worked out in memory before the first write, and a value that does not fit refuses the whole statement.

Syntax#

SQL
ALTER [IGNORE] TABLE [IF EXISTS] [db_name.]tbl_name
    alter_specification [, alter_specification] ...

alter_specification:
    ADD [COLUMN] [IF NOT EXISTS] col_name column_definition [FIRST | AFTER col_name]
  | ADD [COLUMN] [IF NOT EXISTS] (col_name column_definition, ...)
  | ADD PRIMARY KEY (key_part, ...)
  | ADD [CONSTRAINT [symbol]] [UNIQUE] {INDEX | KEY} [IF NOT EXISTS] [index_name] (key_part, ...) [index_option] ...
  | ADD [CONSTRAINT [symbol]] UNIQUE (key_part, ...)
  | ADD [CONSTRAINT [symbol]] FOREIGN KEY [IF NOT EXISTS] [index_name] (col_name, ...) reference_definition
  | ADD [CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]
  | DROP [COLUMN] [IF EXISTS] col_name
  | DROP PRIMARY KEY
  | DROP {INDEX | KEY} [IF EXISTS] index_name
  | DROP {FOREIGN KEY | CONSTRAINT} [IF EXISTS] symbol
  | DROP CHECK [IF EXISTS] symbol
  | MODIFY [COLUMN] [IF EXISTS] col_name column_definition [FIRST | AFTER col_name]
  | CHANGE [COLUMN] [IF EXISTS] old_col_name new_col_name column_definition [FIRST | AFTER col_name]
  | ALTER [COLUMN] col_name { SET DEFAULT expr | DROP DEFAULT | SET VISIBLE | SET INVISIBLE }
  | ALTER INDEX index_name { VISIBLE | INVISIBLE }
  | RENAME COLUMN old_col_name TO new_col_name
  | RENAME {INDEX | KEY} old_index_name TO new_index_name
  | RENAME [TO | AS] new_tbl_name
  | AUTO_INCREMENT [=] value
  | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  | { DISABLE | ENABLE } KEYS
  | FORCE
  | table_option

column_definition, key_part, index_option and reference_definition are as in CREATE TABLE. A table_option is one of ENGINE, ALGORITHM, LOCK, ROW_FORMAT, COMMENT, COLLATE, CHARSET, DEFAULT CHARSET, DEFAULT COLLATE, CHARACTER SET, CHECKSUM, DELAY_KEY_WRITE, KEY_BLOCK_SIZE, MAX_ROWS, MIN_ROWS, PACK_KEYS, STATS_PERSISTENT, STATS_AUTO_RECALC, AVG_ROW_LENGTH, each with an optional = and one value.

Arguments#

NameMeaningDefault
IF EXISTS after TABLEDo nothing, with a note, when the table does not exist (MariaDB form).off
IF [NOT] EXISTS after the verb or the kind of objectThe change is done or skipped according to what the table has, with the note MariaDB gives (MariaDB form).off
FIRST, AFTER colPosition of an added or modified column.last / unchanged
ENGINE, ALGORITHM, LOCK, ROW_FORMAT, CONVERT TO, FORCE, DISABLE KEYS, ENABLE KEYS and the other table optionsRead and ignored: the indexes are always maintained and there is one engine. mysqldump wraps loads in DISABLE KEYS / ENABLE KEYS.none

Remarks#

  • Errors are MySQL's: 1060 duplicate column, 1054 unknown column, 1091 cannot drop, 1061 duplicate key name, 1072, 1090, 1068, 1075, 1050. Value errors while rewriting rows are 1265, 1366 and 1062 and name the first row in primary-key order.
  • A changed primary key re-keys every row. It is refused (1235) when the table has unique keys, because their claims name the row key.
  • Privileges: ALTER on the table; INDEX for index changes; for RENAME TO: DROP on the old name and CREATE, INSERT on the new name.
  • ALTER TABLE ends an open transaction (implicit commit).
  • Adding a CHECK checks the rows already in the table. Adding a generated column fills the old rows.
  • DROP INDEX name ON tbl and CREATE INDEX are translated into ALTER TABLE (see CREATE INDEX).
  • A column used by a check, a generated column or a functional index can be dropped or renamed without a check (the dependency is not tracked).

Not supported yet#

Refused with error 1235:

  • Adding an AUTO_INCREMENT or PRIMARY KEY column through ADD COLUMN.
  • Dropping a column of a unique key; changing the type of a column of a unique key; renaming a unique key.
  • Changing AUTO_INCREMENT or PRIMARY KEY of an existing column through MODIFY.
  • Moving a table to another database with RENAME.
  • Adding a column whose default is an expression to a table that has rows; adding a NOT NULL date, time or JSON column without a default to a table that has rows.
  • ADD FULLTEXT (accepted, not kept), ADD SPATIAL (kept as a definition), ADD PARTITION and all partition changes.
  • ALTER CHECK name [NOT] ENFORCED and any other change not in the grammar above (the message says "this ALTER TABLE change").
  • ALTER DATABASE, ALTER PROCEDURE, ALTER FUNCTION, ALTER EVENT (see statements not supported yet).

MODIFY col ... PRIMARY KEY and MODIFY col ... AUTO_INCREMENT [PRIMARY KEY] work (the column becomes the primary key; the counter continues after the largest value; error 1068 if a key exists, 1062 for duplicates). The AUTO_INCREMENT column must be the first column of the primary key (error 1075).

Examples#

Example (MariaDB forms and invisible columns):

SQL
ALTER TABLE t ADD COLUMN IF NOT EXISTS y INT;
ALTER TABLE t ADD y2 INT, ADD COLUMN IF NOT EXISTS y INT, ADD COLUMN IF NOT EXISTS y3 INT;
ALTER TABLE t DROP COLUMN IF EXISTS nope;
ALTER TABLE t ADD INDEX IF NOT EXISTS ix (y);
ALTER TABLE IF EXISTS nosuch ADD COLUMN z INT;   -- a note, no error

ALTER TABLE t ALTER COLUMN a SET INVISIBLE;
ALTER TABLE t ALTER INDEX ia VISIBLE;
ALTER TABLE t ADD COLUMN extra INT INVISIBLE;

See also#