[英]Error in adding Foreign key using Mysql workbench
Im adding foreign key to my table 'empcompensation' referring from another table 'employees'. 我将外键添加到我的表“ empcompensation”中,引用另一个表“ employees”。 It shows error.
它显示错误。
ALTER TABLE `simhrmdb`.`empcompensation`
ADD CONSTRAINT `Fk_employee`
FOREIGN KEY (`employee`)
REFERENCES `simhrmdb`.`employees` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
Operation failed: There was an error while applying the SQL script to the database.
操作失败:将SQL脚本应用于数据库时出错。
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails错误1452:无法添加或更新子行:外键约束失败
(`simhrmdb`.`#sql-c5c_6`, CONSTRAINT `Fk_employee` FOREIGN KEY (`employee`) REFERENCES `employees` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) SQL Statement: ALTER TABLE `simhrmdb`.`empcompensation` ADD CONSTRAINT `Fk_employee` FOREIGN KEY (`employee`) REFERENCES `simhrmdb`.`employees` (`id`) ON DELETE CASCADE ON UPDATE CASCADE(`simhrmdb`.`#sql-c5c_6`,CONSTRAINT`Fk_employee`外键(`employee`)参考`employees`(`id`)在更新级联上删除级联)SQL语句:ALTER TABLE`simhrmdb`.empempensensation `添加约束`Fk_employee`外键(`employee`)引用`simhrmdb`.`employees`(`id`)在更新级联上删除级联
i added the same 'id' from 'employees' table to few others table before and it worked perfectly. 我之前从'employees'表中添加了相同的'id'到其他几个表中,并且效果很好。 But now it creates error.
但是现在它会产生错误。 Empcompensation table is the new table that i added.
Empcompensation表是我添加的新表。
In that error log, i can see some letters #sql-c5c_6 . 在该错误日志中,我可以看到一些字母#sql-c5c_6 。 I dont understand this .
我不明白这一点。
This says that some of the values in the employee
column cannot be found in the employees
table. 这表示在
employees
表中找不到employee
列中的某些值。 Find out what they are by using a LEFT JOIN 使用LEFT JOIN找出它们是什么
SELECT employee FROM empcompensation
LEFT JOIN employees on employee = employees.id WHERE employees.id IS NULL
You will then have to figure out what to do with them (delete them, create corresponding entries in the employees table etc) 然后,您必须弄清楚如何处理它们(删除它们,在employees表中创建相应的条目,等等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.