![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
Andreas <maps.on (AT) gmx (DOT) 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 |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
|
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. Hi, A partial index would do the same, but requires less space: create unique index on log(state) WHERE state IN (0,1); |
#7
| |||
| |||
|
|
Marc Mamin <M.Mamin (AT) intershop (DOT) de> wrote: A partial index would do the same, but requires less space: create unique index on log(state) WHERE state IN (0,1); |

#8
| |||
| |||
|
|
Am 12.07.2012 07:14, schrieb Andreas Kretschmer: Marc Mamin <M.Mamin (AT) intershop (DOT) de> wrote: A partial index would do the same, but requires less space: create unique index on log(state) WHERE state IN (0,1); OK, nice ![]() What if I have those states in a 3rd table? So I can see a state-history of when a state got set by whom. objects ( id serial PK, ... ) events ( id serial PK, object_id integer FK on objects.id, ... ) event_states ( id serial PK, event_id integer FK on events.id, state integer ) There still should only be one event per object that has state 0 or 1. Though here I don't have the object-id within the event_states-table. Is it still possible to have a unique index that needs to span over a join of events and event_states? |
![]() |
| Thread Tools | |
| Display Modes | |
| |