免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 12085 | 回复: 1
打印 上一主题 下一主题

关于CREATE TABLE IF NOT EXIST … SELECT [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-12 09:24 |只看该作者 |倒序浏览
对于CREATE TABLE  IF NOT EXIST… SELECT ,如果给定IF NOT EXISTS ,而且表已经存在的情况下
做下列事
CREATE TABLE 的表定义部分被忽略,甚至表定义与目前存在的表不一致,也不会抛出错误
如果存在的表的列数和由SELECT部分的列数不匹配的话 SELECT到的值会被靠右填充到表列中
如:
SELECT产生m列,而表定义有n列,当(mn时,会有错误产生

mysql> create table t1 (a int,b int);
mysql> insert int t1 values (1,1),(1,2);
mysql> drop table t4;
mysql> create table t4 (a int,b int,c int,d int);
mysql> insert into t4 values (1,1,1,1);
mysql> create table IF NOT EXISTS t4 (a int) SELECT * FROM t1;
uery OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 1
mysql> show warnings;
+——-+——+—————————+
| Level | Code | Message |
+——-+——+—————————+
| Note | 1050 | Table ‘t4′ already exists |
+——-+——+—————————+
mysql> select * from t4
-> ;
+——+——+——+——+
| a | b | c | d |
+——+——+——+——+
| 1 | 1 | 1 | 1 |
| NULL | NULL | 1 | 1 |
| NULL | NULL | 1 | 2 |
+——+——+——+——+

mysql> drop table t4;
mysql> create table t4 (a int);
mysql> insert into t4 values(111);
mysql> select * from t4;
+——+
| a |
+——+
| 111 |
+——+
mysql> create table IF NOT EXISTS t4 (a int,b int,c int) SELECT * FROM t1;
ERROR 1136 (21S01): Column count doesn’t match value count at row 1

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
2 [报告]
发表于 2010-03-13 10:12 |只看该作者
mysql官方给出的解释是:
CREATE TABLE  IF NOT EXIST… SELECT的行为,先判断表是否存在,
如果存在,语句就相当于执行insert into select
如果不存在,则相当于create table ... select

create table ... select 的行为又如下:
For CREATE TABLE … SELECT, if IF NOT EXISTS is given and the table already exists, MySQL handles the statement as follows:
* The table definition given in the CREATE TABLE part is ignored. No error occurs, even if the definition does not match that of the existing table.
* If there is a mismatch between the number of columns in the table and the number of columns produced by the SELECT part, the selected values are assigned to the rightmost columns. For example, if the table contains n columns and the SELECT produces m columns, where m < n, the selected values are assigned to the m rightmost columns in the table. Each of the initial n – m columns is assigned its default value, either that specified explicitly in the column definition or the implicit column data type default if the definition contains no default. If the SELECT part produces too many columns (m > n), an error occurs.
* If strict SQL mode is enabled and any of these initial columns do not have an explicit default value, the statement fails with an error.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP