Re: Deleted Flag/Unique Constraint - Mailing list pgsql-general

From Bryan Murphy
Subject Re: Deleted Flag/Unique Constraint
Date
Msg-id bd8531800703291726t25f80f78x4f16d6c21b4472c@mail.gmail.com
Whole thread Raw
In response to Re: Deleted Flag/Unique Constraint  (Jonathan Hedstrom <jhedstrom@desc.org>)
Responses Re: Deleted Flag/Unique Constraint  (Lew <lew@nospam.lewscanon.com>)
List pgsql-general
Thanks!  That works great!

Bryan

On 3/29/07, Jonathan Hedstrom <jhedstrom@desc.org> wrote:
Bryan Murphy wrote:

> Is it possible to declare a unique constraint in combination with a
> deleted flag?
>
> For example, if I have a table like this:
>
> CREATE TABLE
> (
>   ID NOT NULL PRIMARY KEY,
>   Key VARCHAR(32) NOT NULL,
>   Value VARCHAR(32) NOT NULL,
>   Deleted INT NOT NULL DEFAULT 0
> );
>
> can I declare a unique constraint that only checks Key and Value when
> Deleted = 0?

Yes, something like this:

CREATE UNIQUE INDEX index_name ON table_name ( unique_field) WHERE
deleted=0;

-Jonathan

pgsql-general by date:

Previous
From: Jonathan Hedstrom
Date:
Subject: Re: Deleted Flag/Unique Constraint
Next
From: "Bryan Murphy"
Date:
Subject: Re: Deleted Flag/Unique Constraint