该语句用于修改已存在的表的结构,比如:修改表及表属性、新增列、修改列及属性、删除列等。
格式
ALTER TABLE tblname
alter_specification [, alter_specification]...
alter_specification:
ADD [COLUMN] colname column_definition
| ADD [COLUMN] (colname column_definition,...)
| ADD {INDEX | KEY} [indexname] (index_col_name,...) [index_tpye] [index_options]
| ADD UNIQUE [INDEX | KEY] [indexname] (index_col_name,...) [index_type] [index_options]
| ALTER [COLUMN] colname {SET DEFAULT literal | DROP DEFAULT}
| CHANGE [COLUMN] oldcolname newcolname column_definition
| MODIFY [COLUMN] colname column_definition
| DROP [COLUMN] colname
| DROP {INDEX | KEY} indexname
| RENAME [TO | AS] newtblname
| table_options
| partition_options
| DROP TABLEGROUP
| AUTO_INCREMENT [=] num
column_definition:
data_type [NOT NULL | NULL] [DEFAULT defaultvalue]
[AUTO_INCREMENT] [UNIQUE [KEY]] [[PRIMARY] KEY]
[COMMENT ‘string’]
table_options:
[SET] table_option [table_option]...
table_option:
COMMENT [=] ’string’
| COMPRESSION [=] {NONE | LZ4_1.0 | ZSTD_1.0 | SNAPPY_1.0 | ZLIB_1.0}
| EXPIRE_INFO [=] expr
| REPLICA_NUM [=] num
| TABLE_ID [=] id
| BLOCK_SIZE [=] size
| USE_BLOOM_FILTER [=] {True| False}
| STEP_MERGE_NUM [=] num
| TABLEGROUP [=] tablegroupname
| PRIMARY_ZONE [=] zonelist
| AUTO_INCREMENT [=] num
| PCTFREE [=] integer
| {READ ONLY | READ WRITE}
| LOCALITY [=] locality
partition_options:
PARTITION BY
HASH(expr)
| KEY(column_list)
[PARTITIONS num]
[partition_definition ...]
partition_definition:
COMMENT [=] ‘commenttext’
- 增加列
ALTER TABLE tblname
ADD [COLUMN] col_name column_definition;
- column_definition: 请参见 CREATE TABLE 章节
目前不支持增加主键列
修改列属性
ALTER TABLE tblname
ALTER [COLUMN] colname
[SET DEFAULT literal| DROP DEFAULT];
- 修改列类型
ALTER TABLE tblname
MODIFY colname column_definition;
- 删除列
ALTER TABLE tblname
DROP [COLUMN] colname;
不允许删除主键列或者包含索引的列。
- 表重命名
ALTER TABLE tblname
RENAME [TO] newtblname;
- 列重命名
ALTER TABLE tblname
CHANGE [COLUMN] oldcolname newcolname column_definition;
说明
说明:OceanBase1.0 只支持varchar数据类型改大,不支持其他数据类型。
示例:将表 t2 的字段 d 改名为 c,并同时修改字段类型。
ALTER TABLE t2 CHANGE COLUMN d c CHAR(10);
- 设置 Partition 表 BLOCK 大小
ALTER TABLE tblname
SET BLOCK_SIZE [=] blocksize;
- 设置该表的副本数
这里是指表的副本总数是多少。
ALTER TABLE tblname
SET REPLICA_NUM [ = ] num;
- 设置该表的压缩方式
ALTER TABLE tblname
SET COMPRESSION [=] {NONE | LZ4_1.0 | ZSTD_1.0 | SNAPPY_1.0 | ZLIB_1.0};
- 设置是否使用BloomFilter
ALTER TABLE tblname
SET USE_BLOOM_FILTER [=] {True | False};
- 设置注释信息
ALTER TABLE tblname
SET COMMENT [=] 'commentstring';
- 设置渐进合并步数
此功能是设置渐近合并步数,PROGRESSIVE_MERGE_NUM现在限制是1~64。
ALTER TABLE tblname
SET PROGRESSIVE_MERGE_NUM [=] num;
- 设置表的ZONE属性
ALTER TABLE tblname
zone_specification...;
zone_specification:
PRIMARY_ZONE [=] zone
示例
示例1:
- 增加列前,执行以下命令查看表信息。
DESCRIBE test;
ADD前表信息如下:
- 执行以下命令增加c3列。
ALTER TABLE test ADD c3 int;
- 增加列后,执行以下命令查看表信息
DESCRIBE test;
ADD后的表信息如下:
- 执行以下命令删除c3列。
ALTER TABLE test DROP c3;
- 删除列后,执行以下命令查看表信息。
DESCRIBE test;
DROP后表信息如下:
示例2:
ALTER TABLE test SET REPLICA_NUM=2, ADD COLUMN c5 INT;
报错
- 语法错误时报
ERROR 1064 (42000): Youhave an error in your SQL syntax;
- 表不存在,报
ERROR 1146 (42S02): Table'XXX' doesn't exist
- 在已有数据表中增加新主键列,报
ERROR 1503(HY000): A PRIMARY KEY must include all columns in the table
mysql> select * from employees;
+-----+--------+--------+------------+------------+----------+----------+
| id | frame | lname | hired | separated | job_code | store_id |
+-----+--------+--------+------------+------------+----------+----------+
| 4 | 4 | 4 | 2000-04-04 | 2044-04-04 | 1 | 4 |
| 5 | 5 | 5 | 2000-05-05 | 2022-05-05 | 123 | 5 |
| 8 | 8 | 8 | 2000-05-05 | 2001-08-08 | 123 | 9 |
| 2 | test | 2 | 2000-02-02 | 2024-02-02 | 2 | 2 |
| 7 | 7 | 7 | 1999-02-02 | 2007-07-07 | 7 | 10 |
| 3 | 3 | 3 | 2000-03-03 | 2034-03-03 | 3 | 3 |
| 11 | test | test | 2003-03-03 | 2003-05-05 | 11 | 11 |
+-----+--------+--------+------------+------------+----------+----------+
7 rows in set (0.01 sec)
mysql> alter table employees add id2 int primary key;
ERROR 1068 (42000): Multiple primary key defined
- 删除不存在列,报
ERROR 1091 (42000): Can't DROP 'XXX'; check that column/key exists
mysql> alter table employees drop id3;
ERROR 1091 (42000): Can't DROP 'id3'; check that column/key exists
- 数据定义类型和default数据长度不符,报
ERROR 1067 (42000): Invalid default value for 'XXX'
mysql> alter table test1 add colum1 VARCHAR(10) default 'ttttttttttttttttttttttt';
ERROR 1067 (42000): Invalid default value for 'colum1'