Three methods for copying table data and table structure in mysql
When will we use a copy table? For example, if I operate on a table but are afraid of accidental deletion of data, create a table with the same structure in the same database and create a backup with the same table data. If mysqldump is difficult to use, back up files such as. MYD and. MYI.
I. Copy the table structure
Method 1:
- 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)
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:
- 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)
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:
- 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)
Mysql> show create table users \ G; // display the SQL statement ******************************* 1. row *************************** Table: usersCreate Table: create table 'users' (// change 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_increment1 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:
- 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
Mysql> create table c select * from users; // copy the sqlQuery OK of the table, 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
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 and is often used in the past.