CHANGE COLUMN

ALTER TABLE.. CHANGE COLUMN 语句用于在已有表上更改列,包括对列进行重命名,和将数据改为兼容类型。

语法图

AlterTableStmt:

AlterTableStmt

AlterTableSpec:

AlterTableSpec

ColumnKeywordOpt:

ColumnKeywordOpt

ColumnName:

ColumnName

ColumnDef:

ColumnDef

ColumnPosition:

ColumnPosition

示例

  1. CREATE TABLE t1 (id int not null primary key AUTO_INCREMENT, col1 INT);
  1. Query OK, 0 rows affected (0.11 sec)
  1. INSERT INTO t1 (col1) VALUES (1),(2),(3),(4),(5);
  1. Query OK, 5 rows affected (0.02 sec)
  2. Records: 5 Duplicates: 0 Warnings: 0
  1. ALTER TABLE t1 CHANGE col1 col2 INT;
  1. Query OK, 0 rows affected (0.09 sec)
  1. ALTER TABLE t1 CHANGE col2 col3 BIGINT, ALGORITHM=INSTANT;
  1. Query OK, 0 rows affected (0.08 sec)
  1. ALTER TABLE t1 CHANGE col3 col3 INT;
  1. ERROR 1105 (HY000): unsupported modify column length 11 is less than origin 20
  1. ALTER TABLE t1 CHANGE col3 col3 BLOB;
  1. ERROR 1105 (HY000): unsupported modify column type 252 not match origin 8
  1. ALTER TABLE t1 CHANGE col3 col4 BIGINT, CHANGE id id2 INT NOT NULL;
  1. ERROR 1105 (HY000): can't run multi schema change

MySQL 兼容性

  • 目前尚不支持在单个 ALTER TABLE 语句中进行多个更改。
  • 仅支持特定的数据类型更改。例如,支持将 INTEGER 更改为 BIGINT,但不支持将 BIGINT 更改为 INTEGER。不支持从整数更改为字符串格式或 BLOB 类型。

另请参阅