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 2000 Forums
 SQL Server Development (2000)
 Conditional Constraint - HowTo

Author  Topic 

KenA
Starting Member

28 Posts

Posted - 2004-06-30 : 18:09:02
In the table below:

===========
Roles
===========
UserID (int)
GroupID
===========

Considering that UserID is FK from Users Table and GroupID is FK from Groups Table:

===========
Groups
===========
GroupID (int)
GroupName
===========


===========
Users
===========
UserID
Username
Password
Email
===========

And considering a User can have 0 or many Roles, exs:

Roles table
===========
1,1
1,2
1,3
===========
Meaning that this user have roles: 1,2,3


The problem is that I can have also:
Roles table
===========
1,1
1,1
1,1
===========
which is bad, so I need to have a constraint in the Roles table to force GroupID Unique ONLY in case UserID is already there!

Is it possible?


»»» Ken.A

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-06-30 : 21:47:02
[code]CREATE UNIQUE INDEX UI_UserID_RoleID on Roles(UserID,GroupID)[/code]
Go to Top of Page
   

- Advertisement -