I'm creating a table that looks something like this.
CREATE TABLE packages
(
productCode char(2)
, name nvarchar(100)
, ...
)
I want to make sure the productCode is always one of two values: XJ or XD. How do I do that?
I'm creating a table that looks something like this.
CREATE TABLE packages
(
productCode char(2)
, name nvarchar(100)
, ...
)
I want to make sure the productCode is always one of two values: XJ or XD. How do I do that?
ALTER TABLE packages
ADD CONSTRAINT constraintname CHECK (productCode in ('XJ', 'XD'))
Either make it a foreign key to a lookup table, or add a check constraint to enforce it.
CREATE TABLE packages
(
productCode char(2)
, name nvarchar(100)
, ...
,CONSTRAINT productCode CHECK (productCode in ('XJ','XD') )
)
In this case it sounds like the valueset for ProductCode is pretty limited and that you don't expects it to grow in a foreseeable future, so I tend to agree with checkconstraint answers. However in most cases I would implement the foreign key solution as suggested by Mr. Grant as my customers has a nasty habit of changing their mind ( and the requirements as well ) about once a day. In that situation it is my expirence that the FK version is easier to maintain.