This SQLite post explains how to create, add, and drop a primary key in SQLite with syntax and examples.
In SQLite, a primary key is a single field or combination of fields that uniquely defines a record. A table can have only one primary key.
A primary key can be created when you execute a CREATE TABLE statement in SQLite.
The syntax to create a primary key using the CREATE TABLE statement in SQLite is:
OR
The name of the table that you wish to create.
The columns that you wish to create in the table.
The name of the primary key.
The columns that make up the primary key.
Let's look at an example of how to create a primary key using the CREATE TABLE statement in SQLite. We will start with a very simple one where our primary key consists of just one column.
For example:
In this example, we've created a primary key on the employees table called employees_pk. It consists of only one column - the employee_id column.
We could have used the alternate syntax and created this same primary key as follows:
Both of these syntaxes are valid when creating a primary key with only one field.
If you create a primary key that is made up of 2 or more columns, you are limited to using only the first syntax where the primary key is defined at the end of the CREATE TABLE statement.
For example:
This example creates a primary key on the customers table called customers_pk that is made up of a combination of the last_name and first_name columns. So each combination of last_name and first_name must be unique in the customers table.
If your table already exists and you wish to add a primary key later, you can not use the ALTER TABLE statement to create a primary key. Instead, you must create a new table with the primary key and copy the data into this new table.
The syntax to add a primary key to a table in SQLite is:
The name of the table to modify. This is the table that you wish to add a primary key to.
The name of the original table that will be left behind after you have created the new table with the primary key added.
The name of the primary key.
The columns that make up the primary key.
Let's look at an example of how to add a primary key to an existing table in SQLite. So say, we already have an employees table with the following definition:
And we wanted to add a primary key to the employees table that consists of the employee_id. We could run the following commands:
In this example, we've created a primary key on the employees table called employees_pk which consists of the employee_id column. The original table will still exist in the database called old_employees. You can drop the old_employees table once you have verified that your employees table and data are as expected.
In SQLite, you can not use the ALTER TABLE statement to drop a primary key. Instead, you must create a new table with the primary key removed and copy the data into this new table.
The syntax to drop a primary key from a table in SQLite is:
The name of the table to modify. This is the table that you wish to remove the primary key from.
The name of the original table that will be left behind after you have created the new table with the primary key removed.
Let's look at an example of how to remove a primary key from an existing table in SQLite. So say, we already have a suppliers table with the following definition:
And we wanted to drop a primary key from the suppliers table. We could run the following commands:
In this example, we've dropped the primary key 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.