Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql-server

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?

like image 416
wootscootinboogie Avatar asked Jun 07 '12 17:06

wootscootinboogie


People also ask

Can you have a foreign key on a temp table?

FOREIGN KEY constraints aren't enforced on temporary tables.

Can you have a foreign key without a constraint?

Foreign Keys without the constraintsYou don't have to configure a foreign key constraint on a column just because it refers to another column. You could instead configure two tables such that one refers to the other, but without any defined foreign key.

What happens if there is no foreign key in SQL?

The obvious problem with the lack of foreign keys is that a database can't enforce referential integrity and if it wasn't taken care of properly at the higher level then this might lead to inconsistent data (child rows without corresponding parent rows).

How do I temporarily disable foreign key constraints in SQL Server?

Use SQL Server Management StudioIn Object Explorer, expand the table with the constraint and then expand the Keys folder. Right-click the constraint and select Modify. In the grid under Table Designer, select Enforce Foreign Key Constraint and select No from the drop-down menu. Select Close.


2 Answers

Imagine this scenario: You create a foreign key relationship from your temp table to a concrete table's key. One of the restrictions on a foreign key relationship is that you cannot delete a row from a key table that is depended upon by your temp table. Now, generally when you create foreign key relationships you know to delete the dependent table rows before deleting the related rows in the key table, but how does a stored procedure or any other call into the database know to delete rows from your temp table? Not only is it impossible to discover spurious foreign key dependencies, other sessions could not reach your temp table even if it could discover the relationship. This leads to spurious failures in delete statements as foreign key constraints restrict the key table for dependent rows.

like image 79
Kenny Biel Avatar answered Nov 15 '22 07:11

Kenny Biel


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
like image 36
Paul Williams Avatar answered Nov 15 '22 08:11

Paul Williams