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
 Constraint problem SQL

Author  Topic 

snowy
Starting Member

5 Posts

Posted - 2009-09-25 : 20:35:37
hey, could someone please tell me how i would go about
specifying the following constraint, it's quite specific
and I really don't have a clue how to do it ><.

I have a product table, and each product has
a product_ID which MUST consist of 4 letters followed
by 4 digits where the FIRST digit must be 1, 2, 3, 4 or 5

e.g. COKE2189, CAKE5555 etc

Thanx heaps,

raky
Aged Yak Warrior

767 Posts

Posted - 2009-09-26 : 02:45:16

hi..

Just add check constraint like this

ALTER TABLE Product WITH NOCHECK
ADD CONSTRAINT CK_Product_ID CHECK( Product_ID LIKE
'[A-Z][A-Z][A-Z][A-Z][1-5][0-9][[0-9][0-9]')
Go to Top of Page

snowy
Starting Member

5 Posts

Posted - 2009-09-26 : 23:42:31
Ahh thanx, I misread the spec.

i'm not allowed to have the number 4 as one of the First digits
so the FIRST digit of the 4 digits following the 4 letters
must be 1,2,3 or 5.

So would this be correct then?

ALTER TABLE Product WITH NOCHECK
ADD CONSTRAINT CK_Product_ID CHECK( Product_ID LIKE
'[A-Z][A-Z][A-Z][A-Z][1-3][0-9][[0-9][0-9]') OR

PRODUCT_ID LIKE '[A-Z][A-Z][A-Z][A-Z][5][0-9][[0-9][0-9]

ty
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-29 : 00:22:19
ALTER TABLE Product WITH NOCHECK
ADD CONSTRAINT CK_Product_ID CHECK( Product_ID LIKE
'[A-Z][A-Z][A-Z][A-Z][1,2,3,5][0-9][[0-9][0-9]')
Go to Top of Page
   

- Advertisement -