gpt4 book ai didi

mysql - 无法向现有表添加外键

转载 作者:行者123 更新时间:2023-11-29 05:25:20 26 4
gpt4 key购买 nike

我无法将外键添加到现有表中。我想添加外键 publisher_id 从表 games 引用到 publishers (publisher_id)。

但是当运行下面的 sql 时,它说:

Source and target columns must have the same data type, there must be an index on the target columns and referenced data must exist. Cannot add or update a child row: a foreign key constraint fails (vngarena.com.#sql-818_1b1, CONSTRAINT #sql-818_1b1_ibfk_2 FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE ON UPDATE CASCADE

sql是:

ALTER TABLE games
ADD FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE ON UPDATE CASCADE;

这是我的数据库的图表:

enter image description here

更多信息:
- 表 games 有两条记录
- 表 games 有其他外键引用表 gamecats (gamecat_id)
- 表 gamecats 有两条记录
- 表 publishers 没有记录

最佳答案

如果有任何违反关系的现有数据,您需要在添加 key 之前将其清理。

[已编辑]

编辑后:Table games 中的 publisher_id 正在 publishers 表中查找匹配项。添加一个发布者,其 ID 与游戏表中的内容相匹配。

关于mysql - 无法向现有表添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com