T-SQL foreign key check constraint
Posted
by PaN1C_Showt1Me
on Stack Overflow
See other posts from Stack Overflow
or by PaN1C_Showt1Me
Published on 2010-05-28T08:01:14Z
Indexed on
2010/05/28
8:11 UTC
Read the original article
Hit count: 311
When you create a foreign key constraint in a table and you create the script in MS SQL Management Studio, it looks like this.
ALTER TABLE T1 WITH CHECK ADD CONSTRAINT FK_T1 FOREIGN KEY(project_id)
REFERENCES T2 (project_id)
GO
ALTER TABLE T1 CHECK CONSTRAINT FK_T1
GO
What I don't understand is what purpose has the second alter with check constraint. Isn't creating the FK constraint enough? Do you have to add the check constraint to assure reference integrity ?
Another question: how would it look like then when you'd write it directly in the column definition?
CREATE TABLE T1 (
my_column INT NOT NULL CONSTRAINT FK_T1 REFERENCES T2(my_column)
)
Isn't this enough?
© Stack Overflow or respective owner