Hi,
Is there a way to write a unique constraint/index on a table such that you can restrict a column to have a flag in at least one record in a table.
An example table would be:
ID Category Flag
1 A 0
2 A 1
3 B 1
4 B 0
My requirement is that at least one record in each category always have Flag = 1. But there could be a record in another category that has a flag = 1 as well. I want to prevent a category having no records that are marked with a flag. It's basically to provide the ability to identify one record in the category as a primary record that can be referenced elsewhere.
I could do this with a function called by a constraint if I understand correctly (i.e. write some function that runs an aggregate count to make sure there is always 1 record in the count), but that has a performance hit, so I don't really want to do that. But it does not appear that you can do it any other way through constraints or indices.
Any other clever way of doing it?
Thanks,
Reinis