13.1.9.3 ALTER TABLE Examples示例

Begin with a table t1 created as shown here:首先创建一个表t1,如下所示:

CREATE TABLE t1 (a INTEGER, b CHAR(10));

To rename the table from t1 to t2:要将表从t1重命名为t2,请执行以下操作:

ALTER TABLE t1 RENAME t2;

To change column a from INTEGER to TINYINT NOT NULL (leaving the name the same), and to change column b from CHAR(10) to CHAR(20) as well as renaming it from b to c:要将a列从INTEGER更改为TINYINT NOT NULL(名称保持不变),将b列从CHAR(10)更改为CHAR(20),并将其从b重命名为c,请执行以下操作:

ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);

To add a new TIMESTAMP column named d:要添加名为d的新TIMESTAMP列,请执行以下操作:

ALTER TABLE t2 ADD d TIMESTAMP;

To add an index on column d and a UNIQUE index on column a:要在d列上添加索引并在a列上添加UNIQUE索引,请执行以下操作:

ALTER TABLE t2 ADD INDEX (d), ADD UNIQUE (a);

To remove column c:要删除c列,请执行以下操作:

ALTER TABLE t2 DROP COLUMN c;

To add a new AUTO_INCREMENT integer column named c:要添加名为c的新AUTO_INCREMENT整数列,请执行以下操作:

ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
  ADD PRIMARY KEY (c);

We indexed c (as a PRIMARY KEY) because AUTO_INCREMENT columns must be indexed, and we declare c as NOT NULL because primary key columns cannot be NULL.我们索引了c(作为PRIMARY KEY主键),因为必须索引PRIMARY KEY列,并且我们声明cNOT NULL,因为主键列不能为NULL

For NDB tables, it is also possible to change the storage type used for a table or column. 对于NDB表,还可以更改用于表或列的存储类型。For example, consider an NDB table created as shown here:例如,考虑如下所示创建的NDB表:

mysql> CREATE TABLE t1 (c1 INT) TABLESPACE ts_1 ENGINE NDB;
Query OK, 0 rows affected (1.27 sec)

To convert this table to disk-based storage, you can use the following ALTER TABLE statement:要将此表转换为基于磁盘的存储,可以使用以下ALTER TABLE语句:

mysql> ALTER TABLE t1 TABLESPACE ts_1 STORAGE DISK;
Query OK, 0 rows affected (2.99 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `c1` int(11) DEFAULT NULL
) /*!50100 TABLESPACE ts_1 STORAGE DISK */
ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.01 sec)

It is not necessary that the tablespace was referenced when the table was originally created; however, the tablespace must be referenced by the ALTER TABLE:最初创建表时不必引用表空间;但是,表空间必须由ALTER TABLE引用:

mysql> CREATE TABLE t2 (c1 INT) ts_1 ENGINE NDB;
Query OK, 0 rows affected (1.00 sec)

mysql> ALTER TABLE t2 STORAGE DISK;
ERROR 1005 (HY000): Can't create table 'c.#sql-1750_3' (errno: 140)
mysql> ALTER TABLE t2 TABLESPACE ts_1 STORAGE DISK;
Query OK, 0 rows affected (3.42 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> SHOW CREATE TABLE t2\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t2` (
  `c1` int(11) DEFAULT NULL
) /*!50100 TABLESPACE ts_1 STORAGE DISK */
ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.01 sec)

To change the storage type of an individual column, you can use ALTER TABLE ... MODIFY [COLUMN]. 要更改单个列的存储类型,可以使用ALTER TABLE ... MODIFY [COLUMN]For example, suppose you create an NDB Cluster Disk Data table with two columns, using this CREATE TABLE statement:例如,假设使用以下CREATE TABLE语句创建一个包含两列的NDB群集磁盘数据表:

mysql> CREATE TABLE t3 (c1 INT, c2 INT)
    ->     TABLESPACE ts_1 STORAGE DISK ENGINE NDB;
Query OK, 0 rows affected (1.34 sec)

To change column c2 from disk-based to in-memory storage, include a STORAGE MEMORY clause in the column definition used by the ALTER TABLE statement, as shown here:要将列c2从基于磁盘的存储更改为内存存储,请在ALTER TABLE语句使用的列定义中包含一个STORAGE MEMORY子句,如下所示:

mysql> ALTER TABLE t3 MODIFY c2 INT STORAGE MEMORY;
Query OK, 0 rows affected (3.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

You can make an in-memory column into a disk-based column by using STORAGE DISK in a similar fashion.通过以类似的方式使用STORAGE DISK,可以将内存中的列转换为基于磁盘的列。

Column c1 uses disk-based storage, since this is the default for the table (determined by the table-level STORAGE DISK clause in the CREATE TABLE statement). c1使用基于磁盘的存储,因为这是表的默认值(由CREATE TABLE语句中的表级STORAGE DISK子句确定)。However, column c2 uses in-memory storage, as can be seen here in the output of SHOW CREATE TABLE:但是,列c2使用内存存储,如SHOW CREATE TABLE的输出所示:

mysql> SHOW CREATE TABLE t3\G
*************************** 1. row ***************************
       Table: t3
Create Table: CREATE TABLE `t3` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) /*!50120 STORAGE MEMORY */ DEFAULT NULL
) /*!50100 TABLESPACE ts_1 STORAGE DISK */ ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.02 sec)

When you add an AUTO_INCREMENT column, column values are filled in with sequence numbers automatically. 添加AUTO_INCREMENT列时,列值将自动填充序列号。For MyISAM tables, you can set the first sequence number by executing SET INSERT_ID=value before ALTER TABLE or by using the AUTO_INCREMENT=value table option.对于MyISAM表,可以通过在ALTER TABLE之前执行SET INSERT_ID=value或使用表选项选项AUTO_INCREMENT=value来设置第一个序列号。

With MyISAM tables, if you do not change the AUTO_INCREMENT column, the sequence number is not affected. 对于MyISAM表,如果不更改AUTO_INCREMENT列,则序列号不受影响。If you drop an AUTO_INCREMENT column and then add another AUTO_INCREMENT column, the numbers are resequenced beginning with 1.如果删除一个AUTO_INCREMENT列,然后添加另一个AUTO_INCREMENT列,则数字将从1开始重新排序。

When replication is used, adding an AUTO_INCREMENT column to a table might not produce the same ordering of the rows on the replica and the source. 使用复制时,向表中添加AUTO_INCREMENT列可能不会产生副本和源上相同的行顺序。This occurs because the order in which the rows are numbered depends on the specific storage engine used for the table and the order in which the rows were inserted. 这是因为行的编号顺序取决于用于表的特定存储引擎以及插入行的顺序。If it is important to have the same order on the source and replica, the rows must be ordered before assigning an AUTO_INCREMENT number. 如果源和副本的顺序相同很重要,则在分配AUTO_INCREMENT编号之前,必须对行进行排序。Assuming that you want to add an AUTO_INCREMENT column to the table t1, the following statements produce a new table t2 identical to t1 but with an AUTO_INCREMENT column:假设要向表t1添加AUTO_INCREMENT列,以下语句将生成一个与t1相同但具有AUTO_INCREMENT列的新表t2

CREATE TABLE t2 (id INT AUTO_INCREMENT PRIMARY KEY)
SELECT * FROM t1 ORDER BY col1, col2;

This assumes that the table t1 has columns col1 and col2.这假设表t1col1col2列。

This set of statements also produces a new table t2 identical to t1, with the addition of an AUTO_INCREMENT column:这组语句还生成一个与t1相同的新表t2,并添加了一个AUTO_INCREMENT列:

CREATE TABLE t2 LIKE t1;
ALTER TABLE t2 ADD id INT AUTO_INCREMENT PRIMARY KEY;
INSERT INTO t2 SELECT * FROM t1 ORDER BY col1, col2;
Important重要

To guarantee the same ordering on both source and replica, all columns of t1 must be referenced in the ORDER BY clause.为了保证源和副本上的顺序相同,必须在ORDER BY子句中引用t1的所有列。

Regardless of the method used to create and populate the copy having the AUTO_INCREMENT column, the final step is to drop the original table and then rename the copy:无论使用何种方法创建和填充具有AUTO_INCREMENT列的副本,最后一步是删除原始表,然后重命名副本:

DROP TABLE t1;
ALTER TABLE t2 RENAME t1;