DEV Community

Chiazam Ochiegbu
Chiazam Ochiegbu

Posted on

Exclusion Constraints in Postgresql

Exclusion constraints

An exclusion constraint is used when comparing two rows on nominative columns or expressions using the nominative operators. The result of the comparison will be false or null.

Consider the following example in which the conflicting tuple is given the AND operation together:

warehouse_db=# CREATE TABLE movies
  (
  Title TEXT,
  Copies INTEGER
  );
Enter fullscreen mode Exit fullscreen mode

Using the ALTER TABLE command,we get the following:

warehouse_db=# ALTER TABLE movies
  ADD EXCLUDE (title WITH=, copies WITH=);
Enter fullscreen mode Exit fullscreen mode

We will create an exclusion constraint above the ALTER TABLE command. The conditions for a conflicting tuple are AND together. Now, in order for two records to conflict, we’ll use the following:

record1.title = record2.title AND record1.copies = record2.copies.
Enter fullscreen mode Exit fullscreen mode

Next article, we'll consider primary and secondary key constraints.

Top comments (0)