views:

285

answers:

1

I have a table defined by the following SQL:

CREATE TABLE test (
  id       integer PRIMARY KEY NOT NULL UNIQUE,
  status   text NOT NULL,
  enddate  date,
  /* Checks */
  CHECK (status IN ("Current", "Complete"))
);

I'd like to add a constraint that requires enddate to be non-null if the status is "Complete".

Is this possible? I am using SQLite v3.6.16.

+1  A: 

How about:

CHECK (status = "Current" or (status = "Complete" and enddate is not null))
Andomar
Thanks, this works. Is this the only way? In future I may have a larger number of statuses and the check could grow quite large.
Rezzie
When constraints get very complex, the approach is typically to put a layer in front of the table to enforce it. Like a stored procedure layer, or a data access library.
Andomar
Thanks for the advice.
Rezzie