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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 foreign key

Author  Topic 

ergot72
Starting Member

1 Post

Posted - 2013-01-17 : 05:41:49
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?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2013-01-17 : 18:54:25
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
   

- Advertisement -