create table tb as select 和create table tb like的区别
来源:互联网 发布:算法的成就 编辑:程序博客网 时间:2024/05/29 03:14
目的:测试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
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
- create table tb as select 和create table tb like的区别
- Mysql create table tb as select 和create table tb like的区别
- postgres create table as select & create table like
- MySQL create table as 与 like区别
- Create table as select
- create table as select
- create table like 和create table select 比较
- create table like 和create table select 比较
- create table like 和create table select 比较
- MySQL的create table as 与 like区别
- MySQL的create table as 与 like区别
- MySQL的create table as 与 like区别
- insert table 和create table as 区别
- create table as select 和 create table 再insert into select 的差异
- SELECT INTO , INSERT INTO SELECT 和 CREATE TABLE AS SELECT 的区别
- mysql复制表结构create table as和like的区别
- MySQL中表复制:create table like 与 create table as select
- oracle create table as select
- 数理逻辑:谓词演算(9)进一步的形式定理
- 为什么字符型数组可以整体输出,而整形数组不可以
- 我的网络流sap,isap,dinic三种方法的对比总结
- DRY原则和设计模式
- Android面试宝典
- create table tb as select 和create table tb like的区别
- vs版本的选择
- jsp发送中文请求乱码解决方式
- Three20之TTNibDemo工程中自动提示不起作用【Xcode不会自动提示怎么解决?】
- LinkButton 传递多个参数
- sql中怎样把同一张表中相同字段的内容合并为一条记录(不同字段的那一列每个记录后面加逗号)
- presentModalViewController设置动画效果
- tmpfs
- vs2008中如何在项目属性中切换“图形形式”和“控制台形式”——即调出或消除黑窗口