Mysql FK约束失败时-哪个键?

Mysql FK约束失败时-哪个键?,mysql,foreign-keys,Mysql,Foreign Keys,只是再次碰到问题-手动检查数据并找出它,但这在很多情况下都是一个问题。情况是,FK约束失败。您知道错误的约束是什么,因为它告诉您这一点 但是,它不会告诉您哪个键未通过约束。为什么?我如何获得这些数据?我不应该通过挖掘数据来找出哪个键没有通过约束 mysql> create table books (book_id int primary key); mysql> insert into books set book_id = 22; mysql> create table a

只是再次碰到问题-手动检查数据并找出它,但这在很多情况下都是一个问题。情况是,FK约束失败。您知道错误的约束是什么,因为它告诉您这一点

但是,它不会告诉您哪个键未通过约束。为什么?我如何获得这些数据?我不应该通过挖掘数据来找出哪个键没有通过约束

mysql> create table books (book_id int primary key);
mysql> insert into books set book_id = 22;

mysql> create table authors (author_id int primary key);
mysql> insert into authors set author_id = 44;

mysql> create table book_authors (book_id int, author_id int, 
  primary key (book_id, author_id), 
  foreign key (book_id) references books (book_id), 
  foreign key (author_id) references authors (author_id));

mysql> insert into book_authors values (22, 44);
Query OK, 1 row affected (0.01 sec)
到目前为止,很好,我们演示了我们可以将两个外键插入到一个表中,如果我们使用引用表中存在的值,就可以了

现在,另一行的值不存在:

mysql> insert into book_authors values (22, 66);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
  ("test"."book_authors", CONSTRAINT "book_authors_ibfk_2" FOREIGN KEY ("author_id") 
  REFERENCES "authors" ("author_id"))

mysql> insert into book_authors values (33, 44);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails 
  ("test"."book_authors", CONSTRAINT "book_authors_ibfk_1" FOREIGN KEY ("book_id") 
  REFERENCES "books" ("book_id"))
该错误准确地告诉您哪个外键约束失败

如果您想知道失败的值,请查看innodb状态:

mysql> show engine innodb status\G
*************************** 1. row ***************************
  Type: InnoDB
  Name: 
Status: 
=====================================
2020-06-29 14:02:27 0x70000cf55000 INNODB MONITOR OUTPUT
=====================================
...
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2020-06-29 14:01:41 0x70000cf55000 Transaction:
TRANSACTION 557501, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 64, OS thread handle 123145519714304, query id 82122 localhost root update
insert into book_authors values (33, 44)
Foreign key constraint fails for table "test"."book_authors":
,
  CONSTRAINT "book_authors_ibfk_1" FOREIGN KEY ("book_id") REFERENCES "books" ("book_id")
Trying to add in child table, in index PRIMARY tuple:
DATA TUPLE: 4 fields;
 0: len 4; hex 80000021; asc    !;;
 1: len 4; hex 8000002c; asc    ,;;
 2: len 6; hex 0000000881bd; asc       ;;
 3: len 7; hex bb000001330110; asc     3  ;;

But in parent table "test"."books", in index PRIMARY,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 4; hex 80000016; asc     ;;
 1: len 6; hex 0000000881b8; asc       ;;
 2: len 7; hex b70000012f0110; asc     /  ;;

...
这很清楚地说明了这一点

如果在向已有数据的表添加外键约束时出现错误,而问题是子表中的某些值在引用的表中没有匹配值,则可以使用外部联接查找这些值:

mysql> alter table book_authors drop foreign key book_authors_ibfk_1;
mysql> alter table book_authors drop foreign key book_authors_ibfk_2;

mysql> insert into book_authors values (33, 44);
mysql> insert into book_authors values (22, 66);

mysql> select ba.* from book_authors as ba 
  left outer join books as b using (book_id) 
  where b.book_id is NULL;
+---------+-----------+
| book_id | author_id |
+---------+-----------+
|      33 |        44 |
+---------+-----------+

mysql> select ba.* from book_authors as ba 
  left outer join authors as a using (author_id) 
  where a.author_id is NULL;
+---------+-----------+
| book_id | author_id |
+---------+-----------+
|      22 |        66 |
+---------+-----------+

我认为除了错误消息中的内容之外,没有任何方法可以获得更多的细节。欢迎来到MySQL的精彩世界。有一种方法可以获得更多MySQL FK信息。但是绝对基本的调试说,切碎你的代码直到错误消失。注意:在考虑发帖之前,请先阅读您的课本和/或手册,并在谷歌上搜索任何错误消息或您的问题/问题/目标的许多清晰、简洁和准确的措辞,有无您的特定字符串/名称和网站:stackoverflow.com&tags;阅读许多答案。如果你发布一个问题,用一句话作为标题。反思你的研究。查看文本上方的投票箭头鼠标(&T)。这是否回答了您的问题?谢谢你。我知道我以前也用过类似的东西,但我完全忘记了。我仍然认为MySQL的默认错误消息应该包括消息中未通过约束的密钥,而不是深入innodb日志内部。这也意味着远程dbs上的调试变得更加复杂,其中,包括原始错误消息中的密钥,通常会提供足够的信息来解决问题。