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)
 Unique Column Based off FK

Author  Topic 

rypi
Yak Posting Veteran

55 Posts

Posted - 2011-01-29 : 15:25:17
Is it possible to make a column unique based off a foreign key?

For example, lets say there is a table like this:
ID FKID UniqueCol
1 3 Test
2 3 Test1
3 4 Test1
4 4 Test2

The "UniqueCol" can have duplicate entries only if it's FKID is different. So there could be multiple 'Test1' entries as long as there is only 1 'Test1' entry per FKID.

Is this possible?

Thanks in advance.

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-01-29 : 15:42:07
ALTER TABLE <tablename> ADD CONSTRAINT <constraint name> UNIQUE (FKID, UniqueCol)

--
Gail Shaw
SQL Server MVP
Go to Top of Page

rypi
Yak Posting Veteran

55 Posts

Posted - 2011-01-31 : 11:05:39
Thanks GilaMonster!
Go to Top of Page
   

- Advertisement -