I. How to copy the table structure 1: view the copy print? Rows; copy the table structure QueryOK, 0 rowsaffected (0.50sec) mysqlshowtables; -------------- | Tables_in_test | tables | a | users | -------------- 2 rowsinset (0.00sec) Method 2: view the replay
I. How to copy the table structure 1: view the copy print? Rows; // copy the table structure QueryOK, 0 rowsaffected (0.50sec) mysqlshowtables; -------------- | Tables_in_test | tables | a | users | -------------- 2 rowsinset (0.00sec) Method 2: view the replay
I. Copy the table structure
Method 1:
View copy print?
- Mysql> create table a like users; // copy the 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:
View copy print?
- Mysql> create table B select * from users limit 0; // copy the 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:
View copy print?
- Mysql> show create table users \ G; // displays the SQL statement used to create a table.
- * *************************** 1. row ***************************
- Table: users
- Create Table: create table 'users' (// modify the 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 // modify auto_increment
- 1 row in set (0.00 sec)
Copy the SQL statement, change the table name and atuo_increment, and execute the command.
Ii. Copy table data and Table Structure
Method 1:
View copy print?
- Mysql> create table c select * from users; // copy the SQL statement of the 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