Mega Code Archive

 
Categories / MSSQL / Constraints
 

Add constraints for two columns

1> CREATE TABLE T ( 2>     int1 int IDENTITY PRIMARY KEY, 3>     vch1 varchar(5) CHECK (LEN(vch1) > 0), 4>     vch2 varchar(5) CONSTRAINT CK_LEN_TOO_SHORT CHECK (LEN(vch2) > 0) 5> ) 6> GO 1> 2> INSERT T (vch1, vch2) VALUES('a','b') 3> GO (1 rows affected) 1> 2> INSERT T (vch1, vch2) VALUES('','b') 3> GO Msg 547, Level 16, State 1, Server RNTSOFT\SQLEXPRESS, Line 2 The INSERT statement conflicted with the CHECK constraint "CK__T__vch1__7F01C5FD". The conflict occurred in database "master", table "dbo.T", column 'vch1'. The statement has been terminated. 1> INSERT T (vch1, vch2) VALUES('a','') 2> GO Msg 547, Level 16, State 1, Server RNTSOFT\SQLEXPRESS, Line 1 The INSERT statement conflicted with the CHECK constraint "CK_LEN_TOO_SHORT". The conflict occurred in database "master", table "dbo.T", column 'vch2'. The statement has been terminated. 1> INSERT T DEFAULT VALUES 2> GO (1 rows affected) 1> 2> select * from t 3> GO int1        vch1  vch2 ----------- ----- -----           1 a     b           4 NULL  NULL (2 rows affected) 1> drop table t 2> GO 1>