1. 程式人生 > >MySQL create table as與create table like對比

MySQL create table as與create table like對比

      在MySQL資料庫中,關於表的克隆有多種方式,比如我們可以使用create table ..as .. ,也可以使用create table .. like ..方式。然而這2種不同的方式還是有些差異的,他的差異到底在哪裡呢,本文通過演示對此展開描述。

1、mysql sakila表上的結構

--actor表狀態
[email protected][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表索引
[email protected]
[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表結構
[email protected]
[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方式克隆表

[email protected][sakila]> create table actor_as as select * from actor;
Query OK, 200 rows affected (0.06 sec)
Records: 200  Duplicates: 0  Warnings: 0

[email protected][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

[email protected][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)

--從上面的表結構可以看出,表狀態與原表等同,僅僅是建立時間的差異,
[email protected][sakila]> show index from actor_as \G
Empty set (0.00 sec)

--從上面的查詢可以看出,新表沒有任何索引

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

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

[email protected][sakila]> select count(*) from actor_like;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)
--從上面的查詢可知,使用like方式沒有任何資料被克隆到新表

[email protected][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 |
+-------------+----------------------+------+-----+-------------------+-----------------------------+

[email protected][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方式的表插入資料
[email protected][sakila]> insert into actor_like select * from actor;
Query OK, 200 rows affected (0.03 sec)
Records: 200  Duplicates: 0  Warnings: 0

[email protected][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)
--上面的查詢中新表的索引統計資訊被收集

[email protected][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)

[email protected][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方式克隆

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

[email protected][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)

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

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

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

[email protected][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)

[email protected][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 ascreate table like對比

      在MySQL資料庫中,關於表的克隆有多種方式,比如我們可以使用create table ..as .. ,也可以使用create table .. like ..方式。然而這2種不同的方式還是有些差異的,他的差異到底在哪裡呢,本文通過演示對此展開描述。1、mysq

create table ,create as create like三種建表方式的使用詳解

     Hive的hql是基於sql而來,而sql中關於表的建立有幾種方式。同樣,hive也支援這些表的建立方式。      官網文件關於建表的地址:hive常見建表方式官網文件 1.自主創業方式create table&nb

最簡單的方法設定table圓角去掉TABLE內邊框

網上看到許多HTML設定table圓角的方法,有用圖片裁下四個角的,有用CSS實現的老長的程式碼,找了很久沒找到滿意的。 突然靈光一閃,發現其實只要在table的外面套上一個圓角的DIV盒子,把overflow設為hidden就好了~簡單粗暴! 去掉table內邊框的方法

MySQL中表復制:create table like create table as select

brush ref blank 自身 lan select targe pre nbsp CREATE TABLE A LIKE B   此種方式在將表B復制到A時候會將表B完整的字段結構和索引復制到表A中來。 CREATE TABLE A AS SE

MySQL索引使用總結--索引建立方法CREATE INDEXALTER TABLE的區別

MySQL中建立索引的兩種方式 CREATE INDEX index_name ON table_1(filed_1); ALTER TABLE table_1 ADD INDEX(fiel

create table as 復制not null

null 插入 creat created into type from columns err 創建一張表包含非空約束,唯一約束和check約束 SQL> create table dept( 2 ename varchar2(20) constraint me

Oracle 中create table as 複製表結構

Oracle 中create table as 複製表結構 在日常測試或者資料遷移的過程中,經常使用create table as …複製表結構或者複製表結構和資料,用法如下: 複製表結構:create table tab_target as select * from table wh

Hive Create table as select

Hive wiki Create/Drop/Truncate Table 排查問題用到了Create table as create table xxx as select create table table1 as select * from t

db2中create table as的建表方式

DB2沒有類似oracle的create table as select來定義表的方式,所以DB2中把SELECT查詢出來的結果定義為別的表比較麻煩! DB2定義表的3種方式: 首先建立一個例子表,再插入幾條語句。 create table t1(col1 va

慎用create table as select,一定要注意預設值的問題---大一臨時表方法

SQL > create table emp_copy as select * from emp where deptno=10; 第一,注意emp_copy表中沒有定義任何列名,因為我們在列子句中用萬用字元從emp表取得資料,讓Oracle像emp表中一樣生成emp_copy表中的列——相同名稱

sqlserver不能直接create table as select ......

在sqlserver 下想複製一張表的,想到oracle下直接create table xxx as select * from ....即可。 但是結果卻是錯誤的,baidu一下發現。 sqlserver的語法是 : select * into tablenew fr

mysql sql語句 create table時 報錯table doesn't exist

用sql還原一個年代久遠的專案時候發現create table執行時報表不存在。。(這不廢話,不存在我才要create啊)刪了庫重來還是報錯, 對於這個神奇的現象百之,無。谷之,中文網頁無。英文找到了,方法是先新建一個空表,然後刪除,再然後就可以用sql建立其他內容的表了

MySQL ERROR 1005: Can't create table 分析

在mysql 中建立引用約束的時候會出現MySQL ERROR 1005: Can't create table (errno: 150)的錯誤資訊結果是不能建立 引用約束。 出現問題的幾種情況如下: 1、外來鍵的引用型別不一樣,如主鍵是int外來鍵是char 2、

使用create table ...as建立表時要注意的問題

工作中有時候做hive開發了,需要對一張表進行備份。一般都會使用 create table as  ...簡單方便,但是需要注意as建表產生的問題,因為as建表並不會儲存原表樣式。 create ta

1z0-071 Oracle Database 12c SQL 第68題 CREATE TABLE AS

Q68. View the exhibit and examine the structure of the SALES, CUSTOMERS, PRODUCTS and TIMES tables.The PROD_ID column is the foreign key i

轉換sql文件的create table語句為drop table語句

void sql文件 iter test imp rgs pan 轉換 ont 1 package com.csii.pweb.query.action; 2 3 import java.io.BufferedReader; 4 import java.io.F

mysql 索引查詢 、建立 create index add index 的區別

1、索引查詢 ------TABLE_SCHEMA  庫名;TABLE  表名 ------AND UPPER(INDEX_NAME) != 'PRIMARY'  只查詢索引,不需要主鍵 SELECTCONCAT('ALTER TABLE `',TABLE_NAME,'`

How to Create and Query a NoSQL table

Amazon Web Services is Hiring. Amazon Web Services (AWS) is a dynamic, growing business unit within Amazon.com. We are currently hiring So

mysqlas like建立表之差異

對於MySQL的複製相同表結構方法,有create table as 和create table like 兩種,區別是什麼呢?     create table t2 as select *

Mysql innodb錯誤解決 InnoDB: Error: table `mysql`.`innodb_table_stats`

mysql innodb innodb_table_stats 通過ELK監控發現,程序連接mysql DB 失敗,通過看程序的log和mysql的error log發現mysql中出現error查看Mysql日誌發現 InnoDB: Error: table `mysql`.`innodb_ta