我正在做一些测试,我和带有外键的乐队和专辑表有关。 我尝试用外键删除一行,但这返回了父行错误 我的代码:
DELETE FROM BANDS WHERE idBand = '13 ';
DELETE FROM albums WHERE albumId = '13 ';
albumId是idBand外键
那么,如何删除带条件的外键行?
答案 0 :(得分:2)
您必须先从相册表中删除。这是父表,因此所有子行必须先行。只需颠倒删除的顺序即可。我假设你的专辑表是乐队表的孩子
删除此频段的所有专辑
delete from albums where bandid = 13
现在删除乐队
delete from bands where bandid = 13
现在你有一个儿童的歌曲表,所以如何删除所有这些?您必须删除该乐队所有专辑的所有歌曲。现在必须先完成此操作才能删除相册
delete from Songs where albumid in (select albumid from albums where bandid = 13)
答案 1 :(得分:0)
我认为OP基于他的评论输入了错误的SQL?
DELETE FROM albums WHERE albumId='13'
应该是
DELETE FROM albums WHERE bandId='13'