更改 mySQL 表以添加外键时出现错误 #1452

发布于 2024-10-29 00:54:46 字数 1531 浏览 9 评论 0原文

试图帮助实习生完成她的项目。她想将外键添加到现有表中,但此查询:

ALTER TABLE `document` 
  ADD CONSTRAINT `document_ibfk_1` FOREIGN KEY (`cle_author`) 
  REFERENCES `author` (`id_author`) 
  ON DELETE CASCADE 
  ON UPDATE CASCADE;

给出此错误:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`wrc_mysql`.<result 2 when explaining filename '#sql-30e4_7000d'>, CONSTRAINT `document_ibfk_1` FOREIGN KEY (`cle_author`) REFERENCES `author` (`id_author`) ON DELETE CASCADE ON UPDATE CASCADE)

架构就像这样

CREATE TABLE `document` (
  `id_document` int(11) NOT NULL AUTO_INCREMENT,
  `abstract` text,
  `number_of_pages` int(10) DEFAULT NULL,
  `original_surrey_citation` varchar(255) DEFAULT NULL,
  `updated_citation` varchar(255) DEFAULT NULL,
  `library_of_congress` varchar(10) DEFAULT NULL,
  `cross_citation` varchar(50) DEFAULT NULL,
  `doc_type` varchar(255) DEFAULT NULL,
  `questions` varchar(255) DEFAULT NULL,
  `keywords` varchar(255) DEFAULT NULL,
  `cle_author` int(10) NOT NULL,
  PRIMARY KEY (`id_document`),
  KEY `cle_author` (`cle_author`)
) ENGINE=InnoDB AUTO_INCREMENT=22591 DEFAULT CHARSET=utf8

CREATE TABLE `author` (
  `id_author` int(10) NOT NULL AUTO_INCREMENT,
  `author_name` varchar(255) DEFAULT NULL,
  `sender_office` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id_author`),
  KEY `author_name` (`author_name`,`sender_office`)
) ENGINE=InnoDB AUTO_INCREMENT=22591 DEFAULT CHARSET=utf8

有人知道出了什么问题吗?

Trying to help an intern with her project. She wants to add foreign keys to an existing table but this query:

ALTER TABLE `document` 
  ADD CONSTRAINT `document_ibfk_1` FOREIGN KEY (`cle_author`) 
  REFERENCES `author` (`id_author`) 
  ON DELETE CASCADE 
  ON UPDATE CASCADE;

gives this error:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`wrc_mysql`.<result 2 when explaining filename '#sql-30e4_7000d'>, CONSTRAINT `document_ibfk_1` FOREIGN KEY (`cle_author`) REFERENCES `author` (`id_author`) ON DELETE CASCADE ON UPDATE CASCADE)

Schema are like so

CREATE TABLE `document` (
  `id_document` int(11) NOT NULL AUTO_INCREMENT,
  `abstract` text,
  `number_of_pages` int(10) DEFAULT NULL,
  `original_surrey_citation` varchar(255) DEFAULT NULL,
  `updated_citation` varchar(255) DEFAULT NULL,
  `library_of_congress` varchar(10) DEFAULT NULL,
  `cross_citation` varchar(50) DEFAULT NULL,
  `doc_type` varchar(255) DEFAULT NULL,
  `questions` varchar(255) DEFAULT NULL,
  `keywords` varchar(255) DEFAULT NULL,
  `cle_author` int(10) NOT NULL,
  PRIMARY KEY (`id_document`),
  KEY `cle_author` (`cle_author`)
) ENGINE=InnoDB AUTO_INCREMENT=22591 DEFAULT CHARSET=utf8

CREATE TABLE `author` (
  `id_author` int(10) NOT NULL AUTO_INCREMENT,
  `author_name` varchar(255) DEFAULT NULL,
  `sender_office` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id_author`),
  KEY `author_name` (`author_name`,`sender_office`)
) ENGINE=InnoDB AUTO_INCREMENT=22591 DEFAULT CHARSET=utf8

Anyone know what is going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-11-05 00:54:46

您的两个表之间的数据可能不一致。此错误意味着您的 document 表中有一个 cle_author 值,但在 author 表中没有相应的条目。由于 cle_author 值将设置为外键,因此该字段的每个值都必须在 author 表的 id_author 中具有相应的条目> 领域。

You probably have inconsistent data between your two tables. This error means that you have a cle_author value in your document table that doesn't have a corresponding entry in the author table. Since the cle_author value is going to be set up as a foreign key, each value for that field must have a corresponding entry in the author table's id_author field.

绮烟 2024-11-05 00:54:46

根据此页面: Mysql 错误 1452 - 无法添加或更新子行:外键约束失败

检查实习生的数据

使用SELECT cle_author FROM 文档 doc
LEFT JOIN 作者 a ON doc.cle_author=a.id_author
WHERE a.id_author 为 NULL;

并发现她的所有 cle_author 数据都是伪造的,并且不包含对 id_author 值的有效引用。

Per this page: Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails

checked intern's data with

SELECT cle_author FROM document doc
LEFT JOIN author a ON doc.cle_author=a.id_author
WHERE a.id_author IS NULL;

And found ALL of her cle_author data is bogus and does not hold valid references to id_author values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文