ChatGPT解决这个技术问题 Extra ChatGPT

In Postgresql, force unique on combination of two columns

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
As this is a top rank search result in google, Maybe better to provide alter exist table too

S
S.R
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)
)

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
How can I implement this in schema definition?
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.
Is it possible to enforce a unique constraint on just one permutation, such as unique(col1, col2 = '1')?
P
PearsonArtPhoto

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

ALTER TABLE someTable
ADD UNIQUE (col1, col2)

T
Timur Sadykov

Seems like regular UNIQUE CONSTRAINT :)

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

More here


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.
@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.
T
Tay Hess

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