简体   繁体   English

MySQL错误1452

[英]MySQL error 1452

Here's the script 这是剧本

    ALTER TABLE `candycorn`.`bb_users` 
    ADD CONSTRAINT `pf_minecraftusername`
    FOREIGN KEY (`pf_minecraftusername`)
    REFERENCES `candycorn`.`bb_profile_fields_data` (`pf_minecraftusername`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;

and the error description 和错误说明

    ERROR 1452: Cannot add or update a child row: a foreign key constraint fails  
    (`candycorn`.<result 2 when explaining filename '#sql-4e4_1785d'>, CONSTRAINT
    `pf_minecraftusername` FOREIGN KEY (`pf_minecraftusername`) REFERENCES 
    `bb_profile_fields_data` (`pf_minecraftusern)

if somebody could, would you explain what I'm missing out here? 如果有人可以,您能解释一下我在这里错过了什么吗?

The constraint that you are trying to add isn't satisfied by some data already in the tables. 表中已有的某些数据无法满足您要添加的约束。 This can be because a value in bb_users table isn't found in the corresponding column ( pf_minecraftusername ) in bb_profile_fields_data . 这可能是因为在bb_users的相应列( pf_minecraftusername )中bb_profile_fields_data bb_users表中的值。

If you have to add the constraint to tables with data already in them, you have to clean up the tables by hand first. 如果必须将约束添加到已经有数据的表中,则必须首先手动清理表。 Alternatively, you can empty the tables (with truncate, or by making the tables afresh after doing a " drop database "), then add the constraint, and then run whatever scripts you have to put data in the tables. 或者,您可以清空表(使用截断或在执行“ drop database ”后重新创建表),然后添加约束,然后运行将表中的数据放入脚本中的任何脚本。

Of course, if this is a production system, you'll need to do more complex data fixing before you can successfully add the constraint. 当然,如果这是生产系统,则需要先进行更复杂的数据修复,然后才能成功添加约束。

Finally, I suggest that you make a dummy copy of the database with no data at all and add the constraint there, just to check that the constraint is properly specified. 最后,我建议您制作一个完全没有数据的数据库虚拟副本,并在其中添加约束,以检查是否正确指定了约束。 You don't want to be barking up the wrong tree. 您不想被错误的树缠住。

ALTER TABLE tablename with NOCHECK ADD CONSTRAINT [FK_1] FOREIGN KEY ([ Column name ]) REFERENCES restaurants([ column name ]) 带有NOCHECK ADD CONSTRAINT [FK_1] FOREIGN KEY([[ Column name ]])的ALTER TABLE tablename Column name引用餐厅([ column name ])

It seems data is already present in table.. So You need to modify table using above query.. No need to recreate table nd data 似乎数据已经存在于表中。因此您需要使用上述查询来修改表。而无需重新创建表nd数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM