![]() ![]() 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. If no value is provided for this column, the DEFAULT VALUE will be 'Standard shipping'.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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |