Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL NOT NULL Constraint

SQL NOT NULL Constraint


By default, a table's columns accept NULL values.


SQL NOT NULL Constraint

The NOT NULL constraint enforces a column to not accept NULL values.

The NOT NULL constraint forces a field to always contain a value. This means you cannot insert a new record or update a record without adding a value to the field.

The following SQL forces the "ID", "LastName", and "FirstName" columns to not accept NULL values:

Example

CREATE

TABLE

Persons

(

ID

int

NOT

NULL

,

LastName

varchar

(

255

)

NOT

NULL

,

FirstName

varchar

(

255

)

NOT

NULL

,

Age

int

)

;

Add a NOT NULL Constraint

To add a NOT NULL constraint to the "Age" field of an already created table, use the following:

Example

ALTER

TABLE

Persons

MODIFY

Age

int

NOT

NULL

;

Drop a NOT NULL Constraint

To drop a NOT NULL constraint from the "Age" field of an already created table, use the following:

Example

ALTER

TABLE

Persons

MODIFY

Age

int

NULL

;