CREATE TABLE ... SELECT 複製資料表,index 及 key 都無法複製。
SHOW CREATE TABLE ...\G 列出原資料表完整結構。
[root@kvm8 ~]# mysql -s -uroot -p123qwe
mysql> use dyw;
mysql> show create table animals\G
*************************** 1. row ***************************
Table: animals
Create Table: CREATE TABLE `animals` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`foot` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
mysql>
clone_animals,除了第一行名稱不一樣,其他幾行直接複製貼上。
mysql> create table `clone_animals` (
-> `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-> `name` varchar(20) NOT NULL,
-> `foot` tinyint(3) unsigned NOT NULL,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
mysql>
CREATE TABLE newtable LIKE oldtable 複製表格欄位。
mysql> CREATE TABLE clone_animals LIKE animals; mysql>
INSERT INTO ... SELECT ... FROM 從原資料表複製紀錄到新的資料表。
mysql> insert into clone_animals (id,name,foot)
-> select id,name,foot from animals;
mysql>
INSERT newtable SELECT * FROM oldtable 從原資料表複製紀錄到新的資料表。
mysql> INSERT clone_animals SELECT * FROM animals; mysql>
mysql> show create table clone_animals\G
*************************** 1. row ***************************
Table: clone_animals
Create Table: CREATE TABLE `clone_animals` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`foot` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
mysql>
mysql> select * from animals; id name foot 1 dog 4 2 cat 4 3 chicken 2 mysql> select * from clone_animals; id name foot 1 dog 4 2 cat 4 3 chicken 2 mysql>