PostgreSQL UNIQUE Index
Summary: in this tutorial, you will learn how to create a PostgreSQL unique index to ensure the uniqueness of values in one or more columns.
Introduction to PostgreSQL UNIQUE index
The PostgreSQL unique index enforces the uniqueness of values in one or multiple columns.
To create a unique index, you use the following CREATE UNIQUE INDEX
statement:
In this syntax:
- First, specify the index name in the
CREATE UNIQUE INDEX
statement. - Second, provide the name of the table along with a list of indexed columns in the ON clause.
- Third, the
NULL NOT DISTINCT
option treats nulls as equal, whereasNULLS DISTINCT
treats nulls as distinct values. By default, the statement usesNULLS DISTINCT
, meaning that the indexed column may contain multiple nulls.
PostgreSQL offers multiple index types, but only the B-tree index type supports unique indexes.
When you define a unique index for a column, the column cannot store multiple rows with the same values.
If you define a unique index for two or more columns, the combined values in these columns cannot be duplicated in multiple rows.
When you define a primary key or a unique constraint for a table, PostgreSQL automatically creates a corresponding unique index.
PostgreSQL UNIQUE index examples
Let’s explore some examples of using the PostgreSQL unique indexes.
1) Unique indexes for a primary key column and a column with a unique constraint
First, create a table called employees
:
In this statement, the employee_id
is the primary key column and email
column has a unique constraint, therefore, PostgreSQL creates two UNIQUE
indexes, one for each column.
Second, show the indexes of the employees
table:
Here is the output:
2) Using PostgreSQL UNIQUE index for single column example
First, add a column named mobile_phone
to the employees
table:
To ensure that the mobile phone numbers are distinct for all employees, you can define a unique index for the mobile_phone
column using the CREATE INDEX
statement.
Second, create a unique index on the mobile_phone
column of the employees
table:
Third, insert a new row into the employees
table:
Fourth, attempt to insert another row with the same phone number:
PostgreSQL issues the following error due to the duplicate mobile phone number:
3) Using PostgreSQL UNIQUE index for multiple columns
First, add two new columns called work_phone
and extension
to the employees
table:
Multiple employees can share the same work phone number. However, they cannot have the same extension number.
To enforce this rule, you can define a unique index on both work_phone
and extension
columns.
Next, create a unique index that includes both work_phone
and extension
columns:
Then, insert a row into the employees
table:
After that, insert another employee with the same work phone number but a different extension:
The statement works because the combination of values in the work_phone
and extension
column is unique.
Finally, attempt to insert a row with the same values in both work_phone
and extension
columns that already exist in the employees
table:
PostgreSQL issued the following error:
Summary
- Use a PostgreSQL unique index to enforce the uniqueness of values in a column or a set of columns.
- PostgreSQL automatically creates a unique index for a primary key column or a column with a unique constraint.