Curriculum
Course: MYSQL
Login

Curriculum

MYSQL

MySQL References

0/140
Text lesson

MySQL NOT NULL

MySQL NOT NULL Constraint

By default, a column in MySQL can hold NULL values.

The NOT NULL constraint restricts a column from accepting NULL values, ensuring that the column always contains data.

This requirement means you cannot insert a new record or update an existing one without providing a value for this field.”

NOT NULL on CREATE TABLE

The following SQL statement ensures that the “ID”, “LastName”, and “FirstName” columns in the “Persons” table will not allow NULL values during creation:

Example

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255NOT NULL,
    FirstName varchar(255NOT NULL,
    Age int
);

NOT NULL on ALTER TABLE

To add a NOT NULL constraint to the “Age” column in the existing “Persons” table, use the following SQL:

Example

ALTER TABLE Persons
MODIFY Age int NOT NULL;