i have this table:
id | product_group_id | percentage
1 | 1 | 0.2
2 | 1 | 0.3
3 | 1 | 0.5
4 | 2 | 0.4
5 | 2 | 0.6
I want to know if there is a way to create constraints that for each product_group_id the percentage will be equal to 1 ( if product_group_id = 1 so 0.2 + 0.3 + 0.5 = 1).
For example if i will change the percentage where id = 2 to 0.8 or 0.1, the update (or insert) will fail because the sum is not 1.
Thanks in advanced.
It would generally make more sense to enforce this sort of constraint in the API you build on top of the table to manipulate the percentages. Assuming, of course, that each
product_group_idwill be manipulated by at most one session at a time.If you do really want to enforce this sort of restriction, you could create a materialized view that refreshes on commit and create a constraint on that materialized view. Something like
That will allow you to insert rows that sum to 1
and will throw an error when you try to commit changes that cause the sum to be something other than 1
Note that this is checked at commit time which is what you really need since you’ll need to violate the constraint during a transaction when you want to insert multiple rows.
And, as has been pointed out, if there is the possibility of rounding errors, you probably want the
CHECKconstraint to allow sums that are 1 +/- some small epsilon (i.e between 0.999999 and 1.000001)