删除 SQL Server 表中带外键的数据 [英] Delete data with foreign key in SQL Server table

查看:100
本文介绍了删除 SQL Server 表中带外键的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将删除与另一个表(子表)有关系的 SQL Server 表(父表)中的数据.
我尝试了基本的删除查询.但它不起作用(我知道它不会).

I'm going to delete data in an SQL Server table (parent) which has a relationship with another table (child).
I tried the basic Delete query. But it isn't working (and I know it won't).

DELETE FROM table WHERE ...

返回以下错误

DELETE 语句与 REFERENCE 约束冲突...

The DELETE statement conflicted with the REFERENCE constraint ...

我需要保留表的架构.我知道我只需要在查询中添加一些字词,我以前曾经这样做过,但我就是想不起来.

I need to keep the table's schema. I know that I just need to add some words in the query, I've ever done this before, but I just couldn't recall it.

推荐答案

可以在删除前后禁用和重新启用外键约束:

You can disable and re-enable the foreign key constraints before and after deleting:

alter table MyOtherTable nocheck constraint all
delete from MyTable
alter table MyOtherTable check constraint all

这篇关于删除 SQL Server 表中带外键的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆