Let’s say I have a database with columns name
, last_name
and type
They’re all strings, though type can only be TYPE_A
, TYPE_B
or TYPE_C
I want a row in the database to be Unique
when name
and last_name
already exist.
Additionally, I want TYPE_A
and TYPE_B
to be treated as the same in uniqueness and TYPE
C to be treated differently. Essentially, I want either TYPE_A
or TYPE_B
to be seen as TYPE_A_B
. I don’t want to modify any values already present in the database.
I only want to add this constraint to an already existing database, how do I do this?
(I’m using Postgres)
For example: (name, last_name, type)
Valid:
John, Doe Type A
John, Doe Type C
Invalid (Unique constraint violation)
John, Cena Type C
John, Doe Type A
<-
John, Doe Type B
<-
2
Answers
Postgres’ constraints do not support expressions, but you can achieve the same goal with a unique index:
fiddle
If you wanted to enforce unicity on types A/B only, ignoring other types, then you would use a filtering unique index instead:
Create a functional unique index:
(The additional parentheses around the
CASE
expression are mandatory here.)Demo: https://dbfiddle.uk/rIzYyU-n