mysql添加外键失败:sql 1452 Cannot add or update a child row:a foreign key constraint fails

做数据库作业时,我对已存在的两个表中的一个表添加另一个表的主键为外键,遇到以下错误:

sql 1452 Cannot add or update a child row:a foreign key constraint fails的错误。
mysql添加外键失败:sql 1452 Cannot add or update a child row:a foreign key constraint fails
关联的两个表的字段分别如下:(即在score01表中添加courseno为关于course01表的外键约束)
mysql添加外键失败:sql 1452 Cannot add or update a child row:a foreign key constraint fails
原因:设置的外键和对应的另一个表的主键值不匹配。

解决方法:找出不匹配的值修改或者清空两表数据。
mysql添加外键失败:sql 1452 Cannot add or update a child row:a foreign key constraint fails

最后我们在试一下添加外键,可以看到添加外键成功了。
mysql添加外键失败:sql 1452 Cannot add or update a child row:a foreign key constraint fails