table2 に対して最初の INSERT が実行されるのはなぜですか。table2.col_1 は NOT NULL であることに注意してください。col_1 に NULL を挿入しませんが、不思議なことに NULL 値を空の文字列に変換します。MySQL バージョン 5.5.28 を使用しています。ありがとう
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)