Mega Code Archive

 
Categories / MSSQL Tutorial / Constraints
 

Using a Multicolumn CHECK Constraint

4>  CREATE TABLE ClassGrades( 5>     ClassID int, 6>     StudentID int, 7>     GradeLetter varchar(2), 8>     Constraint PK_ClassGrades 9>         PRIMARY KEY(ClassID, StudentID), 10>     Constraint CK_GradeRange_ClassID 11>         CHECK (LEFT(UPPER(GradeLetter),1) 12>         LIKE '[A-F]' AND ClassID < 1000) 13> ) 14> GO 1> 2> INSERT ClassGrades VALUES(1, 1, 'C+') 3> INSERT ClassGrades VALUES(1, 2, 'A+') 4> INSERT ClassGrades VALUES(1, 3, 'V-') 5> INSERT ClassGrades VALUES(1001, 1, 'A') 6> INSERT ClassGrades VALUES(999, 2, 'A') 7> GO (1 rows affected) (1 rows affected) Msg 547, Level 16, State 1, Server J\SQLEXPRESS, Line 4 The INSERT statement conflicted with the CHECK constraint "CK_GradeRange_ClassID". The conflict occurred in database "master", table "dbo.ClassGrades". The statement has been terminated. Msg 547, Level 16, State 1, Server J\SQLEXPRESS, Line 5 The INSERT statement conflicted with the CHECK constraint "CK_GradeRange_ClassID". The conflict occurred in database "master", table "dbo.ClassGrades". The statement has been terminated. (1 rows affected) 1> 2> drop table ClassGrades; 3> GO