CREATE TABLE ... SELECT 複製資料表,index 及 key 都無法複製。要複製一個完全一樣的資料表,可使用以下步驟:
[root@kvm3 ~]# mycli -uroot -p123qwe Connecting to socket /var/lib/mysql/mysql.sock, owned by user mysql MariaDB 10.5.16 mycli 1.26.1 Home: http://mycli.net Bug tracker: https://github.com/dbcli/mycli/issues Thanks to the contributor - zer09
MariaDB root@(none):(none)> use dyw; You are now connected to database "dyw" as user "root" Time: 0.002s
SHOW CREATE TABLE ...\G 列出原資料表完整結構。
MariaDB root@(none):dyw> 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=16 DEFAULT CHARSET=utf8 1 row in set Time: 0.050s
clone_animals,除了第一行名稱不一樣,其他幾行直接複製貼上。
MariaDB root@(none):dyw> 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=16 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected
Time: 0.083s
CREATE TABLE newtable LIKE oldtable 複製表格欄位。
MariaDB root@(none):dyw> create table clone1_animals like animals; Query OK, 0 rows affected Time: 0.037s
animals 與複製的新表格 clone1_animals,欄位特徵都一樣。
MariaDB root@(none):dyw> desc animals; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | <null> | auto_increment | | name | varchar(20) | NO | | <null> | | | foot | tinyint(3) unsigned | NO | | <null> | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set Time: 0.026s MariaDB root@(none):dyw> desc clone_animals; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | <null> | auto_increment | | name | varchar(20) | NO | | <null> | | | foot | tinyint(3) unsigned | NO | | <null> | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set Time: 0.025s MariaDB root@(none):dyw> desc clone1_animals; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | <null> | auto_increment | | name | varchar(20) | NO | | <null> | | | foot | tinyint(3) unsigned | NO | | <null> | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set Time: 0.019s
animals 有四筆紀錄,但複製的新表格 clone1_animals沒有任何紀錄。
MariaDB root@(none):dyw> select * from animals; +----+---------+------+ | id | name | foot | +----+---------+------+ | 1 | dog | 4 | | 2 | cat | 4 | | 3 | chicken | 2 | | 15 | bird | 2 | +----+---------+------+ 4 rows in set Time: 0.026s MariaDB root@(none):dyw> select * from clone_animals; +----+------+------+ | id | name | foot | +----+------+------+ +----+------+------+ 0 rows in set Time: 0.024s MariaDB root@(none):dyw> select * from clone1_animals; +----+------+------+ | id | name | foot | +----+------+------+ +----+------+------+ 0 rows in set Time: 0.017s
INSERT INTO ... SELECT ... FROM 從原資料表複製紀錄到新的資料表。
MariaDB root@(none):dyw> insert into clone_animals (id,name,foot) select id,name,
-> foot from animals;
Query OK, 4 rows affected
Time: 0.020s
INSERT newtable SELECT * FROM oldtable 從原資料表複製紀錄到新的資料表。
MariaDB root@(none):dyw> insert into clone1_animals select * from animals; Query OK, 4 rows affected Time: 0.006s
clone_animals 及 clone1_animals,已有與原表格animals 完全一樣的四筆紀錄。
MariaDB root@(none):dyw> select * from clone_animals; +----+---------+------+ | id | name | foot | +----+---------+------+ | 1 | dog | 4 | | 2 | cat | 4 | | 3 | chicken | 2 | | 15 | bird | 2 | +----+---------+------+ 4 rows in set Time: 0.018s MariaDB root@(none):dyw> select * from clone1_animals; +----+---------+------+ | id | name | foot | +----+---------+------+ | 1 | dog | 4 | | 2 | cat | 4 | | 3 | chicken | 2 | | 15 | bird | 2 | +----+---------+------+ 4 rows in set Time: 0.022s