In MySQL databases, there are multiple ways to clone tables. For example, you can use createtable... Or createtable. However
In MySQL databases, there are multiple ways to clone tables. For example, you can use create table... or create table. However
In MySQL databases, there are multiple ways to clone tables. For example, you can use create table... or create table. However, there are still some differences between the two methods. Where is the difference? This article describes it through demonstration.
1. Structure of mysql sakila table
-- Actor Table Status
Robin @ localhost [sakila]> show table status like 'actor '\ G
* *************************** 1. row ***************************
Name: actor
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 200
Avg_row_length: 81
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 0
Auto_increment: 201
Create_time: 13:08:25
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
-- Actor table Index
Robin @ localhost [sakila]> show index from actor \ G
* *************************** 1. row ***************************
Table: actor
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: actor_id
Collation:
Cardinality: 200
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
* *************************** 2. row ***************************
Table: actor
Non_unique: 1
Key_name: idx_actor_last_name
Seq_in_index: 1
Column_name: last_name
Collation:
Cardinality: 200
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.00 sec)
-- Actor table structure
Robin @ localhost [sakila]> desc actor;
+ ------------- + ---------------------- + ------ + ----- + ------------------- + ----------------------------- +
| Field | Type | Null | Key | Default | Extra |
+ ------------- + ---------------------- + ------ + ----- + ------------------- + ----------------------------- +
| Actor_id | smallint (5) unsigned | NO | PRI | NULL | auto_increment |
| First_name | varchar (45) | NO | NULL |
| Last_name | varchar (45) | NO | MUL | NULL |
| Last_update | timestamp | NO | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+ ------------- + ---------------------- + ------ + ----- + ------------------- + ----------------------------- +
4 rows in set (0.00 sec)
2. Use create table as to clone a table
Robin @ localhost [sakila]> create table actor_as as select * from actor;
Query OK, 200 rows affected (0.06 sec)
Records: 200 Duplicates: 0 Warnings: 0
Robin @ localhost [sakila]> desc actor_as;
+ ------------- + ---------------------- + ------ + ----- + ------------------- + ----------------------------- +
| Field | Type | Null | Key | Default | Extra |
+ ------------- + ---------------------- + ------ + ----- + ------------------- + ----------------------------- +
| Actor_id | smallint (5) unsigned | NO | 0 |
| First_name | varchar (45) | NO | NULL |
| Last_name | varchar (45) | NO | NULL |
| Last_update | timestamp | NO | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+ ------------- + ---------------------- + ------ + ----- + ------------------- + ----------------------------- +
-- From the above results, we can see that the new table lacks key information and the auto-incrementing column property auto_increment.