Another quiz based on comments from aka fenixshadow:
Two identical tables with FOREIGN KEY constraints within the
tables are created:
CREATE TABLE t (
id INT NOT NULL PRIMARY KEY,
type INT,
pid INT,
FOREIGN KEY (pid) REFERENCES t (id)
) ENGINE=INNODB;
CREATE TABLE t1 (
id INT NOT NULL PRIMARY KEY,
type INT,
pid INT,
FOREIGN KEY (pid) REFERENCES t1 (id)
) ENGINE=INNODB;
The tables are created, and then the following updates are done
in table t:
INSERT INTO t VALUES (1,1, NULL);
INSERT INTO t VALUES (2,1, NULL);
UPDATE t SET pid = 2 WHERE id = 1;
Satisfied that all the updates have completed successfully, we
attempt to add the data in t to t1:
INSERT INTO t1 SELECT * from t;
But are greeted with:
ERROR 1452 : Cannot add or update a child row: a foreign key
constraint fails
Question 1: Why did …
[Read more]