sql-server - 是否在未使用约束更新列的 SQL 更新语句上检查外键约束?

标签 sql-server performance foreign-keys

是否在未使用约束更新列的 SQL 更新语句上检查外键约束? (在 MS SQL Server 中)

假设我有几个包含以下列的表格:

OrderItems

    - OrderItemID
    - OrderItemTypeID (FK to a OrderItemTypeID column on another table called OrderItemTypes) 
    - ItemName

如果我只是更新

update [dbo].[OrderItems]
set    [ItemName] = 'Product 3'
where  [OrderItemID] = 2508 

FK 约束会使用上面的更新语句进行查找/检查吗? (甚至认为更新不会改变该列的值?)

最佳答案

不,不检查外键。通过检查两个不同更新的执行计划很容易看出这一点。

create table a (
    id int primary key
)

create table b (
    id int, 
    fkid int
)

alter table b add foreign key (fkid) references a(id)

insert into a values (1)
insert into a values (2)

insert into b values (5,1) -- Seek on table a's PK

enter image description here

update b set id = 6 where id = 5 -- No seek on table a's PK

enter image description here

update b set fkid = 2 where id = 6 -- Seek on table a's PK

enter image description here

drop table b
drop table a

关于sql-server - 是否在未使用约束更新列的 SQL 更新语句上检查外键约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7573933/

相关文章:

java - 尽管在早期迭代中运行,但程序似乎卡住了

python - 性能不可变对象(immutable对象)设计

python - 有效地计算 python/numpy 中许多位串的平均位?

php - 将用户分配到 Laravel 关系中的任何商店?

c# - 重试命令/连接超时是否安全?

sql - 透视 SQL Server 表

mysql - 如何在mysql或JDBC的表中自动插入外键引用?

c# - ASP.NET WebApi JSON 响应和带有外键的实体

mysql - 使用 pentaho unicode 问题从 SQL Server 迁移到 MySQL

LIKE 之间的 SQL Server 日期