MODIFY COLUMN

ALTER TABLE .. MODIFY COLUMN 语句用于修改已有表上的列,包括列的数据类型和属性。若要同时重命名,可改用 CHANGE COLUMN 语句。

语法图

AlterTableStmt:

AlterTableStmt

AlterTableSpec:

AlterTableSpec

ColumnKeywordOpt:

ColumnKeywordOpt

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 MODIFY col1 BIGINT;
  1. Query OK, 0 rows affected (0.09 sec)
  1. SHOW CREATE TABLE t1;
  1. *************************** 1. row ***************************
  2. Table: t1
  3. Create Table: CREATE TABLE `t1` (
  4. `id` int(11) NOT NULL AUTO_INCREMENT,
  5. `col1` bigint(20) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=30001
  8. 1 row in set (0.00 sec)
  1. ALTER TABLE t1 MODIFY col1 INT;
  1. ERROR 1105 (HY000): unsupported modify column length 11 is less than origin 20
  1. ALTER TABLE t1 MODIFY col1 BLOB;
  1. ERROR 1105 (HY000): unsupported modify column type 252 not match origin 8
  1. ALTER TABLE t1 MODIFY col1 BIGINT, MODIFY id BIGINT NOT NULL;
  1. ERROR 1105 (HY000): can't run multi schema change

MySQL 兼容性

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

另请参阅