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
Asked
Active
Viewed 236 times
0
-
1http://stackoverflow.com/questions/1194438/can-i-add-a-unique-constraint-to-a-postgresql-table-after-its-already-created – Pரதீப் Sep 24 '15 at 15:14
-
Thanks for the link @Indian will look at it now – Ignacio Perez Sep 24 '15 at 15:17
1 Answers
5
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.

Gordon Linoff
- 1,242,037
- 58
- 646
- 786