I have a lookup table
Tbl_UserType_value_lookup:
UserTypeID |AllowedValue
-------------------------
1 |x
1 |y
2 |u
3 |a
(This says an user of Type 1 can enter the values of type x and y only and not of type u or a..so on)
I have another table
Tbl_Activity:
UserID |userTypeID |value
---------------------------
Now in this table how can I resrtict a user of type 1 to the values as per Tbl_UserType_value_lookup table using CHECK constraint ?
Is there any other way?
Assuming that you have a unique key on
Tbl_UserType_value_lookupforUserTypeID, Allowed Value, you could have a composite foreign key onTbl_Activitythat references these columns.(ie the combination of UserType, Value would have to exist on
Tbl_UserType_value_lookupto be insertable.There’s quite a lot of discussion of this here:
Creating a composite foreign key in SQL Server 2008