No foreign key restraints on a temporary table? SQL Server 2008

sql-server

Solution

You can create foreign keys between tables in tempdb. For example, try this:

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

Problem

I know that a a temporary table will only exist for as long as a session of SQL Server is open, but why can't you have foreign key restraints on them?

Original source