所以,我试图完成一个查询,根据属性值删除行。这样的事情。
DELETE FROM SPORTING_CLUBS
WHERE state = 'NY';
但是,SPORTING_CLUBS表(CLUB_ID)的主键是两个子表CLUB_ACTIVITY和CLUB_MEMBERSHIP的外键/父键。我在使用SPORTING_CLUBS表中的删除操作完成之前成功删除两个子表中的行时需要遵循的逻辑有问题。
答案 0 :(得分:1)
如果您无法更改FOREIGN KEY约束以添加ON CASCADE DELETE
子句,则需要在从主表中删除之前从子表中删除记录。我建议您使用该事务来保持数据完整性
BEGIN TRANSACTION;
DELETE FROM CLUB_ACTIVITY
WHERE CLUB_ID IN (
SELECT CLUB_ID FROM SPORTING_CLUBS
WHERE state = 'NY');
DELETE FROM CLUB_MEMBERSHIP
WHERE CLUB_ID IN (
SELECT CLUB_ID FROM SPORTING_CLUBS
WHERE state = 'NY');
DELETE FROM SPORTING_CLUBS
WHERE state = 'NY';
COMMIT;