PostgreSQL: Example of Partial UNIQUE Composite Index

The Data validation is always a major concern of any Database System.
We all know about the UNIQUE constraint which prevents the duplicate records.

One of the Previous article, I have already shared one small note about the UNIQUE Index of PostgreSQL.

PostgreSQL: Composite UNIQUE Constraint does not consider NULL Values

In this post, I am sharing a similar post for Partial Unique Index of PostgreSQL.

For example, I want to stop duplicate Student Grages for particular Student Division only.

Create a sample table:

Create a Partial UNIQUE Index:

Insert few sample records:
As we have creted Partial index so we can insert only unique combination of StudDivision (First,Second,Third) and StudGrade.

Try to insert few Invalid records:

Try to insert few records which are not validated by Partition Index:
We can insert duplicate records StudGrade + StudDivision for other StudDivisions except Partial StudDivision.

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of