NOT NULL constraints in SQL

Rumman Ansari   Software Engineer   2023-03-25   5862 Share
☰ Table of Contents

Table of Content:


NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL constraint is applied to a column, you cannot pass a null value to that column. It enforces a column to contain a proper value.

One important point to note about this constraint is that it cannot be defined at table level.

Example using NOT NULL constraint

Code:


CREATE TABLE Student(
s_id int NOT NULL,
 Name varchar(60),
 Age int
);

The above query will declare that the s_id field of Student table will not take NULL value.

SQL NOT NULL on ALTER TABLE

To create a NOT NULL constraint on the "Age" column when the "Student" table is already created, use the following SQL:

Code:


ALTER TABLE Student
MODIFY Age int NOT NULL;