56,912
社区成员




create table t2 (
-> id int primary key,
-> tid int,
-> FOREIGN KEY (tid) REFERENCES t1(id)
-> )engine=innodb;
mysql> create table t1 (
-> id int primary key,
-> col int
-> )engine=innodb;
Query OK, 0 rows affected (0.09 sec)
mysql> create table t2 (
-> id int primary key,
-> tid int,
-> FOREIGN KEY (tid) REFERENCES t1(id)
-> )engine=innodb;
Query OK, 0 rows affected (0.09 sec)
mysql> insert into t2 values (2,1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f
ails (`csdn`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `t1` (`
id`))
mysql> insert into t2 values (1,null);
Query OK, 1 row affected (0.05 sec)
mysql> insert into t1 values (1,91);
Query OK, 1 row affected (0.06 sec)
mysql> insert into t2 values (2,1);
Query OK, 1 row affected (0.03 sec)
mysql> select * from t2;
+----+------+
| id | tid |
+----+------+
| 1 | NULL |
| 2 | 1 |
+----+------+
2 rows in set (0.00 sec)
mysql> select version();
+----------------------+
| version() |
+----------------------+
| 5.1.33-community-log |
+----------------------+
1 row in set (0.00 sec)
mysql>