MySQL
perfectly supports unique constraints.
It does not support partial constraints/indexes, though, so you would need to mark non-primary images with a NULL
instead of 0
.
ALTER TABLE file ADD CONSTRAINT ux_file_customer_primary
UNIQUE (customerId, isPrimaryImage)
You can insert arbitrary number of NULL
values into isPrimaryImage
but only one non-null value per customer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…