从SQL db中删除元组

时间:2016-05-04 10:14:21

标签: sql sql-server sql-server-2008

我必须做以下功课, 我分别有3个表:

TblNation with fields: id_nation (key), first nation, second nation
TblAthlete with fields: id_athlete (key), name, surname
TblUnion with fields: id_nation (key), id_athlete(key)

我必须删除与特定姓氏相对应的元组(它假设没有姓氏字段的冗余)。

我试过了:

DELETE FROM TblNation INNER JOIN
    TblUnion ON TblNation.id_Nation = 
    TblUnion.id_nation INNER JOIN
    TblAthlete ON TblUnion.id_athlete = TblAthlete.id_athlete
WHERE TblAthlete.surname = 'something'

但是这段代码不起作用......任何人都可以帮助我吗?

PS。我试图在Microsoft SQL Server Management Studio中运行此查询 提前致谢

2 个答案:

答案 0 :(得分:2)

试试这个:

DELETE T
FROM TblNation T
INNER JOIN TblUnion TU
    ON T.id_Nation = TU.id_nation
INNER JOIN TblAthlete TA
    ON TU.id_athlete = TA.id_athlete
WHERE TA.surname = 'something'

答案 1 :(得分:1)

我想你明白为什么你想写一个三表连接只是为了从一个表中删除行......

根据您声明的标准(我必须删除与特定姓氏相对应的元组),为什么不从TblAthlete删除字段id_athlete (关键),姓名,姓氏?

BEGIN TRAN
  DELETE from TblAthlete 
   WHERE surname = 'something';
COMMIT