0

Suppose I have a table with two columns:

| id | status | 

Status can take 3 values: booked, started, completed

I need a constraint that will satisfy the following requirement:

For entire set of rows, only one record can be in non completed state: booked or started.

I believe I need an Exclude constraint for that, but I don't know what operator can be specified:

add constraint constraint_x exclude (status with ANY?) where (status <> 'Completed') 

That's the solution I came up to:

add constraint constraint_x exclude ((1) with =) where ((status) <> 'Completed') 

Is this a good one?

2 Answers 2

1

Your solution is ok.

I would solve that with a partial unique index like this:

CREATE UNIQUE INDEX ON table_name ((1)) WHERE status <> 'Completed'; 
Sign up to request clarification or add additional context in comments.

Comments

0

That's the solution I came up to:

add constraint constraint_x exclude ((1) with =) where ((status) <> 'Completed') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.