MySQL命令複製表的方法及不同 原

環境:Win10 64位,MySQL 5.5

下面說下練習時發現的問題及測試過程,如有不對或不全面的地方,歡迎指正,謝謝。

現有一張表user,結構及數據如下,特殊說明下,表中主鍵id爲自增長,且表中有個字段updated_time自動記錄數據更新時間,並且表中已有3個數據,id自增長已到4

mysql> desc user;
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| Field        | Type                | Null | Key | Default           | Extra                       |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| id           | int(10) unsigned    | NO   | PRI | NULL              | auto_increment              |
| name         | varchar(255)        | NO   |     | NULL              |                             |
| gender       | char(1)             | NO   |     | NULL              |                             |
| age          | tinyint(3) unsigned | NO   |     | NULL              |                             |
| updated_time | timestamp           | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.01 sec)

mysql> select * from user;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  2 | 張三     | 男       |  22 | 2018-11-01 14:15:43 |
|  3 | 小紅    | 女      |  18 | 2018-11-01 14:21:12 |
|  4 | 小明    | M      |  16 | 2018-11-02 16:48:41 |
+----+------+--------+-----+---------------------+
3 rows in set (0.00 sec)

第一種 "show create table"方式

mysql> show create table user;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                    |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `gender` char(1) NOT NULL,
  `age` tinyint(3) unsigned NOT NULL,
  `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> CREATE TABLE `user_copy` (
    ->   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    ->   `name` varchar(255) NOT NULL,
    ->   `gender` char(1) NOT NULL,
    ->   `age` tinyint(3) unsigned NOT NULL,
    ->   `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.09 sec)

mysql> show columns from user_copy;
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| Field        | Type                | Null | Key | Default           | Extra                       |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| id           | int(10) unsigned    | NO   | PRI | NULL              | auto_increment              |
| name         | varchar(255)        | NO   |     | NULL              |                             |
| gender       | char(1)             | NO   |     | NULL              |                             |
| age          | tinyint(3) unsigned | NO   |     | NULL              |                             |
| updated_time | timestamp           | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.02 sec)

mysql> select * from user_copy;
Empty set (0.00 sec)

這樣複製的表user_copy只有結構,沒有數據,需要數據的話,需要再行導入,導入方法見文章最後,這裏我們不導入,直接插入一條數據可見id直接從5開始

mysql> select * from user_copy;
Empty set (0.00 sec)

mysql> insert into user_copy(name,gender,age) values('小白','女',14);
Query OK, 1 row affected (0.02 sec)

mysql> select * from user_copy;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  5 | 小白    | 女      |  14 | 2018-11-02 17:39:34 |
+----+------+--------+-----+---------------------+
1 row in set (0.00 sec)

 

第二種 "create table user_copy2 like user;"

mysql> create table user_copy2 like user;
Query OK, 0 rows affected (0.18 sec)

mysql> desc user_copy2;
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| Field        | Type                | Null | Key | Default           | Extra                       |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| id           | int(10) unsigned    | NO   | PRI | NULL              | auto_increment              |
| name         | varchar(255)        | NO   |     | NULL              |                             |
| gender       | char(1)             | NO   |     | NULL              |                             |
| age          | tinyint(3) unsigned | NO   |     | NULL              |                             |
| updated_time | timestamp           | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.02 sec)

mysql> show create table user_copy2;
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                                                                                                                                                                         |
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_copy2 | CREATE TABLE `user_copy2` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `gender` char(1) NOT NULL,
  `age` tinyint(3) unsigned NOT NULL,
  `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

這種方式複製的表同樣只有結構沒有數據,不過,要注意的是,看錶創建語句與user表有個不同的地方是:user_copy2中沒有“AUTO_INCREMENT=5” 這句,這會導致user_copy中新添加的數據id會從1開始,添加一條試下

mysql> select * from user_copy2;
Empty set (0.00 sec)

mysql> insert into user_copy2(name,gender,age) values('小白','女',14);
Query OK, 1 row affected (0.03 sec)

mysql> select * from user_copy2;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  1 | 小白    | 女      |  14 | 2018-11-02 17:42:29 |
+----+------+--------+-----+---------------------+
1 row in set (0.00 sec)

第三種 “create table user_copy3 select * from user;”

mysql> create table user_copy3 select * from user;
Query OK, 3 rows affected (0.08 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> desc user_copy3;
+--------------+---------------------+------+-----+---------------------+-------+
| Field        | Type                | Null | Key | Default             | Extra |
+--------------+---------------------+------+-----+---------------------+-------+
| id           | int(10) unsigned    | NO   |     | 0                   |       |
| name         | varchar(255)        | NO   |     | NULL                |       |
| gender       | char(1)             | NO   |     | NULL                |       |
| age          | tinyint(3) unsigned | NO   |     | NULL                |       |
| updated_time | timestamp           | NO   |     | 0000-00-00 00:00:00 |       |
+--------------+---------------------+------+-----+---------------------+-------+
5 rows in set (0.02 sec)

mysql> show create table user_copy3;
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                                                                                                                        |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_copy3 | CREATE TABLE `user_copy3` (
  `id` int(10) unsigned NOT NULL DEFAULT '0',
  `name` varchar(255) NOT NULL,
  `gender` char(1) NOT NULL,
  `age` tinyint(3) unsigned NOT NULL,
  `updated_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from user_copy3;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  2 | 張三     | 男       |  22 | 2018-11-01 14:15:43 |
|  3 | 小紅    | 女      |  18 | 2018-11-01 14:21:12 |
|  4 | 小明    | M      |  16 | 2018-11-02 16:48:41 |
+----+------+--------+-----+---------------------+
3 rows in set (0.00 sec)

這種複製方法,會把表結構及數據都複製到user_copy3中,但不同的地方可以從user_copy3中的創建語句中可以看出來,與user的區別:

  • id的主鍵設定沒了,默認自增長也沒了,變成了默認0
  • 沒有“AUTO_INCREMENT=5”
  • updated_time的默認值由“CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP”變成了“0000-00-00 00:00:00”
mysql> select * from user_copy3;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  2 | 張三     | 男       |  22 | 2018-11-01 14:15:43 |
|  3 | 小紅    | 女      |  18 | 2018-11-01 14:21:12 |
|  4 | 小明    | M      |  16 | 2018-11-02 16:48:41 |
+----+------+--------+-----+---------------------+
3 rows in set (0.00 sec)

mysql> insert into user_copy3(name,gender,age) values('小白','女',14);
Query OK, 1 row affected (0.02 sec)

mysql> select * from user_copy3;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  2 | 張三     | 男       |  22 | 2018-11-01 14:15:43 |
|  3 | 小紅    | 女      |  18 | 2018-11-01 14:21:12 |
|  4 | 小明    | M      |  16 | 2018-11-02 16:48:41 |
|  0 | 小白    | 女      |  14 | 0000-00-00 00:00:00 |
+----+------+--------+-----+---------------------+
4 rows in set (0.00 sec)

mysql>  insert into user_copy3(name,gender,age) values('小白','女',14);
Query OK, 1 row affected (0.03 sec)

mysql>  insert into user_copy3(name,gender,age) values('小白','女',14);
Query OK, 1 row affected (0.03 sec)

mysql> select * from user_copy3;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  2 | 張三     | 男       |  22 | 2018-11-01 14:15:43 |
|  3 | 小紅    | 女      |  18 | 2018-11-01 14:21:12 |
|  4 | 小明    | M      |  16 | 2018-11-02 16:48:41 |
|  0 | 小白    | 女      |  14 | 0000-00-00 00:00:00 |
|  0 | 小白    | 女      |  14 | 0000-00-00 00:00:00 |
|  0 | 小白    | 女      |  14 | 0000-00-00 00:00:00 |
+----+------+--------+-----+---------------------+
6 rows in set (0.00 sec)

 

所以,經過測試發現,完美的複製表方法是第一種方法,這樣複製出來的表不會給你日後製造意想不到的bug

 

最後附上數據導入方法,從user導入到user_copy中如下所示

mysql> insert into user_copy(id,name,gender,age,updated_time) select id,name,gender,age,updated_time from user;
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user_copy;
+----+------+--------+-----+---------------------+
| id | name | gender | age | updated_time        |
+----+------+--------+-----+---------------------+
|  2 | 張三     | 男       |  22 | 2018-11-01 14:15:43 |
|  3 | 小紅    | 女      |  18 | 2018-11-01 14:21:12 |
|  4 | 小明    | M      |  16 | 2018-11-02 16:48:41 |
+----+------+--------+-----+---------------------+
3 rows in set (0.00 sec)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章