Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 70CF417A3624 for ; Wed, 11 Jul 2012 04:50:56 -0300 (ADT) Received: from mailout-de.gmx.net ([213.165.64.22]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1SorhF-0007Pw-JS for pgsql-sql@postgresql.org; Wed, 11 Jul 2012 07:50:55 +0000 Received: (qmail invoked by alias); 11 Jul 2012 07:50:39 -0000 Received: from mue-88-130-21-236.dsl.tropolys.de (EHLO [192.168.1.113]) [88.130.21.236] by mail.gmx.net (mp032) with SMTP; 11 Jul 2012 09:50:39 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX18UC/d5ulMUO/ZhIjWJLF+Z4F4bMgxoY6Wz9IKo3n VPf61Cj6epHKQf Message-ID: <4FFD3050.3010509@gmx.net> Date: Wed, 11 Jul 2012 09:50:40 +0200 From: Andreas User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Prevent double entries ... no simple unique index Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201207/5 X-Sequence-Number: 36742 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 I can't simply move rejected events in an archive table and keep a unique index on object_id as there are other descriptive tables that reference the event_log.id.