NOT NULL constraint makes sure that a column does not hold NULL value. When we don’t provide value for a particular column while inserting a record into a table, by default it takes NULL value. By specifying NULL constraint, we can be sure that a particular column(s) cannot have NULL values.
How to specify the NULL constraint while creating table
Here I am creating a table “STUDENTS”. I have specified NOT NULL constraint for columns ROLL_NO, STU_NAME and STU_AGE which means you must provide the value for these three fields while inserting/updating records in this table. It enforces these column(s) not to accept null values.
CREATE TABLE STUDENTS( ROLL_NO INT NOT NULL, STU_NAME VARCHAR (35) NOT NULL, STU_AGE INT NOT NULL, STU_ADDRESS VARCHAR (235) , PRIMARY KEY (ROLL_NO) );
Specify the NULL constraint for already existing table
In the above section we learnt how to specify the NULL constraint while creating a table. However we can specify this constraint on a already present table also. For this we need to use ALTER TABLE statement.
ALTER TABLE STUDENTS MODIFY STU_ADDRESS VARCHAR (235) NOT NULL;
After this STU_ADDRESS column will not accept any null values.