Ignacio Perez
Ignacio Perez

Reputation: 2361

Postgres how can I make 2 columns unique

Hello I am new to postgres and know how to make a column unique however I need to make 2 pairs of columns unique (City,State). I have a table where people insert data different cities and states and I would like to make that pair unique. For Example: Dallas,Texas Houston,Texas Dallas,Texas : This should cause an error or not be allowed because the pair of (Dallas,Texas) is not unique. I am using postgres 9.4 and pgadmin any suggestions would be great

Upvotes: 0

Views: 254

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

You can create a unique index on the pair:

create unique index idx_table_city_state on table(city, state);

You can also use a unique constraint in the table definition.

Upvotes: 5

Related Questions