SQL Cascading删除没有外键?

Mae*_*aen 2 sql database

我有以下表格:

Country: Country_ID, CountryName
Regions: Region_ID, RegionName, Country_ID
Areas: Area_ID, AreaName, RegionID
Run Code Online (Sandbox Code Playgroud)

我没有使用外键而且没有计划这样做,我现在要做的就是能够在tableAdapter of country table中的一个查询中删除国家和所有相关区域,区域......

怎么样?

Jon*_*jap 11

在一个查询?我怀疑你能.

但是你可以用三个来做:

delete from Areas where RegionID in (select Region_ID from Regions where Country_ID in (select Country_ID where CountryName='Somelandia'))
delete from Regions where Country_ID in (select Country_ID where CountryName='Somelandia')
delete from Country where CountryName = 'Somelandia'
Run Code Online (Sandbox Code Playgroud)

话虽如此,我强烈建议您重新考虑使用外键关系和级联删除为此目的.

  • +1"强烈建议您重新考虑使用外键关系" (6认同)

sha*_*oth 5

使用事务。启动一个事务,然后使用三个 DELETE 语句,然后是 COMMIT TRANSACTION 语句。