SQL – NOT NULL

Prev     Next

  • NOT NULL is a constraint in SQL which is used not to store null values in a column while inserting new records in a table. Always, NOT NULL constraint ensures a column have NOT NULL value.
  • If we try to insert/update null values into NOT NULL columns, then, the query will be aborted.
  • Syntax for SQL NOT NULL constraint is given below.

SQL Syntax for NOT NULL constraint:

SQL Syntax for NOT NULL constraint CREATE TABLE table_name
(column_name1 data_type(size) NOT NULL,
column_name2 data_type(size) NOT NULL,
column_name3 data_type(size),
etc…);

Note: If we do not mention NOT NULL constraint, then, a table can hold NULL values by default.

Example for how to use SQL NOT NULL constraint:

  • Please execute below query in SQL to create a table with NOT NULL constraint.
CREATE TABLE student
(Student_ID int NOT NULL,
Student_name varchar(255) NOT NULL,
City varchar(255),
Marks int);
  • Now, a new table called “student” is created and the fields “Student_ID” and “Student_name” are specified as NOT NULL. So, these 2 columns can’t be inserted or updated with NULL values.

Prev     Next

Share this website !!!
Facebook Twitter Linkedin