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.”
The following SQL statement ensures that the “ID”, “LastName”, and “FirstName” columns in the “Persons” table will not allow NULL values during creation:
CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int ); |
To add a NOT NULL constraint to the “Age” column in the existing “Persons” table, use the following SQL:
ALTER TABLE Persons MODIFY Age int NOT NULL; |