Data length: greater than 0 : Column Constraints « Constraints « SQL Server / T-SQL






Data length: greater than 0

1> CREATE TABLE T (
2>     int1 int IDENTITY PRIMARY KEY,
3>     vch1 varchar(5) CHECK (LEN(vch1) > 0) NOT NULL,
4>     vch2 varchar(5) CONSTRAINT CK_LEN_TOO_SHORT CHECK (LEN(vch2) > 0) NOT NULL
5> )
6> GO
1> INSERT T (vch1, vch2) VALUES('a','b')
2> GO

(1 rows affected)
1> INSERT T (vch1, vch2) VALUES('','b')
2> GO
Msg 547, Level 16, State 1, Server JAVA2S\SQLEXPRESS, Line 1
The INSERT statement conflicted with the CHECK constraint "CK__T__vch1__02D256E1". 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 JAVA2S\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
Msg 515, Level 16, State 2, Server JAVA2S\SQLEXPRESS, Line 1
Cannot insert the value NULL into column 'vch1', table 'master.dbo.T'; column does not allow nulls. INSERT fails.
The statement has been terminated.
1>
2> select * from t
3> GO
int1        vch1  vch2
----------- ----- -----
          1 a     b

(1 rows affected)
1>
2> drop table t
3> GO
1>
2>

           
       








Related examples in the same category

1.Add constraints for two columns
2.Constraint: PRIMARY KEY
3.Constraint check (Options)
4.Two constraints for one single column: Not NULL and default value