Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

SQL NOT NULL Constraint

  • Last Updated : 22 Sep, 2021

In SQL, constraints are some set of rules that are applied to the data type of the specified table. Or we can say that using constraints we can apply limits on the type of data that can be stored in the particular column of the table. Constraints are typically placed specified along with CREATE statement. By default, a column can hold null values.

Example:

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.



If you don’t want to have a null column or a null value you need to define constraints like NOT NULL. NOT NULL constraints make sure that a column does not hold null values, or in other words, NOT NULL constraint make sure that you cannot insert a new record or update a record without entering a value to the specified column(i.e., NOT NULL column). It prevents for acceptance of NULL values. It can be applied for column-level constraints. 

Syntax:

CREATE TABLE table_Name
(
column1 data_type(size) NOT NULL,
column2 data_type(size) NOT NULL,
....
);

SQL NOT NULL on CREATE a table

In SQL, we can add NOT NULL constraints while creating a table. For example, the “EMPID”, “EName” will not accept NULL values when the EMPLOYEES table is created because NOT NULL constraints are used with these columns.

CREATE TABLE EMPLOYEES(
EMPID INTEGER NOT NULL,
EName VARCHAR2(10) NOT NULL,
DOJ DATE);

SQL NOT NULL on ALTER  table

We can also add a NOT NULL constraint in the existing table using the ALTER statement. For example, if the EMPLOYEES table has already been created then add NOT NULL constraints to the “DOJ” column use ALTER statements in SQL as follows:

ALTER TABLE EMPLOYEES modify DOJ DATE NOT NULL;

My Personal Notes arrow_drop_up
Recommended Articles
Page :