NOT NULL Constraint in SQL
NOT NULL Constraint in SQL
By default, the columns are able to hold NULL values. A NOT NULL constraint in SQL is used to
prevent inserting NULL values into the specified column, considering it as a not accepted value for
that column. This means that you should provide a valid SQL NOT NULL value to that column in the
INSERT or UPDATE statements, as the column will always contain data.
Assume that we have the below simple CREATE TABLE statement that is used to define the
ConstraintDemo1 table. This table contains only two columns, ID and Name. In the ID column
definition statement, the SQL NOT NULL column-level constraint is enforced, considering the ID
column as a mandatory column that should be provided with a valid SQL NOT NULL value. The case
is different for the Name column that can be ignored in the INSERT statement, with the ability to
provide it with NULL value. If the null-ability is not specified while defining the column, it will accept
the NULL value by default:
USE SuppliersDemo
GO
CREATE TABLE ConstraintDemo1
(
ID INT NOT NULL,
Name VARCHAR(50) NULL
)
You will see that the first record will be inserted successfully, as both the ID and Name column’s
values are provided in the INSERT statement. Providing the ID only in the second INSERT statement
will not prevent the insertion process from being completed successfully, due to the fact that the
Name column is not mandatory and accepts NULL values. The last insert operation will fail, as we
only provide the INSERT statement with a value for the Name column, without providing value for
the ID column that is mandatory and cannot be assigned NULL value, as shown in the error message
below:
Checking the inserted data, you will see that only two records are inserted and the missing value for
the Name column in the second INSERT statement will be NULL, which is the default value, as shown
in the result below:
Assume that we need to prevent the Name column on the previous table from accepting NULL
values after creating the table, using the ALTER TABLE T-SQL statement below:
You will see that the command will fail, as it will check the existing values of the Name column for
NULL values first before creating the constraint, as shown in the error message below:
To enforce the NOT NULL Constraints in SQL, we should remove all NULL values of the Name column
from the table, using the UPDATE statement below, that replaces the NULL values with empty string:
If you try to create the Constraints in SQL again, it will be created successfully as shown below:
The SQL NOT NULL constraint can be also created using the SQL Server Management Studio, by
right-clicking on the needed table and select the Design option. Beside each column, you will find a
small checkbox that you can use to specify the null-ability of that column. Unchecking the checkbox
beside the column, a SQL NOT NULL constraint will be created automatically, preventing any NULL
value from being inserted to that column, as shown below: