pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Kretschmer <akretschmer@spamfence.net>
To: pgsql-sql@postgresql.org
Subject: Re: Prevent double entries ... no simple unique index
Date: Wed, 11 Jul 2012 10:24:58 +0200
Message-ID: <20120711082458.GA19013@tux> (raw)
In-Reply-To: <20120711081607.GA17798@tux>
References: <4FFD3050.3010509@gmx.net>
	<20120711081607.GA17798@tux>

Andreas Kretschmer <akretschmer@spamfence.net> wrote:

> Andreas <maps.on@gmx.net> wrote:
> 
> > Hi,
> >
> > I've got a log-table that records events regarding other objects.
> > Those events have a state that shows the progress of further work on  
> > this event.
> > They can be  open, accepted or rejected.
> >
> > I don't want to be able to insert addition events regarding an object X  
> > as long there is an open or accepted event.
> > On the other hand as soon as the current event gets rejected a new event  
> > should be possible.
> >
> > So there may be several rejected events at any time but no more than 1  
> > open or accepted entry.
> >
> > Can I do this within the DB so I don't have to trust the client app?
> >
> > The layout looks like this
> > Table : objects ( id serial, .... )
> >
> > Table : event_log ( id serial, oject_id integer references objects.id,  
> > state integer, date_created timestamp, ... )
> > where state is   0 = open, -1 = reject, 1 = accept
> 
> test=# create table log (state int not null, check (state in (-1,0,1)));
> CREATE TABLE
> Time: 37,527 ms
> test=*# commit;
> COMMIT
> Time: 0,556 ms
> test=# create unique index on log((case when state in (0,1) then 1 else
> null end));
> CREATE INDEX

Or this one:

test=*# create unique index on log((case when state = 0 then 0 when
state = 1 then 1 else null end));
CREATE INDEX


Now you can insert one '0' and one '1' - value - but no more.


Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



view thread (8+ messages)  latest in thread

Message-ID: <20120711082458.GA19013@tux>
Permalink:  ../20120711082458.GA19013@tux/
Also on:    postgresql.org/message-id/20120711082458.GA19013@tux

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: akretschmer@spamfence.net
  Subject: Re: Prevent double entries ... no simple unique index
  In-Reply-To: <20120711082458.GA19013@tux>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox