代码之家  ›  专栏  ›  技术社区  ›  SmartestVEGA

如何从相互引用的表中删除行?

  •  1
  • SmartestVEGA  · 技术社区  · 15 年前

    我想这样做:

    delete from table1 a,table2 b, table3  c 
     where a.col1 = b.col1 
       and b.col2 = c.col2 
       and a.co3 <> 8001;
    

    但这给了我一个错误。

    4 回复  |  直到 15 年前
        1
  •  3
  •   KM.    15 年前

    先删除最低级别,然后从那里向上移动,每个级别一个删除,直到最高级别:

    DELETE FROM ChildTable WHERE ParentID=...
    
    DELECT FROM ParentTable WHERE ParentID=...
    
        2
  •  2
  •   bkaid    15 年前

    您可以启用级联删除,然后删除父记录。

        3
  •  1
  •   Thomas    15 年前

    由于您没有指定每个表的外键和字段,因此我猜测:

    Delete TableC
    Where Exists( Select 1 From TableA Where TableA.Col1 = TableC.Col2 And TableA.Col3 <> '8001' )
    
    Delete TableB
    Where Exists( Select 1 From TableA Where TableA.Col1 = TableB.Col2 And TableA.Col3 <> '8001' )
    
    Delete TableA
    Where Col3 <> '8001'
    
        4
  •  0
  •   anu    15 年前

    从表1A、表2B、表3C中删除A
    其中a.col1=b.col1
    和b.col2=c.col2
    以及a.co3<>8001;