A NOT NULL phrase adds a constraint to restrict the input of rows with a null value. : Constriant violation « Constraints « SQL Server / T-SQL Tutorial






5>
6> CREATE TABLE T (
7>     int1 int IDENTITY PRIMARY KEY,
8>     vch1 varchar(5)
9>         CHECK (LEN(vch1) > 0)
10>          NOT NULL,
11>      vch2 varchar(5)
12>          CONSTRAINT CK_LEN_TOO_SHORT
13>          CHECK (LEN(vch2) > 0)
14>          NOT NULL
15>  )
16>  GO
1>
2> INSERT T (vch1) VALUES('40222')
3> INSERT T (vch1) VALUES('4022')
4> INSERT T (vch1) VALUES('r0222')
5> INSERT T DEFAULT VALUES
6> GO
Msg 515, Level 16, State 2, Server J\SQLEXPRESS, Line 2
Cannot insert the value NULL into column 'vch2', table 'master.dbo.T'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 515, Level 16, State 2, Server J\SQLEXPRESS, Line 3
Cannot insert the value NULL into column 'vch2', table 'master.dbo.T'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 515, Level 16, State 2, Server J\SQLEXPRESS, Line 4
Cannot insert the value NULL into column 'vch2', table 'master.dbo.T'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 515, Level 16, State 2, Server J\SQLEXPRESS, Line 5
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>
3> drop table t;
4> GO
1>








7.12.Constriant violation
7.12.1.Trap Both Primary Key and CHECK Constraint Violations
7.12.2.Exception in a transaction
7.12.3.A NOT NULL phrase adds a constraint to restrict the input of rows with a null value.
7.12.4.Update statement and check-constraints