从表中删除记录的过程彼此相关

时间:2018-02-08 07:10:17

标签: oracle stored-procedures cascading-deletes

我的表有这些结构(在oracle中):

---table grandmother ---
  ID
  some other columns


---table mother---
  ID
  grandmother_ID
  some other columns

---table girl
  ID
  mother_ID
  some other columns

---table grandchild1
  ID
  girl_ID
  some other columns 

---table grandchild2
  ID
  girl_ID
  some other columns 

现在,如何编写一个从祖母表中删除一条记录的程序?很明显,我们应该删除祖母表中祖母_IDs和女孩表中的mother_IDs以及来自孙子孙女2和孙子孙女2的girl_IDs的记录。

create or replace PROCEDURE GRAND_MOTHER_DELETE
( 
    IN_ID      IN      GRAND_MOTHER.ID%TYPE 
 )
 AS 
 BEGIN

  DELETE FROM GRAND_CHILD1 GC1 , GRAND_CHILD2 GC2 USING GC1 , GC2
  WHERE GC1.GIRL_ID = GC2.GIRL_ID
  AND GC2.GIRL_ID IN(   
  SELECT ID FROM GIRL WHERE MOTHER_ID IN (
  SELECT ID FROM MOTHER WHERE GRAND_MOTHER_ID=IN_ID));      

  DELETE
  FROM   GRAND_MOTHER
  WHERE  ID  = IN_ID; 

 END;

但它给出了语法错误。

1 个答案:

答案 0 :(得分:0)

根据@Littlefoot回答,我在grandchild1,grandchild2,girl和mother table中添加ON DELETE CASCADE,我的问题解决了。