![]() ![]() ![]() Therefore, you should check the pg_stat_activity table to see the current pending transactions that are on-going using the following query: SELECTĬode language: SQL (Structured Query Language) ( sql ) If you have any pending transactions, it will wait for all transactions to complete before changing the table. Notice that the ALTER TABLE statement acquires an exclusive lock on the table. UNIQUE USING INDEX equipment_equip_id Code language: SQL (Structured Query Language) ( sql ) Third, add a unique constraint to the equipment table using the equipment_equip_id index. ON equipment (equip_id) Code language: SQL (Structured Query Language) ( sql ) CREATE UNIQUE INDEX CONCURRENTLY equipment_equip_id Second, create a unique index based on the equip_id column. Let’s take a look at the following example.įirst, suppose you have a table named equipment: CREATE TABLE equipment ( Sometimes, you may want to add a unique constraint to an existing column or group of columns. Adding unique constraint using a unique index The value of the column c2 or c3 needs not to be unique. The combination of values in column c2 and c3 will be unique across the whole table. PostgreSQL allows you to create a UNIQUE constraint to a group of columns using the following syntax: CREATE TABLE table ( Code language: SQL (Structured Query Language) ( sql ) Creating a UNIQUE constraint on multiple columns ERROR: duplicate key value violates unique constraint "person_email_key"ĭETAIL: Key already exists. VALUES( 'jack', 'doe', Code language: SQL (Structured Query Language) ( sql ) INSERT INTO person(first_name,last_name,email) Second, insert another row with duplicate email. VALUES( 'john', 'doe', Code language: SQL (Structured Query Language) ( sql ) Note that the UNIQUE constraint above can be rewritten as a table constraint as shown in the following query: CREATE TABLE person (įirst, insert a new row into the person table using INSERT statement: INSERT INTO person(first_name,last_name,email) ) Code language: SQL (Structured Query Language) ( sql ) The following statement creates a new table named person with a UNIQUE constraint for the email column. When you add a UNIQUE constraint to a column or a group of columns, PostgreSQL will automatically create a unique index on the column or the group of columns. It rejects the change and issues an error if the value already exists. The same process is carried out for updating existing data. When a UNIQUE constraint is in place, every time you insert a new row, it checks if the value is already in the table. PostgreSQL provides you with the UNIQUE constraint that maintains the uniqueness of the data correctly. 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 addresses or usernames. 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. ![]()
0 Comments
Leave a Reply. |