Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 foreign key
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 01/17/2013 :  05:41:49  Show Profile  Reply with Quote
I have a database set up sort of like this

Table1(id, ...)
Table2(id, ...)
Comment(id, typeId, str)

where typeId=1 means Table1 and typeId=2 means Table2

what I want to do is to create foreign keys to Table1 and Table2 from Comment
sort of
ALTER TABLE Comment ADD CONSTRAINT FK_Comment_Table1 FOREIGN KEY(id, typeId) REFERENCES Table1 (id, 1);
ALTER TABLE Comment ADD CONSTRAINT FK_Comment_Table2 FOREIGN KEY(id, typeId) REFERENCES Table2 (id, 2);

Is it possible?
If not, how should I set up the relations?

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 01/17/2013 :  05:55:43  Show Profile  Reply with Quote
nope not possible.
only way to do this would be to enforce this referential logic by means of trigger/check constraint.

SQL Server MVP

Go to Top of Page

Aged Yak Warrior

550 Posts

Posted - 01/17/2013 :  18:54:25  Show Profile  Reply with Quote
You could try adding a TypeID column to Table1 and Table2, and then using that column name in the FK definition.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000