clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 921212 fiddles created (11944 in the last week).

CREATE TABLE codes ( product_id INT NOT NULL, code INT NOT NULL, ucode BIT AS (CASE WHEN code = 11 THEN b'1' ELSE NULL END) VIRTUAL, -- PERSISTENT, CONSTRAINT code_11_product_id_unique UNIQUE (ucode, product_id) ) ;
 hidden batch(es)


insert into codes (product_id, code) values (1, 11), (1, 222), (1, 222), (1, 333), (2, 11), (2, 222), (2, 222);
 hidden batch(es)


select * from codes;
product_id code ucode
1 11 1
1 222
1 222
1 333
2 11 1
2 222
2 222
 hidden batch(es)


insert into codes -- should fail (product_id, code) values (2, 11) ;
Duplicate entry '\x01-2' for key 'code_11_product_id_unique'
 hidden batch(es)


select * from codes;
product_id code ucode
1 11 1
1 222
1 222
1 333
2 11 1
2 222
2 222
 hidden batch(es)