Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

SQL NOT NULL

SQL NOT NULL Constraint

By default, columns can contain NULL values.

The NOT NULL constraint ensures a column cannot accept them, mandating that every field must have a value.

Thus, inserting or updating records requires specifying a value for this field.

SQL NOT NULL on CREATE TABLE

The subsequent SQL guarantees that the “ID”, “LastName”, and “FirstName” columns will not allow NULL values upon creating the “Persons” table:

Example

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

SQL NOT NULL on ALTER TABLE

To apply a NOT NULL constraint to the “Age” column after the creation of the “Persons” table, utilize the following SQL:

SQL Server / MS Access:

ALTER TABLE Persons
ALTER COLUMN Age int NOT NULL

My SQL / Oracle (prior version 10G):

ALTER TABLE Persons
MODIFY COLUMN Age int NOT NULL

Oracle 10G and later:

ALTER TABLE Persons
MODIFY Age int NOT NULL