我按照如何从How to Delete using INNER JOIN with SQL Server?
执行删除操作我要删除的数据:
select * from com.Address a
inner join com.Contact as c on c.AddressId = a.AddressId
inner join cqt.CQMTrainer as t on t.ContactId = c.ContactId
select from com.Contact c
inner join cqt.CQMTrainer as t on t.ContactId = c.ContactId
这是我要执行的删除:
delete a from com.Address a
inner join com.Contact as c on c.AddressId = a.AddressId
inner join cqt.CQMTrainer as t on t.ContactId = c.ContactId
delete c from com.Contact c
inner join cqt.CQMTrainer as t on t.ContactId = c.ContactId
但是,一旦我执行它们,我就会遇到一些外键错误:
DELETE语句与REFERENCE约束冲突 “FK_com.Contact_com.Address_AddressId”。冲突发生在 数据库“”,表“com.Contact”,列'AddressId'。
我错误地追踪了什么?我甚至尝试添加begin transaction
和commit transaction
。
答案 0 :(得分:1)
如果你以另一种方式做到这一点怎么样:
delete c from com.Contact c
inner join cqt.CQMTrainer as t on t.ContactId = c.ContactId
delete a from com.Address a
inner join com.Contact as c on c.AddressId = a.AddressId
inner join cqt.CQMTrainer as t on t.ContactId = c.ContactId
答案 1 :(得分:0)
查看您的查询时,您的关系已配置为在尝试删除另一个引用的记录时抛出异常。如果要避免它,则必须选择数据库服务器应如何处理此类情况。这是在参考属性上设置的。如果您使用的是SQL Server Management Studio,则:
然后你会发现两个规则,一个用于更新,一个用于删除,你 可以选择:
如果你没有使用这样的编辑器,可以通过放置
来设置相同的编辑器REFERENCES TableName ColumnName
值如:
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT }