1, Copy table structure
Method 1:
- mysql> create table a like users; //Copy table structure
- Query OK, 0 rows affected (0.50 sec)
- mysql> show tables;
- +----------------+
- | Tables_in_test |
- +----------------+
- | a |
- | users |
- +----------------+
- 2 rows in set (0.00 sec)
Method 2:
- mysql> create table b select * from users limit 0; //Copy table structure
- 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)
Method 3:
- mysql> show create table users\G; //Display the created sql
- *************************** 1. row ***************************
- Table: users
- Create Table: CREATE TABLE `users` ( //Change table name
- `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 //Change auto increment
- 1 row in set (0.00 sec)
copy the sql statement, change the table name and atuo increment, and then execute it.
2, Copy table data and table structure
Method 1:
- mysql> create table c select * from users; //sql of replication table
- Query OK, 4 rows affected (0.00 sec)
- Records: 4 Duplicates: 0 Warnings: 0
Method 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
The above two methods are convenient, fast and flexible.
Method 3:
First create an empty table, INSERT INTO new table SELECT * FROM old table, or
INSERT INTO new table (field 1, field 2 .) SELECT field 1, field 2 FROM old table
This method is not very convenient, which I used to use.