Mega Code Archive

 
Categories / MSSQL Tutorial / Constraints
 

A table-level check constraint that limits Banker IDs to a specific format

A statement that defines the check constraint 8> CREATE TABLE Bankers 9> (BankerID        CHAR(6)     NOT NULL PRIMARY KEY, 10> BankerName       VARCHAR(50) NOT NULL, 11> CHECK     ((BankerID LIKE '[A-Z][A-Z][0-9][0-9][0-9][0-9]') AND 12>            (LEFT(BankerID,2) = LEFT(BankerName,2)))) 13> GO 1> 2> --An INSERT statement that fails due to the check constraint 3> 4> INSERT Bankers VALUES ('Mc4559','Castle Printers, Inc.') 5> 6> 7> drop table Bankers; 8> GO Msg 547, Level 16, State 1, Server J\SQLEXPRESS, Line 4 The INSERT statement conflicted with the CHECK constraint "CK__Bankers__6FFF798F". The conflict occurred in database "master", table "dbo.Bankers". The statement has been terminated.