A table-level check constraint that limits Banker IDs to a specific format : Check « Constraints « SQL Server / T-SQL Tutorial






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.








7.5.Check
7.5.1.Using CHECK Constraints
7.5.2.Adding a CHECK Constraint to an Existing Table
7.5.3.Constraints with name
7.5.4.CHECK Clause
7.5.5.Check Constraint
7.5.6.CHECK (DATEPART(DAY, GETDATE()) < 28)
7.5.7.A check constraint uses an expression to qualify records that are acceptable for any Inserts or Updates
7.5.8.Use or to link two conditions for check constraint
7.5.9.Using a Multicolumn CHECK Constraint
7.5.10.Check for data length
7.5.11.Pattern based constraint
7.5.12.Mark nocheck for a constraint
7.5.13.A table-level check constraint that limits Banker IDs to a specific format