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
 General SQL Server Forums
 New to SQL Server Programming
 constrains or forigen key ?

Author  Topic 

tsures77
Starting Member

1 Post

Posted - 2011-01-22 : 09:27:11

hi all !
have a newbe question about constrains.
using sql 2000.
i have 2 tables: customer and customerTypes.

customer table has the follwing columns :
1. CustomerID int pk
2. CustomerName varchar
3.CuatomerLastName varchar
4.CustomerType int

customerTypes table has the follwing columns :
1.TypeID int pk
2.TypeName varchar
3.TypeValue int

now ...
i want that in column customer.CustomerType the values can only come from customerTypes.TypeValue where customerTypes.TypeName equals "regular".

shoul i use constarins or just forigen key ?

thanks in advance
hubby

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-01-22 : 09:48:20
A Foreign Key is a constraint. And that is the type of constraint you want.
ALTER TABLE customer
ADD CONSTRAINT FK_Customer_CustomerType Foreign Key (CustomerType)
References CustomerTypes (TypeID);
GO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-24 : 10:41:02
if you want to enforce that where condition also you need to implement this in a trigger or use CHECK constraint utilizing a UDF where you check this.

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

Go to Top of Page
   

- Advertisement -