我们如何自动定义与另一个表相同的MySQL表结构?
使用LIKE关键字的CREATETABLE命令将能够定义MySQL表的结构与另一个表的结构相同。
语法
CREATE TABLE new_table LIKE old_table;
示例
mysql> Create table employee(ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT, NAME VARCHAR(20)); mysql> Describe employee; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | NAME | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.07 sec)
下面的查询将创建与表employee具有相似结构的表employee1。可以通过运行DESCRIBE查询来检查它。
mysql> create table employee1 like employee; mysql> describe employee1; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | NAME | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.14 sec)