[英]alter table add foreign key fails
I have 3 tables and they all have innodb engine: 我有3张桌子,他们都有innodb引擎:
video(url, title, desc, country,...) url -> primary key
videoCat(_url, category) {_url,category} -> primary key
favorite(fav_url, thumb_path) fav_url -> primary key
then I do: 然后我做:
alter table favorite
add foreign key(fav_url) references video(url)
on delete cascade
and everything goes smooth, but when I try: 一切顺利,但当我尝试:
alter table videoCat
add foreign key(_url) references video(url)
on delete cascade
I get: 我明白了:
1452 - Cannot add or update a child row: a foreign key constraint fails (
bascelik_lookaroundyou
.<result 2 when explaining filename '#sql-efa_1a6e91a'>, CONSTRAINT#sql-efa_1a6e91a_ibfk_1
FOREIGN KEY (_url
) REFERENCESvideo
(url
) ON DELETE CASCADE) 1452 - 无法添加或更新子行:外键约束失败(bascelik_lookaroundyou
。<结果2解释文件名'#sql-efa_1a6e91a'>时,CONSTRAINT#sql-efa_1a6e91a_ibfk_1
FOREIGN KEY(_url
)REFERENCESvideo
(url
)ON DELETE CASCADE )
why??? 为什么???
ps I am using phpmyadmin ver. ps我正在使用phpmyadmin ver。 3.3.9.2 3.3.9.2
The table videoCat has one or more rows that violates the foreign key constraint. 表videoCat有一行或多行违反外键约束。 This is usually that you have a row with a value for _url that does not exist in the table video. 通常情况下,您有一个行,其中_url的值在表视频中不存在。
You can check for this with the following query: 您可以使用以下查询检查:
SELECT videoCat._url
FROM videoCat LEFT JOIN video ON videoCat._url = video.url
WHERE video.url IS NULL
EDIT 编辑
Per request, here's a query to delete those pesky rows: 每个请求,这是一个删除那些讨厌的行的查询:
DELETE FROM videoCat
WHERE NOT EXISTS (
SELECT *
FROM video
WHERE url = videoCat._url
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.