SQL NOT NULL Constraint

By default, a column can hold NULL value. The SQL NOT NULL Constraint is used to force a column cannot accept a NULL value, and must hold a value.

CREATE TABLE Users
(
    UserID int NOT NULL,
    FirstName varchar(100) NOT NULL,
    LastName varchar(100) NOT NULL,
    Email varchar(200),
    Phone varchar(50),
    Birthday Date    
)

The above script will create a table named “Users”, the column UserID, FirstName and LastName cannot hold NULL value. If you try to insert a row without specify the UserID, FirstName or LastName:

INSERT INTO Users (UserID, FirstName, Email, Phone, Birthday)
VALUES (1, 'Tom', 'test@test.com', '999-9999', '1992-12-12')

You would get an error, because the LastName is not specified and would be set a NULL value.

Leave a Comment