两个表,通过外键关联。现在想要删除顶级表,但在操作前需要判定在此记录下有没有通过外键关联的记录,如果有,先删除下层表中对应的记录。。想要通过触发器完成,以下是代码:
CREATE TRIGGER `aa` BEFORE DELETE ON `shop_class_second`
FOR EACH ROW begin
delete from shop_class_third where cla_thr_cla_sec_id=
end;shop_class_second 是项级表,shop_class_third是下层表。但是不知道怎么样传入外键的值,求指教。
CREATE TRIGGER `aa` BEFORE DELETE ON `shop_class_second`
FOR EACH ROW begin
delete from shop_class_third where cla_thr_cla_sec_id=
end;shop_class_second 是项级表,shop_class_third是下层表。但是不知道怎么样传入外键的值,求指教。
FOR EACH ROW
begin
CREATE TEMPORARY TABLE temp1 select id from old;
delete from shop_class_third where cla_thr_cla_sec_id in(select id from temp1);
DROP TEMPORARY TABLE temp1;
end;
FOR EACH ROW begin
delete from shop_class_third where cla_thr_cla_sec_id=new.cla_thr_cla_sec_id
end;我用navicat设计的,保存的时候提示图片显示的错误。用 new.cla_thr_cla_sec_id 这种写法应该是不行的
FOR EACH ROW begin
delete from shop_class_third where cla_thr_cla_sec_id=old.cla_thr_cla_sec_id
end;
delete from shop_class_second where cla_sec_cla_fir_id=OLD.cla_fir_id;
end;
这是最终可以招待的代码。与前面几位提供的差别不大,但使用的是“OLD”而非“NEW”。