0% found this document useful (0 votes)
118 views3 pages

PostgreSQL UNIQUE Constraint

This document discusses PostgreSQL UNIQUE constraints which ensure that values stored in a column or group of columns are unique across table rows. It provides an example of adding a UNIQUE constraint to an "email" column to prevent duplicate email entries. It also demonstrates how to create a UNIQUE constraint for multiple columns and how to add a UNIQUE constraint to an existing table by creating a unique index.

Uploaded by

Lord_King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

PostgreSQL UNIQUE Constraint

This document discusses PostgreSQL UNIQUE constraints which ensure that values stored in a column or group of columns are unique across table rows. It provides an example of adding a UNIQUE constraint to an "email" column to prevent duplicate email entries. It also demonstrates how to create a UNIQUE constraint for multiple columns and how to add a UNIQUE constraint to an existing table by creating a unique index.

Uploaded by

Lord_King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PostgreSQL UNIQUE Constraint

Summary: in this tutorial, you will learn about PostgreSQL UNIQUE constraint to make
sure that values stored in a column or a group of columns are unique across rows in a
table.

Sometimes, you want to ensure that values stored in a column or a group of columns are
unique across the whole table such as email address and username. PostgreSQL
provides you with the UNIQUE constraint to make sure that the uniqueness of the data is
maintained correctly.

With the UNIQUE constraint, every time you insert a new row, PostgreSQL checks if the
value is already in the table. If it finds that the new value is already there, it rejects the
change and issues an error. The same process is carried out for updating existing data.

When you add a UNIQUE constraint to a column or a group of columns, PostgreSQL will
create a unique index on the respective column or a group of columns automatically.

PostgreSQL UNIQUE constraint example


The following statement creates a new table named person with a UNIQUE constraint
for the email column.

1 CREATE TABLE person (


2 id serial PRIMARY KEY,
3 first_name VARCHAR (50),
4 last_name VARCHAR (50),
5 email VARCHAR (50) UNIQUE
6 );

Note that the UNIQUE constraint above can be rewritten as a table constraint as shown
in the following query:

1 CREATE TABLE person (


2 id SERIAL PRIMARY KEY,
3 first_name VARCHAR (50),
4 last_name VARCHAR (50),
5 email VARCHAR (50),
6 UNIQUE(email)
7 );

First, we insert a new row into the person table using INSERT statement:

1 INSERT INTO person(first_name,last_name,email)


2 VALUES
3 (
4 'john',
5 'doe',
6 '[email protected]'
7 );

Second, we insert another row with duplicate email.

1 INSERT INTO person(first_name,last_name,email)


2 VALUES
3 (
4 'jack',
5 'doe',
6 '[email protected]'
7 );

PostgreSQL issued an error message.

1 [Err] ERROR: duplicate key value violates unique constraint "person_email_key"


2 DETAIL: Key (email)=([email protected]) already exists.

Creating a UNIQUE constraint on multiple columns


PostgreSQL allows you to create a UNIQUE constraint to a group of columns using the
following syntax:

1 CREATE TABLE table (


2 c1 data_type,
3 c2 data_type,
4 c3 data_type,
5 UNIQUE (c2, c3)
6 );

The combination of values in column c2 and c3 will be unique across the whole table.
The value of the column c2 or c3 needs not to be unique.

Adding unique constraint using a unique index


Sometimes, you may want to add a unique constraint to an existing column or group of
columns. Let’s take a look at the following example.

First, suppose we have a table named equipment:

1 CREATE TABLE equipment (


2 id serial PRIMARY KEY,
3 name VARCHAR (50) NOT NULL,
4 equip_id VARCHAR (16) NOT NULL
5 );
Second, we create a unique index based on the equip_id column.

1 CREATE UNIQUE INDEX CONCURRENTLY equipment_equip_id


2 ON equipment (equip_id);

Third, we add a unique constraint to the equipment table using the


equipment_equip_id index.

1 ALTER TABLE equipment


2 ADD CONSTRAINT unique_equip_id
3 UNIQUE USING INDEX equipment_equip_id;

Notice that the ALTER TABLE statement acquires an exclusive lock on the table. If you
have any pending transactions, it will wait for all transactions to complete before
changing the table. Therefore, you should check the pg_stat_activity table to see
the current pending transactions that are on-going using the following query:

1 SELECT
2 datid,
3 datname,
4 usename,
5 state
6 FROM
7 pg_stat_activity;

You should look at the result to find the state column with the value idle in
transaction. Those are the transactions that are pending to complete.

In this tutorial, we explained to you about the UNIQUE constraint to make values stored
in a column or a group of columns unique across the rows of a table.

Related Tutorials
 PostgreSQL CREATE TABLE
 PostgreSQL Primary Key
 PostgreSQL Foreign Key
 PostgreSQL CHECK Constraint
 PostgreSQL Not-Null Constraint

You might also like