395

I would like to set up a table in PostgreSQL such that two columns together must be unique. There can be multiple values of either value, so long as there are not two that share both.

For instance:

CREATE TABLE someTable (
    id int PRIMARY KEY AUTOINCREMENT,
    col1 int NOT NULL,
    col2 int NOT NULL
)

So, col1 and col2 can repeat, but not at the same time. So, this would be allowed (Not including the id)

1 1
1 2
2 1
2 2

but not this:

1 1
1 2
1 1 -- would reject this insert for violating constraints
1
  • 6
    As this is a top rank search result in google, Maybe better to provide alter exist table too
    – Mamrezo
    Commented Jan 21, 2020 at 14:15

4 Answers 4

490
CREATE TABLE someTable (
    id serial PRIMARY KEY,
    col1 int NOT NULL,
    col2 int NOT NULL,
    UNIQUE (col1, col2)
)

autoincrement is not postgresql. You want a integer primary key generated always as identity (or serial if you use PG 9 or lower. serial was soft-deprecated in PG 10).

If col1 and col2 make a unique and can't be null then they make a good primary key:

CREATE TABLE someTable (
    col1 int NOT NULL,
    col2 int NOT NULL,
    PRIMARY KEY (col1, col2)
)
9
  • 10
    I like the suggestion of a primary key over unique here, because we do not allow NULL values in this case. From the PostgeSQL docs: "Note that a unique constraint does not, by itself, provide a unique identifier because it does not exclude null values.)" postgresql.org/docs/8.1/static/ddl-constraints.html#AEN2038
    – ndequeker
    Commented Feb 3, 2017 at 13:53
  • 10
    In some scenarios you might want a surrogate key to be used as a primary key rather than a combination of columns. In particular to improve performance when doing joins on big data volumes. I personally went for the UNIQUE CONSTRAINT solution below. Commented Jan 13, 2019 at 7:54
  • 1
    Is it possible to enforce a unique constraint on just one permutation, such as unique(col1, col2 = '1')? Commented Nov 12, 2019 at 19:20
  • 1
    But don’t sequential integers as primary keys outperform combinations as primary keys in indexing? Performance-wise. Commented Apr 2, 2022 at 13:21
  • 1
    Not sure I understand @awm's question. If col1 is unique, and col2 is unique, then col1+col2 is also unique, so there would be no need for a composite key or constraint.
    – SteveW
    Commented Oct 5, 2023 at 19:14
301

Create unique constraint that two numbers together CANNOT together be repeated:

ALTER TABLE someTable
ADD UNIQUE (col1, col2)
45

If, like me, you landed here with:

  • a pre-existing table,
  • to which you need to add a new column, and
  • also need to add a new unique constraint on the new column as well as an old one, AND
  • be able to undo it all (i.e. write a down migration)

Here is what worked for me, utilizing one of the above answers and expanding it:

-- up

ALTER TABLE myoldtable ADD COLUMN newcolumn TEXT;
ALTER TABLE myoldtable ADD CONSTRAINT myoldtable_oldcolumn_newcolumn_key UNIQUE (oldcolumn, newcolumn);

---

ALTER TABLE myoldtable DROP CONSTRAINT myoldtable_oldcolumn_newcolumn_key;
ALTER TABLE myoldtable DROP COLUMN newcolumn;

-- down
1
  • The manual DROP CONSTRAINT doesn't seem to be necessary. By dropping the column, the constraint also disappears. Commented Aug 22 at 7:17
38

Seems like regular UNIQUE CONSTRAINT :)

CREATE TABLE example (
a integer,
b integer,
c integer,
UNIQUE (a, c));

More here

2
  • 1
    Does this add an index for a and an index for c independently? Because I need to quickly find based on a sometimes, and quickly find based on c sometimes. Commented Sep 16, 2019 at 13:08
  • @CMCDragonkai This index on a,c internally serve for two types of queries. A query with 'a' as where clause and a query with columns a and c present in the where clause will be benefited from the above index. In both ways, column 'a' should be present, as it's the look-up column. Commented Aug 13, 2021 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.