check constraint using SELECT from lookup table sql server 2005

Hi Experts,

Just wondering if this is A possible and B the right thing to do if it is.
Or what is best practice when you have lots of small (count) variable constraints.

I would like to use a constraint on a table field, as a lookup from another table, tblConstraints.

So for example see code attachment
--Table for All Constraints
CREATE TABLE tblConstraints (					intConstraintID int IDENTITY(1,1) NOT NULL,
vchConstraintName varchar(50) NOT NULL,
vchConstraintValue varchar(100) NOT NULL,
intOrder int NULL
--Insert a Constraint
INSERT INTO tblConstraints (vchConstraintName, vchConstraintValue)
SELECT 'TreeSpecies',  'BAN'

--Select Constraint
SELECT vchConstraintValue
FROM tblConstraints
WHERE vchConstraintName = 'TreeSpecies'

--Add Constraint to a table??
CREATE TABLE [dbo].[TreeSpecies] ( 
 [Species] char (255)  NOT NULL )
 ALTER TABLE [TreeSpecies]
ADD CONSTRAINT [CK_TreeSpecies_Species] CHECK ( ( 
 [Species] IN ( 
SELECT vchConstraintValue
FROM tblConstraints
WHERE vchConstraintName = 'TreeSpecies' 
)  )  )  ) ;

Open in new window

Craig LambieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Unfortunately you cannot create a check constraint based on the value in another table. What you should do in this case is set up a foreign key so that the values in your main table have to also exist in another table.
Craig LambieAuthor Commented:
ok, this is what I thought, but I understand that FK has to be PK of the other table...
Hence needing to create a table for every lookup/ constraint, which I just don't understand....

Of course the other alternative is to edit the Constraints using a SP I guess.... of course you can use this type of SQL from to query the constraints, so why not a "non system" table for it?

USE AdventureWorks;
SELECT AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id

Open in new window

Craig LambieAuthor Commented:
Any further comments gothamite?
The column that a foreign key references does not have to be a primary key, it just has to have a unique constraint on it.

I don't think SQL is simply 'looking' in sys.constraints, rather that the constraints are a piece of table metadata the existence of which can be viewed using this system view.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.