SQL NOT NULL Constraint
By default, in SQL a column can accept NULL values.
The NOT NULL
constraint specifies that a column does not accept NULL values.
If a NOT NULL
constraint is specified on a column, then SQL will not accept to insert a new row in a table without adding a non-NULL for that column.
Note: NULL means no entry has been made. It is different from a blank, zero (0), or a zero-length character string such as ''.
SQL NOT NULL on CREATE TABLE
The following SQL statement will create a new table called "Employee" with five columns, out of which three columns, id, first_name, last_name, do not accept NULL values.
CREATE TABLE employee (
id INT NOT NULL
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
birth_data DATE,
hire_date DATE
);
SQL NOT NULL on ALTER TABLE
The following SQL statement will add a NOT NULL
constraint on the "birth_date" column after the creation of the "Employee" table.
ALTER TABLE employee
MODIFY birth_date DATE NOT NULL;