目的:測試create table a as select * from b 與create table a like b的區(qū)別
mysql下測試:
源表:ti
表結(jié)構(gòu)如下
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語句創(chuàng)建表
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
對比源表的表結(jié)構(gòu),發(fā)現(xiàn)KEY `new_msg_flag` (`new_msg_flag`)沒有被創(chuàng)建
b 使用like子句創(chuàng)建表
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
對比源表的表結(jié)構(gòu),兩者完全一致,完整的包含了表結(jié)構(gòu)和索引
結(jié)論:mysql下create table a as select * from b形式創(chuàng)建的表不包含索引信息,like子句形式包含完整表結(jié)構(gòu)和索引信息
所以 as select 子句一般適用于建表并復(fù)制源表數(shù)據(jù)的情況,like子句適用于只復(fù)制表結(jié)構(gòu)的情況
誤用的風(fēng)險: 索引的缺失對于業(yè)務(wù)的性能是致命的,不必多說.
Oracle下:
a create as select同樣不會創(chuàng)建索引
b oracle不支持like子句
至于如何實現(xiàn)完全創(chuàng)建表結(jié)構(gòu)和索引的方法?現(xiàn)在還沒有
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。