Skip to content

Instantly share code, notes, and snippets.

@pfote
Created September 6, 2012 12:18
Show Gist options
  • Save pfote/3655665 to your computer and use it in GitHub Desktop.
Save pfote/3655665 to your computer and use it in GitHub Desktop.
postgres .. unique index on two fields
test=# create table j (id1 int, id2 int);
CREATE TABLE
test=*# create unique index idx_j on j(greatest(id1, id2), least(id1, id2));
CREATE INDEX
test=*# insert into j values (1,2);
INSERT 0 1
test=*# insert into j values (2,1);
ERROR:  duplicate key violates unique constraint "idx_j"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment