This SQLite post explains how to create, add, and drop unique constraints in SQLite with syntax and examples.
A unique constraint is a single field or combination of fields that uniquely defines a record. Some of the fields can contain null values as long as the combination of values is unique.
Primary Key | Unique Constraint |
---|---|
None of the fields that are part of the primary key can contain a NULL value. (While the SQL-89 and SQL-92 standards do not allow a NULL value in a primary key, SQLite does allow a NULL under certain circumstances. We strongly recommend against using a NULL value in a primary key.) |
Some of the fields that are part of the unique constraint can contain NULL values as long as the combination of values is unique. |
A unique constraint can be created when you execute a CREATE TABLE statement in SQLite.
The syntax for creating a unique constraint using a CREATE TABLE statement in SQLite is:
The name of the table that you wish to create.
The columns that you wish to create in the table.
The name of the unique constraint.
The columns that make up the unique constraint.
Let's look at an example of how to create a unique constraint in SQLite.
In this example, we've created a unique constraint on the employees table called name_unique. It consists of the last_name and first_name fields.
If your table already exists and you wish to add a unique constraint later, you can not use the ALTER TABLE statement to add the unique constraint. Instead, you must create a new table with the unique constraint and copy the data into this new table.
The syntax to add a unique constraint to a table in SQLite is:
The name of the table to modify. This is the table that you wish to add a unique constraint to.
The name of the original table. It will be left behind after you have created the new table with the unique constraint added.
The name of the unique constraint.
The columns that make up the unique constraint.
Let's look at an example of how to add a unique constraint to an existing table in SQLite. So say, we already have a products table with the following definition:
And we wanted to add a unique constraint to the products table that consists of the product_name. We could run the following commands:
In this example, we've created a unique constraint on the products table called product_name_unique which consists of the product_name column. The original table will still exist in the database called old_products. You can drop the old_productstable once you have verified that your products table and data are as expected.
In SQLite, you can not use the ALTER TABLE statement to drop a unique constraint. Instead, you must create a new table with the unique constraint removed and copy the data into this new table.
The syntax to drop a unique constraint from a table in SQLite is:
The name of the table to modify. This is the table that you wish to remove the unique constraint from.
The name of the original table. It will be left behind after you have created the new table with the unique constraint removed.
Let's look at an example of how to remove a unique constraint from an existing table in SQLite. So say, we already have a suppliers table with the following definition:
And we wanted to drop the unique constraint from the suppliers table. We could run the following commands:
In this example, we've dropped the unique constraint on the existing suppliers table. The original table will still exist in the database called old_suppliers. You can drop the old_suppliers table once you have verified that your suppliers table and data are as expected.