CHECK constraint v/s EXCLUSION constraint
Posted on 22 Jul, 2022
CHECK
constraint
CHECK
constraintCHECK checks whether a record being entered matches the boolean criteria or not. If the record violates that condition, it is not entered into the table.
EXCLUSION
constraint
EXCLUSION
constraintEXCLUSION lets you enforce a constraint on multiple rows (for a given column). It let's you validate what data is valid for a row based on other rows that exist within the table.
Exclusion constraints tell the database (conceptually) "When you're given a new or updated row, compare it against each existing row. Here's a list of comparisons to use. If all the comparisons between the new data and an existing row return true, reject the new data.
This constraint use btree indexes to do comparisons at a large scale. In PostgreSQL you have to create the btree_gist
extension to use the EXCLUSION constraint.
Let's see how to use this constraint
Note that if you specify "=", the semantics are equal to UNIQUE
constraint. The magic lies in using multi-column constraints. For e.g.
Here Row1 will conflict with Row2, IFF:
Row1.a == Row2_.a
, andRow1.b && Row2.b
You can utilize other PostgreSQL geometric operators here to build more complex constraints.
Resources
Last updated