如何对Foreign Key 约束的引用列进行 insert 或 update

如果一个Table是其他表的外键,那么对该Table的引用列进行Update 或 Insert 会受到很多限制。Disable Foreign Key Constraints with INSERT and UPDATE Statements。

查看FK的信息,主要使用sys.foreign_keys 和 sys.foreign_key_columns 这两个表,辅助表:sys.objects (type=‘F‘),sys.tables,sys.columns

--get FK list

select fk.object_id as FK_Object_ID,
    fk.name as FK_name,
    pt.name as ParentTable_Name,
    pc.name as ParentTable_Column_Name,
    rt.name as ReferencedTable_Name,
    rc.name as ReferencedTable_Column_Name
from sys.foreign_keys fk with(nolock)
inner join sys.foreign_key_columns fkc with(nolock)
    on fk.object_id=fkc.constraint_object_id
inner join sys.tables pt with(nolock)
    on fkc.parent_object_id=pt.object_id
inner join sys.columns pc with(nolock)
    on fkc.parent_object_id=pc.object_id and fkc.parent_column_id=pc.column_id
inner join sys.tables as rt with(nolock)
    on fkc.referenced_object_id=rt.object_id
inner join sys.columns rc with(nolock)
    on fkc.referenced_object_id=rc.object_id and fkc.referenced_column_id=rc.column_id
where rt.name=N‘Referenced_Table_name‘

--disable FK constraint
ALTER table ParentTable_name
nocheck constraint FK_name

--enable FK constraint
ALTER table ParentTable_name
check constraint FK_name

参考MSDN:ALTER TABLE (Transact-SQL)

ALTER TABLE  schema_name . table_name
[ WITH { CHECK | NOCHECK } ]
{ CHECK | NOCHECK } CONSTRAINT { ALL | constraint_name [ ,...n ] }

WITH CHECK | WITH NOCHECK

Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.

If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.

The query optimizer does not consider constraints that are defined WITH NOCHECK. Such constraints are ignored until they are re-enabled by using ALTER TABLE table WITH CHECK CHECK CONSTRAINT ALL.

{ CHECK | NOCHECK } CONSTRAINT     

Specifies that constraint_name is enabled or disabled. This option can only be used with FOREIGN KEY and CHECK constraints. When NOCHECK is specified, the constraint is disabled and future inserts or updates to the column are not validated against the constraint conditions. DEFAULT, PRIMARY KEY, and UNIQUE constraints cannot be disabled.

ALL     

Specifies that all constraints are either disabled with the NOCHECK option or enabled with the CHECK option.

参考Post:

How can I list all foreign keys referencing a given table in SQL Server?

时间: 2024-11-24 17:51:22

如何对Foreign Key 约束的引用列进行 insert 或 update的相关文章

删除提示 FOREIGN KEY 约束引用”

有时想删除某个表时,提示“无法删除对象 'Orders',因为该对象正由一个 FOREIGN KEY 约束引用”,原因很简单不要急躁,它被其它表的外键引用了,所以无法删除,在此只需先找到哪些表的外键引用了该表的字段.通过系统函数就能解决(SQL Server系统函数提供了非常完善的功能,能代替我们查找和解决许多问题). select    fk.name,fk.object_id,OBJECT_NAME(fk.parent_object_id) as referenceTableNamefrom

SQL FOREIGN KEY 约束

SQL FOREIGN KEY 约束 一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY. 让我们通过一个例子来解释外键.请看下面两个表: "Persons" 表: Id_P LastName FirstName Address City 1 Adams John Oxford Street London 2 Bush George Fifth Avenue New York 3 Carter Thomas Changan Street Beijing &qu

SQL PRIMARY KEY 约束\SQL FOREIGN KEY 约束\SQL CHECK 约束

SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主键. SQL PRIMARY KEY Constraint on CREATE TABLE 下面的 SQL 在 "Persons" 表创建时在 "Id_P" 列创建 PRIMARY KEY 约束: MySQL: CREATE TABLE Persons ( Id_P i

更改具有Foreign key约束的表

1.Foreign key 说明: foreign key(外键) 建立起了表与表之间的约束关系,让表与表之间的数据更具有完整性和关联性.设想,有两张表A.B,A表中保存了许多电脑制造商的信息,比如联想.戴尔.惠普和华硕,B表中保存了许多多型号的电脑,比如lenovo1,lenovo2,hp1,hp2,hp3,dell1,dell2,asus1,现在如何将两张具有从属关系(每个电脑品牌下都有很多型号的电脑)的表关联起来呢? 我们可以在B表中设置外键.首先我们给这个外键关联关系起个名字xx,外键作

sql数据库删除表的外键约束(INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX)

使用如下SQL语句查询出表中外键约束名称: 1 select name 2 from sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id 3 where f.parent_object_id=object_id('表名') 执行如下SQL语句删除即可. 1 alter table 表名 drop constraint 外键约束名 sql数据库删除表的外键约束(INSERT 语句与 F

清空数据库错误:因为该表正由 FOREIGN KEY 约束引用 解决办法

如下解决了五个问题 1. 清空数据 2. 有外键也可以, 因为是逆向删除, 从最后一张表删除. 且使用的是delete, 因为truncate不能对有外键的表 3. 种子问题, 如果表存在种子重设为0, 如不存在就不操作 4. 加了事务, 中间报错, 有后悔机会 5. 截断日志功能, 因为使用delete, 删除后日志文件会增大, 可以不使用 if( object_id('pr_DataClear') is not null ) drop procedure pr_DataClear go cr

sql 创建外键关系时,ALTER TABLE 语句与 FOREIGN KEY 约束"FK_Doorl_REFERENCE_Floor"冲突

http://blog.csdn.net/dingxingmei/article/details/12996665 关键点: 可视化建立关系操作中有一个属性 : 在创建或重新启用时检查现有数据“    默认为“  是”   把它设置为 否 . 就可以了

oracle约束总结(not null/unique/primary key/foreign key/check)

约束(constraint):对创建的表的列属性.字段进行的限制.诸如:not null/unique/primary key/foreign key/check 作用范围: ①列级约束只能作用在一个列上 ②表级约束可以作用在多个列上(当然表级约束也可以作用在一个列上) 定义方式:列约束必须跟在列的定义后面,表约束不与列一起,而是单独定义. - -NOT NULL:不为空约束,只能定义在列级 CREATE TABLE employees( employee_id NUMBER(6), --<sp

SQL高级应用--约束二--(PRIMARY KEY、FOREIGN KEY)

四.SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录 主键必须包含唯一的值 主键列不能包含 NULL 值 每个表都应该有一个主键,并且每个表只能有一个主键 下面的SQL 在 Persons 表创建时在 Id_P列创建PRIMARY KEY 约束 MySQL CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255),