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
列,并且我们声明c
为NOT 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=
before value
ALTER TABLE
or by using the AUTO_INCREMENT=
table option.value
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
.t1
有col1
和col2
列。
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;
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;