使用内连接删除
DELETING using Inner join
我遵循了如何从以下位置执行删除: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
但是,一旦我执行它们,我就遇到了一些外键错误:
The DELETE statement conflicted with the REFERENCE constraint
"FK_com.Contact_com.Address_AddressId". The conflict occurred in
database "", table "com.Contact", column 'AddressId'.
我在做什么?我什至尝试添加 begin transaction
和 commit transaction
.
查看您的查询,您的关系已配置为在您尝试删除另一条引用的记录时抛出异常。如果你想避免它,你必须选择数据库服务器应该如何处理这种情况。这是在参考属性上设置的。如果您使用的是 SQL Server Management Studio,则:
- 用鼠标右键单击 table select 'Design'
- 然后是您定义关系的列(它将
将其显示为行)
- 然后鼠标右键单击该列
你感兴趣,select 'Relationships'
- 然后在左窗格中按名称找到您的关系,单击它
- 然后在右窗格中,您将看到“插入和更新”
规范'组,展开
然后你会发现两条规则,一条更新,一条删除,你
可以 select:
- 无操作 - 像您的情况一样抛出异常
- Cascade - 删除引用行
- 设置 Null - 将 null 放入通过 FK 值引用该行的行中
- 设置默认值 - 将为此类列设置默认值。
如果你没有使用这样的编辑器,你可以在
之后设置相同的编辑器
REFERENCES TableName ColumnName
值如:
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT }
如果换一种方式呢:
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
- 首先您要删除联系人table的信息作为错误
说你有地址 ID 的联系人参考,这就是为什么你不能先删除地址 table
我遵循了如何从以下位置执行删除: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
但是,一旦我执行它们,我就遇到了一些外键错误:
The DELETE statement conflicted with the REFERENCE constraint "FK_com.Contact_com.Address_AddressId". The conflict occurred in database "", table "com.Contact", column 'AddressId'.
我在做什么?我什至尝试添加 begin transaction
和 commit transaction
.
查看您的查询,您的关系已配置为在您尝试删除另一条引用的记录时抛出异常。如果你想避免它,你必须选择数据库服务器应该如何处理这种情况。这是在参考属性上设置的。如果您使用的是 SQL Server Management Studio,则:
- 用鼠标右键单击 table select 'Design'
- 然后是您定义关系的列(它将 将其显示为行)
- 然后鼠标右键单击该列 你感兴趣,select 'Relationships'
- 然后在左窗格中按名称找到您的关系,单击它
- 然后在右窗格中,您将看到“插入和更新” 规范'组,展开
然后你会发现两条规则,一条更新,一条删除,你 可以 select:
- 无操作 - 像您的情况一样抛出异常
- Cascade - 删除引用行
- 设置 Null - 将 null 放入通过 FK 值引用该行的行中
- 设置默认值 - 将为此类列设置默认值。
如果你没有使用这样的编辑器,你可以在
之后设置相同的编辑器REFERENCES TableName ColumnName
值如:
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT }
如果换一种方式呢:
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
- 首先您要删除联系人table的信息作为错误
说你有地址 ID 的联系人参考,这就是为什么你不能先删除地址 table