PostgreSQL Primary Key
Summary: in this tutorial, you will learn about the PostgreSQL primary key and how to manage primary key constraints effectively.
Introduction to PostgreSQL primary key
A primary key is a column or a group of columns used to uniquely identify a row in a table. The column that participates in the primary key is known as the primary key column.
A table can have zero or one primary key. It cannot have more than one primary key.
It is a good practice to add a primary key to every table. When you add a primary key to a table, PostgreSQL creates a unique B-tree index on the column or a group of columns used to define the primary key.
Technically, a primary key constraint is the combination of a not-null constraint and a UNIQUE constraint.
Typically, you define primary for a table when creating it:
In this syntax, you define the primary key as the column constraint of the primary key column.
If the primary key consists of more than one column, you can define it using the table constraint:
To add a primary key to an existing table, you the ALTER TABLE ... ADD PRIMARY KEY
statement:
If you don’t explicitly specify the name for the primary key constraint, PostgreSQL will assign a default name to the primary key constraint.
By default, PostgreSQL uses the format table-name_pkey
as the default name for the primary key constraint.
To assign a name for the primary key, you can use the CONSTRAINT
clause as follows:
PostgreSQL primary key examples
Let’s explore some examples of using the PostgreSQL primary key.
1) Creating a table with a primary key that consists of one column
The following statement creates a table with a primary key that consists of one column:
In this example, we create the orders with the order_id as the primary key.
We define the order_id column with the type SERIAL so that PostgreSQL will generate a unique integer (1, 2, 3, and so on) when you insert a new row into the table without providing the value for the order_id column.
This ensures the value in the order_id is unique for every row in the table.
2) Creating a table with a primary key that consists of two columns
The following example shows how to define a primary key that consists of two columns order_id
and item_no
:
3) Adding a primary key to an existing table
First, create a table called products
without defining any primary key.
Second, add a primary key constraint to the products
table using the ALTER TABLE ... ADD PRIMARY KEY
statement:
4) Adding an auto-incremented primary key to an existing table
First, create a new table called vendors
that does not have a primary key:
Second, insert some rows into the vendors
table:
Output:
Third, add a primary key named vendor_id
into the vendors
table with the type SERIAL
:
Finally, verify the vendor_id column:
Output:
Drop a primary key
To remove a primary key from a table, you use the following ALTER TABLE
statement:
In this syntax:
- First, specify the name of the table from which you want to remove the primary key.
- Second, provide the primary key constraint to drop.
Let’s take an example of removing the primary key constraint from the vendors
table using psql.
First, display the structure of the vendors
table using the \d command:
Output:
The output indicates that the primary key constraint is vendors_pkey.
Second, drop the primary key from the vendors
table using the ALTER TABLE ... DROP CONSTRAINT
statement:
Output:
The statement removes only the primary key constraint but does not remove the vendor_id column:
Output:
Summary
- Use the
PRIMARY KEY
constraint to define a primary key for a table when creating the table. - Use the
ALTER TABLE ... ADD PRIMARY KEY
statement to add a primary key to a table. - Use the
ALTER TABLE ... DROP CONSTRAINT
statement to drop a primary key from a table.