我知道一个临时表仅在打开SQL Server会话时才存在,但是为什么对它们没有外键约束呢?

最佳答案

您可以在tempdb中的表之间创建外键。例如,尝试以下操作:

use tempdb

create table parent
(
    parent_key int primary  key clustered
)

create table child
(
    child_key int primary key clustered,
    child_parent_key int
)
alter table child add constraint fk_child_parent foreign key (child_parent_key) references parent(parent_key)

insert into parent(parent_key) select 1
insert into child(child_key, child_parent_key) select 1, 1
insert into child(child_key, child_parent_key) select 2, 2 -- this fails because of the FK constraint

drop table child
drop table parent

07-28 02:49
查看更多