Comparison between MySQLcreatetableas and createtablelike

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.