SQLite NOT NULL Constraint
NOT NULL
By default, a column can hold NULL values. NOT NULL makes sure that the column always has a value.
Example
This table must have a value in PersonID, LastName and FirstName:
CREATE TABLE IF NOT EXISTS Persons (
PersonID INTEGER NOT NULL,
LastName TEXT NOT NULL,
FirstName TEXT NOT NULL,
Age INTEGER
);
Try it Yourself »
Breaking the Rule
This example tries to insert a person without a last name:
Example
CREATE TABLE IF NOT EXISTS Persons (
PersonID INTEGER NOT NULL,
LastName TEXT NOT NULL,
FirstName TEXT NOT NULL,
Age INTEGER
);
INSERT INTO Persons (PersonID, FirstName)
VALUES (1, 'Emil');
Try it Yourself »
NOT NULL With DEFAULT
A column that is NOT NULL often has a DEFAULT value too. Then the column gets the default value when you do not give one:
Example
CREATE TABLE IF NOT EXISTS Members (
Name TEXT NOT NULL,
Level TEXT NOT NULL DEFAULT 'Basic'
);
INSERT INTO Members (Name) VALUES ('Nora');
SELECT * FROM Members;
Try it Yourself »