patternsqlMinor
Add constraint to ensure flag column is <= sum of column in other table
Viewed 0 times
sumcolumnflagensureconstraintothertableadd
Problem
I have a column RSEFlag that contains a flag value that is the sum of one or more of 1, 2, 4. I have separate table that contains the description of each of those flag values:
I would like to constrain my RSEFlag column so that it is less than or equal to the sum of values from the RSE table.
Is this a worthwhile exercise and what is the best approach?
RSECode RSEDescription
------- --------------
4 Environment
1 Reliability
2 SafetyI would like to constrain my RSEFlag column so that it is less than or equal to the sum of values from the RSE table.
Is this a worthwhile exercise and what is the best approach?
Solution
- You can use a
CHECKconstraint defined against a UDF to enforce this, but this comes with many caveats.
- You can also define
INSERTandUPDATEtriggers to do this same work.
-
In Oracle, you could create a materialized view that joined the two tables together and placed your
RSEFlag column next to the appropriate sum. Then you would apply a CHECK constraint to enforce that `RSEFlagContext
StackExchange Database Administrators Q#6234, answer score: 4
Revisions (0)
No revisions yet.