MySQL復制表-CREATE SELECT


假設存在以下Table:

mysql> select * from staff;
+----+----------+-------+
| id | name     | slary |
+----+----------+-------+
|  3 | haofugui | 10000 |
|  4 | guoming  |  3500 |
|  5 | haotian  |  2900 |
+----+----------+-------+
3 rows in set (0.00 sec)

mysql> describe staff;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | YES  |     | NULL    |                |
| slary | int(11)  | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

 

1. 只復制表結構到新表

語句1:CREATE TABLE new_table_name SELECT [field1,field2... | *] FROM old_table_name WHERE 1=2;

語句2:CREATE TABLE new_table _name LIKE old_table_name;

示例:

 

mysql> create table staff_bak select id,name from staff where 1=2; //根據舊表的指定屬性創建一個新的空表
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from staff_bak;   //新建的數據庫為空表
Empty set (0.00 sec)

mysql> describe staff_bak;       //原表的主鍵和自動增長不能被復制 +-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   |     | 0       |       |
| name  | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 

mysql> create table staff_bak_1 like staff;  //根據舊表創建一個新的空表,無法指定屬性或屬性組
Query OK, 0 rows affected (0.03 sec)

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

mysql> describe staff_bak_1;    //所有數據類型和完整性約束條件都能被復制,包括主鍵和自動增長 +-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | YES  |     | NULL    |                |
| slary | int(11)  | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

 

注意:語句1可指定復制的屬性范圍,但無法復制主鍵類型和自增方式;

          語句2會把舊表的所有字段類型都復制到新表,但無法復制指定屬性或屬性組。

 

2. 復制表結構及數據到新表

語句:CREATE TABLE new_table_name SELECT [field1,field2... | *] FROM old_table_name;

mysql> create table staff_bak select id,name from staff; //根據舊表將指定屬性及其數據創建新表
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> describe staff_bak;    //新表結構展示 +-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   |     | 0       |       |
| name  | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from staff_bak;  //新表數據顯示 +----+----------+
| id | name     |
+----+----------+
|  3 | haofugui |
|  4 | guoming  |
|  5 | haotian  |
+----+----------+
3 rows in set (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM