`
wangshaofei
  • 浏览: 272852 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

阅读更多

目的:测试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子句

 

权限不会随like一起复制

原表 test.test

原表table级别权限:

root:mysql> select * from tables_priv where Db='test' and Table_name='test';
+-------------+------+-------+------------+----------------+---------------------+-----------------------------+-------------+
| Host        | Db   | User  | Table_name | Grantor        | Timestamp           | Table_priv                  | Column_priv |
+-------------+------+-------+------------+----------------+---------------------+-----------------------------+-------------+
| 192.168.%.% | test | root  | test       | root@localhost | 2009-09-17 11:22:25 | Select,Insert,Update,Delete |             | 
| 192.168.%.% | test | lidan | test       | root@localhost | 2009-09-17 11:23:58 | Select,Insert,Update,Delete |             | 
+-------------+------+-------+------------+----------------+---------------------+-----------------------------+-------------+
2 rows in set (0.00 sec)

复制表:

root:test> create table pri_test like test;
Query OK, 0 rows affected (0.00 sec)

pri_test的权限
root:mysql> select * from tables_priv where Db='test' and Table_name='pri_test';
Empty set (0.00 sec)

可见使用like不可能保持原表和复制表的权限一致



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

分享到:
评论

相关推荐

    MySQL中create table as 与like的区别分析

    本文分析了MySQL中create table as 与like的区别。分享给大家供大家参考,具体如下: 对于mysql的复制相同表结构方法,有create table as 和create table like 两种,区别是什么呢? 代码如下:create table t2 as ...

    MySQL中表复制:create table like 与 create table as select

    主要介绍了MySQL中表复制:create table like 与 create table as select,需要的朋友可以参考下

    超实用sql语句

    select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 6、说明:外连接查询(表名1:a 表名2:b) select a.a, a.b, a.c, b.c, b.d, b.f from a ...

    经典全面的SQL语句大全

    select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 6、说明:外连接查询(表名1:a 表名2:b) select a.a, a.b, a.c, b.c, b.d, b.f from a ...

    oracle复制表结构和复制表数据语句分享

    1. 复制表结构及其数据: 代码如下:create table table_name_new as select * from table_name_old2. 只复制表结构: 代码如下:create table table_name_new as select * from table_name_old where 1=2;或者: 代码...

    mysql中复制表结构的方法小结

    CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2 或者 CREATE TABLE 新表 LIKE 旧表 2.复制表结构及数据到新表 CREATE TABLE 新表 SELECT * FROM 旧表 3.复制旧表的数据到新表(假设两个表结构一样)  ...

    MySQL复制表结构和内容到另一张表中的SQL语句

    CREATE TABLE 新表 SELECT * FROM 旧表 2.只复制表结构到新表 代码如下: CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2 即:让WHERE条件不成立. 方法二:(低版本的mysql不支持,mysql4.0.25 不支持,mysql5已经...

    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 注:列增加后将不能删除。...

    经典SQL语句大全

    select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 6、说明:外连接查询(表名1:a 表名2:b) select a.a, a.b, a.c, b.c, b.d, b.f from a ...

    经典Sql语句 数据库

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

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

    select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 6、说明:外连接查询(表名1:a 表名2:b) select a.a, a.b, a.c, b.c, b.d, b.f from a...

    sql经典语句一部分

    select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 6、说明:外连接查询(表名1:a 表名2:b) select a.a, a.b, a.c, b.c, b.d, b.f from a ...

    某知名公司内部ORACLE培训资料(如果你看后觉得不行,可以损我,人格担保)

    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...

    T-SQL高级查询

    select * from student where name like '%[a][o]%'; select * from student where name not like '%a%'; select * from student where name like 'ja%'; select * from student where name not like '%[j,n]%'; ...

    Oracle事例

    create snapshot snapshot_to_study as select * from TABLE_NAME@to_study; 创建角色 create role aa identified by aaa; 授权 grant create snapshot,alter snapshot to aaa; grant aaa to emp; create ...

    数据库表ERP表参考。仅供参考

    --if exists(select * from sysobjects where name='视图名') -- drop view 视图名 --go --create view 视图名 --as --select 字段名 from 表名 [条件] --go --主外健约束语句没有执行 use T90ERP go --********...

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

    '',A表) select * from 数据库名..B表 /*************导入Access********************/ insert into B表 selet * from openrowset('Microsoft.Jet.OLEDB.4.0', 'x:\A.mdb';'admin';'',A表) 文件名为参数 declare ...

    MySQL复制表的三种方式(小结)

    create table table_name_new as (select * from table_name_old); 只复制表结构 create table table_name_new as select * from table_name_old where 1=2; 或者 create table table_name_new like table_name...

Global site tag (gtag.js) - Google Analytics