爲什麼第一個INSERT要經過table2。請注意,table2.col_1不是NULL。它不會爲col_1插入NULL,但會神奇地將NULL值轉換爲空字符串。我正在使用MySQL版本5.5.28。謝謝MySQL在NOT NULL列中插入帶有NULL值的記錄
mysql> DROP TABLE IF EXISTS table1, table2;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS table1 (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
-> col_1 VARCHAR(45) NOT NULL ,
-> col_2 VARCHAR(45) NOT NULL ,
-> PRIMARY KEY (`id`))
-> ENGINE = InnoDB;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE table2 LIKE table1;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO table1 (id, col_1, col_2) VALUES (NULL, "xxx","yyy");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO table2 (id, col_1, col_2) SELECT NULL, NULL, col_2 FROM table1 WHERE id=1;
Query OK, 1 row affected, 1 warning (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 1
mysql> SHOW WARNINGS;
+---------+------+-------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------+
| Warning | 1048 | Column 'col_1' cannot be null |
+---------+------+-------------------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM table2;
+----+-------+-------+
| id | col_1 | col_2 |
+----+-------+-------+
| 1 | | yyy |
+----+-------+-------+
1 row in set (0.00 sec)
mysql> INSERT INTO table2 (id, col_1, col_2) VALUES(NULL, NULL, "zzz");
ERROR 1048 (23000): Column 'col_1' cannot be null
mysql> SELECT * FROM table2;
+----+-------+-------+
| id | col_1 | col_2 |
+----+-------+-------+
| 1 | | yyy |
+----+-------+-------+
1 row in set (0.00 sec)
也許它插入'NULL'字符串,而不是空(*不*) – 2013-04-09 15:00:08
奇怪了,我做的是相同的,我得到了插入空...也許你可以在綁定值時指定PDO :: PARAM_INT? – Sebas 2013-04-09 15:05:58
@JW請解釋一下。我只是編輯了原始帖子,以顯示如果插入由SELECT提供,它只插入一行。 – user1032531 2013-04-09 15:06:02