title: ALTER TABLE summary: TiDB 数据库中 ALTER TABLE 的使用概况。

ALTER TABLE

ALTER TABLE 语句用于对已有表进行修改,以符合新表结构。ALTER TABLE 语句可用于:

语法图

  1. AlterTableStmt ::=
  2. 'ALTER' IgnoreOptional 'TABLE' TableName ( AlterTableSpecListOpt AlterTablePartitionOpt | 'ANALYZE' 'PARTITION' PartitionNameList ( 'INDEX' IndexNameList )? AnalyzeOptionListOpt )
  3. TableName ::=
  4. Identifier ('.' Identifier)?
  5. AlterTableSpec ::=
  6. TableOptionList
  7. | 'SET' 'TIFLASH' 'REPLICA' LengthNum LocationLabelList
  8. | 'CONVERT' 'TO' CharsetKw ( CharsetName | 'DEFAULT' ) OptCollate
  9. | 'ADD' ( ColumnKeywordOpt IfNotExists ( ColumnDef ColumnPosition | '(' TableElementList ')' ) | Constraint | 'PARTITION' IfNotExists NoWriteToBinLogAliasOpt ( PartitionDefinitionListOpt | 'PARTITIONS' NUM ) )
  10. | ( ( 'CHECK' | 'TRUNCATE' ) 'PARTITION' | ( 'OPTIMIZE' | 'REPAIR' | 'REBUILD' ) 'PARTITION' NoWriteToBinLogAliasOpt ) AllOrPartitionNameList
  11. | 'COALESCE' 'PARTITION' NoWriteToBinLogAliasOpt NUM
  12. | 'DROP' ( ColumnKeywordOpt IfExists ColumnName RestrictOrCascadeOpt | 'PRIMARY' 'KEY' | 'PARTITION' IfExists PartitionNameList | ( KeyOrIndex IfExists | 'CHECK' ) Identifier | 'FOREIGN' 'KEY' IfExists Symbol )
  13. | 'EXCHANGE' 'PARTITION' Identifier 'WITH' 'TABLE' TableName WithValidationOpt
  14. | ( 'IMPORT' | 'DISCARD' ) ( 'PARTITION' AllOrPartitionNameList )? 'TABLESPACE'
  15. | 'REORGANIZE' 'PARTITION' NoWriteToBinLogAliasOpt ReorganizePartitionRuleOpt
  16. | 'ORDER' 'BY' AlterOrderItem ( ',' AlterOrderItem )*
  17. | ( 'DISABLE' | 'ENABLE' ) 'KEYS'
  18. | ( 'MODIFY' ColumnKeywordOpt IfExists | 'CHANGE' ColumnKeywordOpt IfExists ColumnName ) ColumnDef ColumnPosition
  19. | 'ALTER' ( ColumnKeywordOpt ColumnName ( 'SET' 'DEFAULT' ( SignedLiteral | '(' Expression ')' ) | 'DROP' 'DEFAULT' ) | 'CHECK' Identifier EnforcedOrNot | 'INDEX' Identifier IndexInvisible )
  20. | 'RENAME' ( ( 'COLUMN' | KeyOrIndex ) Identifier 'TO' Identifier | ( 'TO' | '='? | 'AS' ) TableName )
  21. | LockClause
  22. | AlgorithmClause
  23. | 'FORCE'
  24. | ( 'WITH' | 'WITHOUT' ) 'VALIDATION'
  25. | 'SECONDARY_LOAD'
  26. | 'SECONDARY_UNLOAD'

示例

创建一张表,并插入初始数据:

  1. CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, c1 INT NOT NULL);
  2. INSERT INTO t1 (c1) VALUES (1),(2),(3),(4),(5);
  1. Query OK, 0 rows affected (0.11 sec)
  2. Query OK, 5 rows affected (0.03 sec)
  3. Records: 5 Duplicates: 0 Warnings: 0

执行以下查询需要扫描全表,因为 c1 列未被索引:

  1. EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
  1. +-------------------------+----------+-----------+---------------+--------------------------------+
  2. | id | estRows | task | access object | operator info |
  3. +-------------------------+----------+-----------+---------------+--------------------------------+
  4. | TableReader_7 | 10.00 | root | | data:Selection_6 |
  5. | └─Selection_6 | 10.00 | cop[tikv] | | eq(test.t1.c1, 3) |
  6. | └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
  7. +-------------------------+----------+-----------+---------------+--------------------------------+
  8. 3 rows in set (0.00 sec)

你可以使用 ALTER TABLE .. ADD INDEX 语句在 t1 表上添加索引。添加后,EXPLAIN 的分析结果显示 SELECT * FROM t1 WHERE c1 = 3; 查询已使用效率更高的索引范围扫描:

  1. ALTER TABLE t1 ADD INDEX (c1);
  2. EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
  1. Query OK, 0 rows affected (0.30 sec)
  2. +------------------------+---------+-----------+------------------------+---------------------------------------------+
  3. | id | estRows | task | access object | operator info |
  4. +------------------------+---------+-----------+------------------------+---------------------------------------------+
  5. | IndexReader_6 | 10.00 | root | | index:IndexRangeScan_5 |
  6. | └─IndexRangeScan_5 | 10.00 | cop[tikv] | table:t1, index:c1(c1) | range:[3,3], keep order:false, stats:pseudo |
  7. +------------------------+---------+-----------+------------------------+---------------------------------------------+
  8. 2 rows in set (0.00 sec)

TiDB 允许用户为 DDL 操作指定使用某一种 ALTER 算法。这仅为一种指定,并不改变实际的用于更改表的算法。如果你只想在群集的高峰时段允许即时 DDL 更改,则 ALTER 算法会很有用。示例如下:

  1. ALTER TABLE t1 DROP INDEX c1, ALGORITHM=INSTANT;
  1. Query OK, 0 rows affected (0.24 sec)

如果某一 DDL 操作要求使用 INPLACE 算法,而用户指定 ALGORITHM=INSTANT,会导致报错:

  1. ALTER TABLE t1 ADD INDEX (c1), ALGORITHM=INSTANT;
  1. ERROR 1846 (0A000): ALGORITHM=INSTANT is not supported. Reason: Cannot alter table by INSTANT. Try ALGORITHM=INPLACE.

但如果为 INPLACE 操作指定 ALGORITHM=COPY,会产生警告而非错误,这是因为 TiDB 将该指定解读为该算法或更好的算法。由于 TiDB 使用的算法可能不同于 MySQL,所以这一行为可用于 MySQL 兼容性。

  1. ALTER TABLE t1 ADD INDEX (c1), ALGORITHM=COPY;
  2. SHOW WARNINGS;
  1. Query OK, 0 rows affected, 1 warning (0.25 sec)
  2. +-------+------+---------------------------------------------------------------------------------------------+
  3. | Level | Code | Message |
  4. +-------+------+---------------------------------------------------------------------------------------------+
  5. | Error | 1846 | ALGORITHM=COPY is not supported. Reason: Cannot alter table by COPY. Try ALGORITHM=INPLACE. |
  6. +-------+------+---------------------------------------------------------------------------------------------+
  7. 1 row in set (0.00 sec)

MySQL 兼容性

TiDB 中的 ALTER TABLE 语法主要存在以下限制:

  • 单条 ALTER TABLE 语句不能完成多项操作。
  • 当前不支持有损更改,例如从 BIGINT 类型更改为 INT 类型。
  • 不支持空间数据类型。

其它限制可参考:TiDB 中 DDL 语句与 MySQL 的兼容性情况

另请参阅