0% found this document useful (0 votes)
54 views

NOT NULL Constraint in SQL

The NOT NULL constraint in SQL prevents NULL values from being inserted into specified columns. It requires columns with the NOT NULL constraint to have a valid non-NULL value provided during INSERT and UPDATE statements. The document demonstrates creating a table with an ID column defined as NOT NULL, allowing it to successfully insert records with a value for ID but not when only the non-mandatory Name column is provided. It also shows that the NOT NULL constraint cannot be added to an existing column with NULL values already, those values must first be updated to non-NULL.

Uploaded by

Priyanka Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

NOT NULL Constraint in SQL

The NOT NULL constraint in SQL prevents NULL values from being inserted into specified columns. It requires columns with the NOT NULL constraint to have a valid non-NULL value provided during INSERT and UPDATE statements. The document demonstrates creating a table with an ID column defined as NOT NULL, allowing it to successfully insert records with a value for ID but not when only the non-mandatory Name column is provided. It also shows that the NOT NULL constraint cannot be added to an existing column with NULL values already, those values must first be updated to non-NULL.

Uploaded by

Priyanka Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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
)
 

If we try to perform the below three insert operations:

INSERT INTO ConstraintDemo1 ([ID],[NAME]) VALUES (1,'Ali')


GO
INSERT INTO ConstraintDemo1 ([ID]) VALUES (2)
GO
INSERT INTO ConstraintDemo1 ([NAME]) VALUES ('Fadi')
GO
 

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:

ALTER TABLE ConstraintDemo1 ALTER COLUMN [Name] VARCHAR(50) NOT NULL

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:

UPDATE ConstraintDemo1 SET [Name]='' WHERE [Name] IS NULL

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:

You might also like