Skip site navigation (1) Skip section navigation (2)

Re: Deleted Flag/Unique Constraint

From: "Bryan Murphy" <bryan(dot)murphy(at)gmail(dot)com>
To: "John D(dot) Burger" <john(at)mitre(dot)org>
Cc: "pgsql general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Deleted Flag/Unique Constraint
Date: 2007-03-30 00:31:10
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-general
I think the other guys suggestion will work better. ;)

Really, the table was just an example off the top of my head.  I believe we
do use a boolean as the deleted flag.  We primarily use it to track the user
who originally created an item (even if their account is deleted).  It's a
bit like a financial system where you can't retroactively change the data.
We always want to know who was associated with the original transaction,
even long after their account was deleted.
Thanks for the suggestion though!


On 3/29/07, John D. Burger <john(at)mitre(dot)org> wrote:
> On Mar 29, 2007, at 17:39, 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:
> >
> > (
> >   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?
> I gather you want the constraint enforced only if the item is not
> deleted - duplicate <key, value> pairs are fine if all but one are
> deleted, yes?  You can't do this with this table definition as is,
> but ...
> Unique constraints only apply to groups of column values that are non-
> null, so, if you'd be willing to do something like the following, I
> think you can get roughly what you want:
> CREATE TABLE keyValues  -- Dunno what name you wanted
> (
>    ID serial PRIMARY KEY,  -- Dunno what type you wanted
>    Key VARCHAR(32) NOT NULL,
>    Value VARCHAR(32) NOT NULL,
>    notDeleted Boolean DEFAULT True,
>    UNIQUE (Key, Value, notDeleted),
>    CHECK (notDeleted = True)
> );
> I've changed your flag to be a boolean - this is not strictly
> necessary, but conveys its semantics better, I think.  More
> importantly, I've reversed its polarity, and arranged for it to only
> have values of True or NULL.  When it is NULL, which we can interpret
> as meaning it is not notDeleted, i.e., it is deleted, the unique
> constraint will not apply.
> This is an abuse of NULL, and means that your app will have to set
> the flag to NULL, rather than False, but I believe it satisfies your
> requirements.  You could hide the abuse behind a view, if necessary.
> - John Burger
>    MITRE
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings

In response to


pgsql-general by date

Next:From: Ron JohnsonDate: 2007-03-30 00:58:33
Subject: Re: COPY command details
Previous:From: Bryan MurphyDate: 2007-03-30 00:26:51
Subject: Re: Deleted Flag/Unique Constraint

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group