1
CREATE TABLE employee (
    id varchar(40) NOT NULL,
    legacy_id varchar(40) NOT NULL,
    short_name varchar(255) NULL,   
    is_deleted bool NOT NULL DEFAULT false,
    CONSTRAINT employee_pkey PRIMARY KEY (id)
)

I want to plan for unique constraint like this

The table must have these constraint

1. Unique constraint is required on column 'legacy_id' and 'is_deleted = false'. Its okay to have multiple values with legacy_id and is_deleted = true. 

Is there any way to achieve this

1 Answer 1

1

You cannot have a constraint for that, but a partial unique index will do just as well:

CREATE UNIQUE INDEX ON employee (legacy_id)
   WHERE is_deleted;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.