loading

MySQL NOT NULL

MySQL NOT NULL Constraint

A column may contain NULL values by default.

A column is required to NOT accept NULL values by the NOT NULL constraint.

Because of this, a field is required to have a value at all times. This implies that you cannot update or add a new record without first putting a value to the field.

NOT NULL on CREATE TABLE

When the “Persons” table is created, the following SQL makes sure that the “ID,” “LastName,” and “FirstName” columns will not accept NULL values:

Example

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

NOT NULL on ALTER TABLE

Use the following SQL to establish a NOT NULL constraint on the “Age” column when the “Persons” table has already been created:

Example

				
					ALTER TABLE Persons
MODIFY Age int NOT NULL;
				
			
Share this Doc

MySQL NOT NULL

Or copy link

Explore Topic