MySQL create table as与create table like对比

原创 2015年07月08日 11:06:34
      在MySQL数据库中,关于表的克隆有多种方式,比如我们可以使用create table ..as .. ,也可以使用create table .. like ..方式。然而这2种不同的方式还是有些差异的,他的差异到底在哪里呢,本文通过演示对此展开描述。


1、mysql sakila表上的结构

--actor表状态
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: 2014-12-25 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表索引
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: A
  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: A
  Cardinality: 200
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

--actor表结构
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、使用create table as方式克隆表

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 |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
--从上面的结果可以看出新表缺少了key信息,以及自增列属性 auto_increment

robin@localhost[sakila]> show table status like 'actor_as'\G
*************************** 1. row ***************************
           Name: actor_as
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 200
 Avg_row_length: 81
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2015-01-19 10:42:53
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

--从上面的表结构可以看出,表状态与原表等同,仅仅是创建时间的差异,
robin@localhost[sakila]> show index from actor_as \G
Empty set (0.00 sec)

--从上面的查询可以看出,新表没有任何索引

3、使用create table like方式克隆表

robin@localhost[sakila]> create table actor_like like actor;
Query OK, 0 rows affected (0.01 sec)

robin@localhost[sakila]> select count(*) from actor_like;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)
--从上面的查询可知,使用like方式没有任何数据被克隆到新表

robin@localhost[sakila]> desc actor_like;
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| 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 |
+-------------+----------------------+------+-----+-------------------+-----------------------------+

robin@localhost[sakila]> show index from actor_like\G
*************************** 1. row ***************************
        Table: actor_like
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: actor_id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: actor_like
   Non_unique: 1
     Key_name: idx_actor_last_name
 Seq_in_index: 1
  Column_name: last_name
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

--从上面的表结构以及索引信息可以看到,表除了没有数据之外,结构被进行了完整克隆
--下面为like方式的表插入数据
robin@localhost[sakila]> insert into actor_like select * from actor;
Query OK, 200 rows affected (0.03 sec)
Records: 200  Duplicates: 0  Warnings: 0

robin@localhost[sakila]> show index from actor_like\G
*************************** 1. row ***************************
        Table: actor_like
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: actor_id
    Collation: A
  Cardinality: 200
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: actor_like
   Non_unique: 1
     Key_name: idx_actor_last_name
 Seq_in_index: 1
  Column_name: last_name  -- Author: Leshami
    Collation: A          -- Blog  : http://blog.csdn.net/leshami 
  Cardinality: 200
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)
--上面的查询中新表的索引统计信息被收集

robin@localhost[sakila]> explain select * from actor where last_name like 'A%';
+----+-------------+-------+-------+---------------------+---------------------+---------+------+------+-----------------------+
| id | select_type | table | type  | possible_keys       | key                 | key_len | ref  | rows | Extra                 |
+----+-------------+-------+-------+---------------------+---------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | actor | range | idx_actor_last_name | idx_actor_last_name | 137     | NULL |    7 | Using index condition |
+----+-------------+-------+-------+---------------------+---------------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

robin@localhost[sakila]> explain select * from actor_like where last_name like 'A%';
+----+-------------+------------+-------+---------------------+---------------------+---------+------+------+-----------------------+
| id | select_type | table      | type  | possible_keys       | key                 | key_len | ref  | rows | Extra                 |
+----+-------------+------------+-------+---------------------+---------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | actor_like | range | idx_actor_last_name | idx_actor_last_name | 137     | NULL |    7 | Using index condition |
+----+-------------+------------+-------+---------------------+---------------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
--从上面的执行计划可以看出,like方式建表与原表使用了相同的执行计划

4、基于myisam引擎进行create table like方式克隆

robin@localhost[sakila]> alter table actor_like engine=myisam;
Query OK, 200 rows affected (0.03 sec)
Records: 200  Duplicates: 0  Warnings: 0

robin@localhost[sakila]> show table status like 'actor_like'\G
*************************** 1. row ***************************
           Name: actor_like
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 200
 Avg_row_length: 25
    Data_length: 5016
Max_data_length: 281474976710655
   Index_length: 7168
      Data_free: 0
 Auto_increment: 201
    Create_time: 2015-01-19 11:19:55
    Update_time: 2015-01-19 11:19:55
     Check_time: 2015-01-19 11:19:55
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

robin@localhost[sakila]> create table actor_like_isam like actor_like;
Query OK, 0 rows affected (0.01 sec)

robin@localhost[sakila]> insert into actor_like_isam select * from actor_like;
Query OK, 200 rows affected (0.00 sec)
Records: 200  Duplicates: 0  Warnings: 0

robin@localhost[sakila]> insert into actor_like_isam select * from actor_like;
Query OK, 200 rows affected (0.00 sec)
Records: 200  Duplicates: 0  Warnings: 0

robin@localhost[sakila]> show index from actor_like_isam\G
*************************** 1. row ***************************
        Table: actor_like_isam
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: actor_id
    Collation: A
  Cardinality: 200
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: actor_like_isam
   Non_unique: 1
     Key_name: idx_actor_last_name
 Seq_in_index: 1
  Column_name: last_name
    Collation: A
  Cardinality: 100
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

robin@localhost[sakila]> explain select * from actor_like_isam where last_name like 'A%';
+----+-------------+-----------------+-------+---------------------+---------------------+---------+------+------+-----------------------+
| id | select_type | table           | type  | possible_keys       | key                 | key_len | ref  | rows | Extra                 |
+----+-------------+-----------------+-------+---------------------+---------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | actor_like_isam | range | idx_actor_last_name | idx_actor_last_name | 137     | NULL |    6 | Using index condition |
+----+-------------+-----------------+-------+---------------------+---------------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

--从上面的测试可以看出基于myisam引擎方式对原表结构也是使用完成克隆方式

5、小结
a、create table like方式会完整地克隆表结构,但不会插入数据,需要单独使用insert into或load data方式加载数据
b、create table as  方式会部分克隆表结构,完整保留数据
c、create table as select .. where 1=0 会克隆部分表结构,但不克隆数据。
d、如果启用了gtid,create table as方式不被支持。收到ERROR 1786 (HY000): CREATE TABLE ... SELECT is forbidden when @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1.

版权声明:本文为博主原创文章,欢迎扩散,扩散请务必注明出处。

MySQL数据库表的基本操作——创建表CREATE TABLE

在操作数据库表之前要先选择数据库,USE
  • u011701678
  • u011701678
  • 2013年08月31日 13:14
  • 28487

MySQL 5.6 create table like 同步异常问题分析与总结

我们都知道,mysql5.6版本如果设置了enforce-gtid-consistency=true,则mysql禁止执行create table ... select ...语句,原因是这条语句在m...
  • u010027484
  • u010027484
  • 2016年11月17日 09:40
  • 284

create table like 和create table select 比较

语法: CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name     [(create_definition,...)]     [table_op...
  • Heng_Ji
  • Heng_Ji
  • 2013年01月10日 16:00
  • 22866

PostgreSQL 动态表复制(CREATE TABLE...LIKE)

关于PostgreSQL实现动态表复制的全过程~
  • wlwlwlwl015
  • wlwlwlwl015
  • 2016年09月13日 09:25
  • 4594

慎用create table as select,一定要注意默认值的问题

慎用create table as select,一定要注意默认值的问题 博客分类:  Oracle oraclecreate talbe as select数据库迁移  再做一些数据迁移...
  • haiross
  • haiross
  • 2013年11月28日 10:44
  • 83408

create table:使用SELECT语句创建表

参考:http://blog.itpub.net/24237320/viewspace-1098689/oracle下直接create table xxx as select * from ...即可...
  • ClamReason
  • ClamReason
  • 2015年10月10日 11:18
  • 7052

[SQL]实现Oracle拷贝表Create table as select

Transact-SQL 拷贝表 在做数据迁移的时候会经常复制表,或者只复制表结构,在ORACLE下可以使用:create table as select...,但是在SQLServer下这种语法是...
  • dietime1943
  • dietime1943
  • 2017年05月26日 09:29
  • 543

mysql 之as 与 like创建表之差异

对于MySQL的复制相同表结构方法,有create table as 和create table like 两种,区别是什么呢?     create table t2 as select ...
  • u013757199
  • u013757199
  • 2016年05月23日 10:02
  • 340

mysql更改表引擎INNODB为MyISAM的方法总结

mysql数据库分类为INNODB为MyISAM两种表存储引擎了,两种各有优化在不同类型网站可能选择不同,下面小编为各位介绍mysql更改表引擎INNODB为MyISAM技巧。 常见的mysql表引...
  • sanbingyutuoniao123
  • sanbingyutuoniao123
  • 2015年11月25日 16:17
  • 1763

Oracle创建表语句(Create table)语法详解及示例

Oracle创建表语句(Create table)语法详解及示例     创建表(Create table)语法详解 1. ORACLE常用的字段类型 ORACLE常用的字段类型有 VA...
  • haiross
  • haiross
  • 2013年09月17日 15:12
  • 239259
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:MySQL create table as与create table like对比
举报原因:
原因补充:

(最多只允许输入30个字)