`

create table tb as select 和create table tb like的区别(转载)

 
阅读更多

目的:测试create table a as select * from b 与create table a  like b的区别

mysql下测试:

源表:ti

表结构如下

root:test> show create table ti\G
*************************** 1. row ***************************
       Table: ti
Create Table: CREATE TABLE `ti` (
  `id` int(11) DEFAULT NULL,
  `amount` decimal(7,2) DEFAULT NULL,
  `m_photo_big` varchar(64) DEFAULT NULL,
  `tr_date` date DEFAULT NULL,
  `new_msg_flag` tinyint(4) NOT NULL DEFAULT '0',
  `love_listreq` int(3) DEFAULT '1',
  `love_listconfig` int(3) DEFAULT '1',
  KEY `new_msg_flag` (`new_msg_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

a 使用create as select语句创建表

root:test> create table ti2 as select * from ti limit 0;
Query OK, 0 rows affected (0.00 sec)

sroot:test> how create table ti2 ;
----------------------------
CREATE TABLE `ti2` (
  `id` int(11) DEFAULT NULL,
  `amount` decimal(7,2) DEFAULT NULL,
  `m_photo_big` varchar(64) DEFAULT NULL,
  `tr_date` date DEFAULT NULL,
  `new_msg_flag` tinyint(4) NOT NULL DEFAULT '0',
  `love_listreq` int(3) DEFAULT '1',
  `love_listconfig` int(3) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

对比源表的表结构,发现KEY `new_msg_flag` (`new_msg_flag`)没有被创建


b 使用like子句创建表

root:test> create table ti1 like ti;
Query OK, 0 rows affected (0.06 sec)

root:test> show create table ti1;  
----------------------------------------
CREATE TABLE `ti1` (
  `id` int(11) DEFAULT NULL,
  `amount` decimal(7,2) DEFAULT NULL,
  `m_photo_big` varchar(64) DEFAULT NULL,
  `tr_date` date DEFAULT NULL,
  `new_msg_flag` tinyint(4) NOT NULL DEFAULT '0',
  `love_listreq` int(3) DEFAULT '1',
  `love_listconfig` int(3) DEFAULT '1',
  KEY `new_msg_flag` (`new_msg_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

对比源表的表结构,两者完全一致,完整的包含了表结构和索引

结论:mysql下create table a as select * from b形式创建的表不包含索引信息,like子句形式包含完整表结构和索引信息

所以 as select 子句一般适用于建表并复制源表数据的情况,like子句适用于只复制表结构的情况

误用的风险: 索引的缺失对于业务的性能是致命的,不必多说.

Oracle下:

a create as select同样不会创建索引

b oracle不支持like子句

至于如何实现完全创建表结构和索引的方法有待继续探讨!

GAME OVER

分享到:
评论

相关推荐

    超实用sql语句

    B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2...

    经典全面的SQL语句大全

    B:create table tab_new as select col1,col2… from tab_old definition only C: select * into table2 from table  5、说明:  删除新表:drop table tabname  6、说明:  增加一个列:Alter table tabname ...

    经典SQL语句大全

    B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2...

    数据库操作语句大全(sql)

    B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2...

    sql经典语句一部分

    B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2...

    精髓Oralcle讲课笔记

    sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unlock; --那么这个用户名就能使用了。 --(默认全局数据库名orcl) 1、select ename, sal * 12 from emp; --计算...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    1. Create table命令 用于创建表。在创建表时,经常会创建该表的主键、外键、唯一约束、Check约束等  语法结构 create table 表名( [字段名] [类型] [约束] ……….. CONSTRAINT fk_column FOREIGN KEY(column1...

    sqlserver存储过程

    select * from Users where UserName like @UserName and UserName like @nextName; go exec proc_findUsersByName; exec proc_findUsersByName '%l%', 'j%'; -------------------------------------------------...

    xls转mdb代码以及.exe执行软件

    SELECT cast(cast(科目编号 as numeric(10,2)) as nvarchar(255))+' ' 转换后的别名 FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 'Data Source="c:\test.xls";User ID=Admin;Password=;Extended properties=...

    Absolute Database for D7

    DROP TABLE, ALTER TABLE statements CREATE INDEX, DROP INDEX statements INSERT, UPDATE, DELETE statements BETWEEN, IN, LIKE, IS NULL, EXISTS operators Aggregate functions COUNT,SUM,MIN,MAX,AVG Most of...

    qemu-0.13.0(编译过全处理器支持)

    create a new Network Interface Card and connect it to VLAN 'n' -net user[,vlan=n][,name=str][,net=addr[/mask]][,host=addr][,restrict=y|n] [,hostname=host][,dhcpstart=addr][,dns=addr][,tftp=dir][,...

    Qemu-1.0.1 for windows

    -acpitable [sig=str][,rev=n][,oem_id=str][,oem_table_id=str][,oem_rev=n][,asl_compiler_id=str][,asl_compiler_rev=n][,{data|file}=file1[:file2]...] ACPI table description -smbios file=binary load ...

    springmybatis

    Create TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ...

Global site tag (gtag.js) - Google Analytics