Conditional SQLite check constraint?

Rezzie picture Rezzie · Apr 11, 2010 · Viewed 34.8k times · Source

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.

Answer

Andomar picture Andomar · Apr 11, 2010

How about:

CHECK (status = "Current" or (status = "Complete" and enddate is not null))