mysql 複製表結構、表資料的方法

來源:互聯網
上載者:User

標籤:

From: http://blog.163.com/[email protected]/blog/static/109463675201191173221759/

一,複製表結構
方法1:
mysql> create table a like users;         //複製表結構  
Query OK, 0 rows affected (0.50 sec)   
  
mysql> show tables;   
+----------------+   
| Tables_in_test |   
+----------------+   
| a              |   
| users          |   
+----------------+   
2 rows in set (0.00 sec)  
mysql> create table a like users;         //複製表結構
Query OK, 0 rows affected (0.50 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
| users          |
+----------------+
2 rows in set (0.00 sec)

方法2:
mysql> create table b select * from users limit 0;   //複製表結構  
Query OK, 0 rows affected (0.00 sec)   
Records: 0  Duplicates: 0  Warnings: 0   
  
mysql> show tables;   
+----------------+   
| Tables_in_test |   
+----------------+   
| a              |   
| b              |   
| users          |   
+----------------+   
3 rows in set (0.00 sec)  
mysql> create table b select * from users limit 0;   //複製表結構
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
| b              |
| users          |
+----------------+
3 rows in set (0.00 sec)

方法3:
mysql> show create table users\G;          //顯示創表的sql  這裡也可以用 desc users;顯示表的結構
*************************** 1. row ***************************   
Table: users   
Create Table: CREATE TABLE `users` (       //改表名  
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,   
`user_name` varchar(60) NOT NULL DEFAULT ‘‘,   
`user_pass` varchar(64) NOT NULL DEFAULT ‘‘,   
PRIMARY KEY (`ID`)   
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8  //改auto_increment  
1 row in set (0.00 sec)  
mysql> show create table users\G;          //顯示創表的sql
*************************** 1. row ***************************
Table: users
Create Table: CREATE TABLE `users` (       //改表名
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(60) NOT NULL DEFAULT ‘‘,
`user_pass` varchar(64) NOT NULL DEFAULT ‘‘,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8  //改auto_increment
1 row in set (0.00 sec)把sql語句copy出來,改一下表名和atuo_increment,然後在執行一下。
二,複製表資料,以及表結構
方法1:
mysql> create table c select * from users;      //複製表的sql  
Query OK, 4 rows affected (0.00 sec)   
Records: 4  Duplicates: 0  Warnings: 0  
mysql> create table c select * from users;      //複製表的sql
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

方法2:
mysql> create table d select user_name,user_pass from users where id=1;   
Query OK, 1 row affected (0.00 sec)   
Records: 1  Duplicates: 0  Warnings: 0  
mysql> create table d select user_name,user_pass from users where id=1;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0上面的2種方法,方便,快捷,靈活性強。
方法3:
先建立一個空表, INSERT INTO 新表 SELECT * FROM 舊錶 ,或者
INSERT INTO 新表(欄位1,欄位2,…….) SELECT 欄位1,欄位2,…… FROM 舊錶
這種方法不是很方便,也是我以前經常用的。

 

mysql 複製表結構、表資料的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.