CREATE TABLE foo ( dt AS DATE NOT NULL, type AS TEXT NOT NULL, CONSTRAINT unique_dt_type UNIQUE(dt,type) -- check constraint(?) ) Having a brain-dud when trying to think of the right syntax to create a unique constraint when only a certain condition exists.
Given, type can have values A-F, there can only be one A per date, but there can be multiple B-F. Example of good table:
2010-01-02 | 'A' -- only one 2010-01-02 | 'B' -- can have multiple 2010-01-02 | 'B' 2010-01-02 | 'B' 2010-01-02 | 'C' -- can have multiple 2013-01-02 | 'A' -- only one 2010-01-02 | 'B' -- can have multiple 2010-01-02 | 'B' 2013-01-02 | 'F' -- can have multiple 2013-01-02 | 'F' Tried reading check/unique syntax but there weren't any examples. CHECK came close but only limited it to a range and wasn't used in conjunction with a UNIQUE scenario. Also tried searching, but my search skills are either not up to par, or there aren't any similar questions.
sql conditional unique, but no success (they were dated)whereis allowed during index creationCreate Unique Index ... Where ..., and it will be enforced like a constraint. (I'm on a phone, else I'd expand that into an answer)