gpt4 book ai didi

MySql工作台: add Foreign key error

转载 作者:行者123 更新时间:2023-11-29 17:57:16 24 4
gpt4 key购买 nike

我试图在教程表中添加外键“USERNAME”,但出现错误。

Executing:
ALTER TABLE `databse`.`tutorial`
ADD CONSTRAINT `USERNAME`
FOREIGN KEY (`USERNAME`)
REFERENCES `databse`.`register` (`USERNAME`)
ON DELETE CASCADE
ON UPDATE CASCADE;

Operation failed: There was an error while applying the SQL script to the database.
ERROR 1452: Cannot add or update a child row:
a foreign key constraint fails (`databse`.`#sql-e7c_5`, CONSTRAINT `USERNAME` FOREIGN KEY (`USERNAME`)
REFERENCES `register` (`USERNAME`) ON DELETE CASCADE ON UPDATE CASCADE)
SQL Statement:
ALTER TABLE `databse`.`tutorial`
ADD CONSTRAINT `USERNAME`
FOREIGN KEY (`USERNAME`)
REFERENCES `databse`.`register` (`USERNAME`)
ON DELETE CASCADE
ON UPDATE CASCADE

外键设置: enter image description here

教程表设置: enter image description here

有什么想法吗?谢谢你

我解决了这个问题,我创建了一个新的“教程”表替换“教程”表,并使用相同的方式添加外键,它起作用了! = =

还是谢谢你的帮助!!

最佳答案

如上所述here :

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.

在您显示的代码中:您尝试引用“USERNAME”列,这不是上次捕获中的主键

因此,您可以将 register 表中的主键更改为 USERNAME,也可以将外键更改为引用 TutorialName

关于MySql工作台: add Foreign key error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48689316/

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