Conditional SQLite check constraint?

后端 未结 3 1640
忘了有多久
忘了有多久 2020-12-29 03:12

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,
          


        
3条回答
  •  猫巷女王i
    2020-12-29 03:30

    There's nothing stopping you from having multiple CHECK constraints on a single table. IMO the simplest and most easily expandable solution:

    CHECK (status IN ("Current", "Complete"))
    CHECK (status <> "Complete" OR enddate IS NOT NULL)
    

    This uses the fact that if A then B is logically equivalent to either not A or B.

提交回复
热议问题