Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>IMHO, this basically is a normalisation problem. The column named "id" does not uniquely address a row, so it can never be a PK. At least a new (surrogate) key(element) is needed. The constraint itself cannot be expressed as an expression "within the row", so it has to be expressed in terms of a FK.</p> <p>So it breaks down into two tables: One with PK=id, and a FK REFERENCING two.sid</p> <p>Two with PK= surrogate key, and FK id REFERENCING one.id The original payload "value" also lives here.</p> <p>The "one bit variable" disappears, because it can be expressed in terms of EXISTS. (effectively table one points to the row that holds the token)</p> <p>[I expect the Postgres rule system could be used to use the above two-tables-model to emulate the intended behaviour of the OP. But that would be an ugly hack...]</p> <p>EDIT/UPDATE:</p> <p>Postgres supports partial/conditional indices. (don't know about ms-sql)</p> <pre><code>DROP TABLE tmp.one; CREATE TABLE tmp.one ( sid INTEGER NOT NULL PRIMARY KEY -- surrogate key , id INTEGER NOT NULL , status INTEGER NOT NULL DEFAULT '0' /* ... payload */ ); INSERT INTO tmp.one(sid,id,status) VALUES (1,1,0) , (2,1,1) , (3,1,0) , (4,2,0) , (5,2,0) , (6,2,1) , (7,3,0) , (8,3,0) , (9,3,1) ; CREATE UNIQUE INDEX only_one_non_zero ON tmp.one (id) WHERE status &gt; 0 -- "partial index" ; \echo this should succeed BEGIN ; UPDATE tmp.one SET status = 0 WHERE sid=2; UPDATE tmp.one SET status = 1 WHERE sid=1; COMMIT; \echo this should fail BEGIN ; UPDATE tmp.one SET status = 1 WHERE sid=4; UPDATE tmp.one SET status = 0 WHERE sid=9; COMMIT; SELECT * FROM tmp.one ORDER BY sid; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload