- 论坛徽章:
- 0
|
对于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 |
|