DROP COLUMN

This statement drops a column from a specified table. DROP COLUMN is online in TiDB, which means that it does not block read or write operations.

Synopsis

AlterTableStmt

DROP COLUMN - 图1

DropColumnSpec

DROP COLUMN - 图2

ColumnName

DROP COLUMN - 图3

  1. AlterTableStmt
  2. ::= 'ALTER' 'IGNORE'? 'TABLE' TableName DropColumnSpec ( ',' DropColumnSpec )*
  3. DropColumnSpec
  4. ::= 'DROP' 'COLUMN'? 'IF EXISTS'? ColumnName ( 'RESTRICT' | 'CASCADE' )?
  5. ColumnName
  6. ::= Identifier ( '.' Identifier ( '.' Identifier )? )?

Examples

  1. mysql> CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 INT NOT NULL, col2 INT NOT NULL);
  2. Query OK, 0 rows affected (0.12 sec)
  3. mysql> INSERT INTO t1 (col1,col2) VALUES (1,1),(2,2),(3,3),(4,4),(5,5);
  4. Query OK, 5 rows affected (0.02 sec)
  5. Records: 5 Duplicates: 0 Warnings: 0
  6. mysql> SELECT * FROM t1;
  7. +----+------+------+
  8. | id | col1 | col2 |
  9. +----+------+------+
  10. | 1 | 1 | 1 |
  11. | 2 | 2 | 2 |
  12. | 3 | 3 | 3 |
  13. | 4 | 4 | 4 |
  14. | 5 | 5 | 5 |
  15. +----+------+------+
  16. 5 rows in set (0.01 sec)
  17. mysql> ALTER TABLE t1 DROP COLUMN col1, DROP COLUMN col2;
  18. ERROR 1105 (HY000): can't run multi schema change
  19. mysql> SELECT * FROM t1;
  20. +----+------+------+
  21. | id | col1 | col2 |
  22. +----+------+------+
  23. | 1 | 1 | 1 |
  24. | 2 | 2 | 2 |
  25. | 3 | 3 | 3 |
  26. | 4 | 4 | 4 |
  27. | 5 | 5 | 5 |
  28. +----+------+------+
  29. 5 rows in set (0.00 sec)
  30. mysql> ALTER TABLE t1 DROP COLUMN col1;
  31. Query OK, 0 rows affected (0.27 sec)
  32. mysql> SELECT * FROM t1;
  33. +----+------+
  34. | id | col2 |
  35. +----+------+
  36. | 1 | 1 |
  37. | 2 | 2 |
  38. | 3 | 3 |
  39. | 4 | 4 |
  40. | 5 | 5 |
  41. +----+------+
  42. 5 rows in set (0.00 sec)

MySQL compatibility

  • Dropping primary key columns or columns covered by the composite index is not supported.

See also