3

Say I have a table with a created, uploaded, processed and archived columns, all of which accept a date. However, I want it so the fields have to be updated in chronological order and you can't skip a step, but the trailing steps don't have to be set until they're ready (i.e., created <= uploaded <= processed <= archived). I implemented this like so:

create table batch (
  id        number primary key,
  created   date   default sysdate not null,
  uploaded  date,
  processed date,
  archived  date,

  check (uploaded  is null or uploaded  >= created),
  check (processed is null or processed >= uploaded),
  check (archived  is null or archived  >= processed)
);

This works, but it allows you to skip steps. For example, I can insert an arbitrary value for processed if uploaded is still null. I presume this to be because the null comparison is always vacuous. Do I therefore have to do something like:

create table batch (
  id        number primary key,
  created   date   default sysdate not null,
  uploaded  date,
  processed date,
  archived  date,

  check (uploaded  is null or uploaded >= created),
  check (processed is null or (uploaded is not null and processed >= uploaded)),
  check (archived  is null or (processed is not null and archived >= processed))
);

Is there a neater way?

1
  • 2
    I think your suggestion is fine although I would go for a different design. I would have a different table to handle the steps this way in the future if this workflow change it will be much more easy to adapt. And the workflow you would check with a trigger. Jul 17, 2014 at 12:56

3 Answers 3

2

I think your design is sound. I would split the checks for chronological order and the NULL checks, but this is only a question of style, not logic.

create table batch (
  id        number primary key,
  created   date   default sysdate not null,
  uploaded  date,
  processed date,
  archived  date,

  CHECK (created <= uploaded),
  CHECK (uploaded <= processed),
  CHECK (processed <= archived),

  CHECK (uploaded IS NOT NULL OR processed IS NULL),
  CHECK (processed IS NOT NULL OR archived IS NULL)
);
1

In the end, I went for my own solution:

create table batch (
  id        number primary key,
  created   date   default sysdate not null,
  uploaded  date,
  processed date,
  archived  date,

  check (uploaded  is null or uploaded >= created),
  check (processed is null or (uploaded is not null and processed >= uploaded)),
  check (archived  is null or (processed is not null and archived >= processed))
);

Granted that this constrains the workflow to this static schema, as mentioned by Jorge, but that's a tradeoff I'm willing to accept!

0

You could create 4 tables. One for each process.

  1. batches_created
  2. batches_uploaded
  3. batches_processed
  4. batches_archived
create table batches_created (  
  id        number primary key,  
  created   date   default sysdate not null  
);

create table batches_uploaded (  
  id        number primary key,  
  created   date   default sysdate not null,  
  constraint fk_batch_uploaded FOREIGN KEY( id ) REFERENCES batches_created( id )  
);

create table batches_processed (
  id        number primary key,  
  created   date   default sysdate not null,  
  constraint fk_batch_processed FOREIGN KEY( id ) REFERENCES batches_uploaded( id )  
);

create table batches_archived (  
  id        number primary key,  
  created   date   default sysdate not null,  
  constraint fk_batch_archived FOREIGN KEY( id ) REFERENCES batches_processed( id )  
);

So you can't have a batch archived without first create it, uploade it, and then process it.

3
  • Thank you for this...although I wouldn't necessarily call it "neater" ;) Jul 17, 2014 at 14:12
  • 1
    @Xophmeister: you have to add constraints to ensure there are not more than one batches_uploaded per batches_created etc...
    – wolφi
    Jul 17, 2014 at 14:32
  • I like downvote without comment. No chance to improve.
    – Luc M
    Jul 17, 2014 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.