In this part of the SQLite tutorial, we will work with constraints
Constraints are placed on columns. They limit the data, that can be inserted into tables.
We have the following constraints:
• NOT NULL
• PRIMARY KEY
• FOREIGN KEY
NOT NULL constraint
A column with a NOT NULL constraint, cannot have NULL values.
sqlite> CREATE TABLE People(Id integer, LastName text NOT NULL,
FirstName text NOT NULL, City text);
We create two columns with NOT NULL constraints.
sqlite> INSERT INTO People VALUES(1, 'Hanks', 'Robert', 'New York');
sqlite> INSERT INTO People VALUES(2, NULL, 'Marianne', 'Chicago');
SQL error: People.LastName may not be NULL
The first SELECT statement is executed OK, the second one fails. The SQL error says, the LastName
column may not be NULL.
The UNIQUE constraint ensures, that all data are unique in a column.
sqlite> CREATE TABLE Brands(Id integer, BrandName text UNIQUE);
Here we create a table Brands. The BrandName column is set to be UNIQUE. There cannot be two
brands with a same name.
sqlite> INSERT INTO Brands VALUES(1, 'Coca Cola');
sqlite> INSERT INTO Brands VALUES(2, 'Pepsi');
sqlite> INSERT INTO Brands VALUES(3, 'Pepsi');
SQL error: column BrandName is not unique
We get an SQL error 'column BrandName is not unique'. There can only be one Pepsi.
Note that a PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
The PRIMARY KEY constraint uniquely identifies each record in a database table. There can be more
UNIQUE columns, but only one primary key in a table. Primary keys are important when designing the
database tables. Primary keys are unique ids. We use them to refer to table rows. Primary keys become
foreign keys in other tables, when creating relations among tables. Due to to a 'long-standing coding
oversight', primary keys can be NULL in SQLite. This is not the case with other databases.
sqlite> CREATE TABLE Brands(Id integer PRIMARY KEY, BrandName text);
The Id column of the Brands table becomes a primary key.
sqlite> INSERT INTO Brands(BrandName) VALUES('Coca Cola');
sqlite> INSERT INTO Brands(BrandName) VALUES('Pepsi');
sqlite> INSERT INTO Brands(BrandName) VALUES('Sun');
sqlite> INSERT INTO Brands(BrandName) VALUES('Oracle');
sqlite> SELECT * FROM Brands;
1 Coca Cola
In SQLIte if a column is integer and primary key, it is also autoincrement.
A FOREIGN KEY in one table points to a PRIMARY KEY in another table. It is a referential constraint
between two tables. The foreign key identifies a column or a set of columns in one (referencing) table
that refers to a column or set of columns in another (referenced) table.
We will be demonstrating this constraint on two tables. Authors and Books.
CREATE TABLE Authors(AuthorId integer PRIMARY KEY, Name text);
Here we create the Authors table.
CREATE TABLE Books(BookId integer PRIMARY KEY, title text, AuthorId integer,
FOREIGN KEY(AuthorId) REFERENCES Authors(AuthorId));
We create the Books table. Here we have an AuthorId column name, which acts as a foreign key. It
references to the primary key of the Authors table.
In SQLite, foreign keys are not enforced by default. To enforce the foreign key, the library must be
compiled with proper flags, it must be at least version 3.6.19 and the pragma key for foreign keys must
What would foreign key enforcement mean in our example? We could not insert a row into the Books
table with an AuthorId, which is not present in Authors book.
A CHECK constraint is a condition that defines valid data of a relational database. The check is
executed when adding or updating data to the column in question.
sqlite> .schema Orders
CREATE TABLE Orders(Id integer PRIMARY KEY, OrderPrice integer
We look at the definition of the Orders table. We see a CHECK constraint imposed on the OrderPrice
column. Logically, the price of an order must be a positive value.
sqlite> INSERT INTO Orders(OrderPrice, Customer) VALUES(-10, 'Johnson');
SQL error: constraint failed
If we try to insert an invalid value, we get an error saying: 'constraint failed'.
The DEFAULT constraint inserts a default value into the column, if no value is available.
sqlite> CREATE TABLE Hotels(Id integer PRIMARY KEY, Name text,
City text DEFAULT 'not available');
To demonstrate the DEFAULT constraint, we create a Hotels table. The City column has a default 'not
sqlite> INSERT INTO Hotels(Name, City) VALUES('Kyjev', 'Bratislava');
sqlite> INSERT INTO Hotels(Name) VALUES('Slovan');
sqlite> SELECT * FROM Hotels;
Id Name City
--- ------- --------------
1 Kyjev Bratislava
2 Slovan not available
In the first statement, we provide both hotel name and city name. In the second statement, we provide
only the hotel name. SQLite puts the default value there, the 'not available' text.
In this part of the SQLite tutorial, we have worked with views, triggers and transactions in SQLite.