mercredi 28 janvier 2015

How to enforce an "exactly-one-per-group" constraint?


I have a table x that I've defined like this:



CREATE TABLE x (
xid INTEGER NOT NULL PRIMARY KEY,
yid INTEGER NOT NULL REFERENCES y(yid),
is_principal BOOLEAN NOT NULL
);


This definition misses one constraint that x must satisfy. In English, this constraint could be described like this:



there may be one or more rows with a given value in the yid field, but among them there must always be exactly one whose is_principal field is TRUE1.



I'm looking for a way to enforce this constraint.


(In case it matters, I'm particularly interested in solutions applicable to SQLite3 and PostgreSQL.)




1 Another way to express the same constraint would be to say that the following two queries should always produce identical outputs:



SELECT DISTINCT (yid) FROM x ORDER BY yid;
SELECT yid FROM x WHERE is_principal ORDER BY yid;




Aucun commentaire:

Enregistrer un commentaire