create table as 和create table like的区别

对于MySQL的复制相同表结构方法,有create table as 和create table like 两种,区别是什么呢?

/*
-- 没有开启gtid的情况下,不拷贝数据,只创建一模一样的表结构,包括索引约束等,结合insert语句可以实现复制一个表的结构和数据的目的
create table tbl_test_bak like tbl_test;
insert into tbl_test_bak select * from tbl_test; -- 以下方式也可以创建表结构,包含数据,但是没有索引约束等,所以不推荐再使用了。而且,在开启gtid情况下,会报语法错误
create table tbl_test_bak as select * from tbl_test;
*/ -- drop table if exists tbl_test ;
create table tbl_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
PRIMARY KEY (id),
KEY idx_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ; insert into tbl_test(name) values('aaa');
insert into tbl_test(name) values('bbb');
insert into tbl_test(name) values('AAA');
insert into tbl_test(name) values('BBB'); mysql> select * from tbl_test;
+----+------+
| id | name |
+----+------+
| 1 | aaa |
| 3 | AAA |
| 2 | bbb |
| 4 | BBB |
+----+------+
4 rows in set (0.00 sec) mysql> create table tbl_test_bak like tbl_test;
Query OK, 0 rows affected (0.04 sec) mysql> select * from tbl_test_bak\G;
Empty set (0.00 sec) ERROR:
No query specified mysql> create table tbl_test_bak as select * from tbl_test;
Query OK, 4 rows affected (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> show create table tbl_test_bak\G;
*************************** 1. row ***************************
Table: tbl_test_bak
Create Table: CREATE TABLE `tbl_test_bak` (
`id` bigint(20) NOT NULL DEFAULT '',
`name` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec) ERROR:
No query specified mysql> select * from tbl_test_bak;
+----+------+
| id | name |
+----+------+
| 1 | aaa |
| 3 | AAA |
| 2 | bbb |
| 4 | BBB |
+----+------+
4 rows in set (0.00 sec)

create table as 和create table like的区别的更多相关文章

  1. jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.

    jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the ...

  2. [Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table

    Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Crea ...

  3. How to Quickly Create a Copy of a Table using Transact-SQL

    The easiest way to create a copy of a table is to use a Transact-SQL command. Use SELECT INTO to ext ...

  4. 【转载】alter table move 和 alter table shrink space的区别

    move 和shrink 的共同点1.收缩段2.消除部分行迁移3.消除空间碎片4.使数据更紧密 shrink 语法:  alter table TABLE_NAME shrink space [com ...

  5. How To Convert A Partitioned Table To A Non-Partitioned Table Using DataPump In 11g (Doc ID 1276049.1)

    How To Convert A Partitioned Table To A Non-Partitioned Table Using DataPump In 11g (Doc ID 1276049. ...

  6. 通过dbcp链接池对数据库操作报 Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)--解决方案

    org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for ...

  7. How To determine DDIC Check Table, Domain and Get Table Field Text Data For Value?

     How To determineDDIC Check Table, Domain and Get Table Field Text Data For Value? 1.Get Table Fie ...

  8. HTML table表格转换为Markdown table表格[转]

    举个栗子,当我想要把这个页面的第一个表格转换成Markdown Table时,怎么做更快,效率更高? 只需简单三步,请看示例: 第一步:复制包含HTML table标签的代码 复制table代码(HT ...

  9. Bootstrap table方法,Bootstrap table事件,配置

    调用 BootStrap Table 方法的语法: $('#table').bootstrapTable('method', parameter); 例如: $('#my_table').bootst ...

随机推荐

  1. Asp.Net登陆记住用户功能实现

    1.效果和原理 原理是利用Asp.net的Cookies.选中CheckBox,把输入的用户名和密码用Cookies存储起来,设置过期时间7天,超过时间自动清除Cookie信息. 2.前台代码 < ...

  2. Swift JSON字符串和字典以及数组的互转

    1.JSONString转换为字典 // JSONString转换为字典 func getDictionaryFromJSONString(jsonString:String) ->NSDict ...

  3. 【PAT】A1034Head of a Gang

    昨天准备学完图相关的知识,但是学起来挺懵的,理解起来不难,但自己一回想,又什么都想不起来. 翻来覆去看图的遍历,还是觉得有点没到位. 所以做题来检测一下,果然学和自己做是两码事. 先看的书,又看的柳婼 ...

  4. Spring系列-SpringBoot 学习路径

    学习spring boot 已经有很长一段时间,与其说学习,不如说是使用. 在过去的很长时间我一直奉行实用主义,任何技术我都是在应用中使用,很少是因为为了学习而学习. 当然,有那么几次心血来潮,去专门 ...

  5. 【Python 09】汇率兑换2.0-2(分支语句)

    分支语句:根据判断条件选择程序执行路径 1.使用方法 if <条件1>: <语句块1> elif <条件2>: <语句块2> ... else: < ...

  6. Python开发【第一篇】基础题目一

    1.求1-2+3-4+5.....99的所有数的和 n = 1 s = 0 while n<100: temp = n%2 if temp == 0: #偶数 s = s-n else: s = ...

  7. 文本分类实战(八)—— Transformer模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  8. Spring Security(二十九):9.4.1 ExceptionTranslationFilter

    ExceptionTranslationFilter is a Spring Security filter that has responsibility for detecting any Spr ...

  9. localStorage sessionStorage 增强版

    1. 保留了localStorage sessionStorage的(setItem getItem removeItem clear key)api,使用上几乎差不多 2. 增强了setItem方法 ...

  10. Linux和Uboot下eMMC boot分区读写

    关键词:eMMC boot.PARTITION_CONFIG.force_ro等. 1. eMMC的分区 大部分eMMC都有类似如下的分区,其中BOOT.RPMB和UDA一般是默认存在的,gpp分区需 ...