The first question you get to your mind when you listen to the word integrity constraint is what is an integrity constraint? I got the same doubt when I started to learn this topic.
What are Integrity Constraints?
First of all, what is a constraint? We know that constraint is a restriction. That means it is controlling till a limit after that it will signal an error.
So, now what is Integrity? We know that integrity is the quality of being honest to morals. That means, it is a quality for obeying morals.
So, Integrity Constraints for DBMS are the restrictions that will ensure that whenever we do modify or create data we should not cross the ethics of the data like gathering and protecting data.
The textbook meaning of Integrity Constraints is "Integrity constraints ensure that the data insertion, updating, and other processes have to be performed in such a way that data integrity is not affected."
I think now you guys understood Integrity Constraints. Let's now discuss the Types of Integrity Constraints of DBMS.
Types of Integrity Constraints of DBMS-:
Entity Integrity Constraint-:
This is the integrity constraint for the primary key(If you want to know about the primary key just read my previous blog).
There are three conditions for the primary key-:
Every table must have a primary key.
That means relational databases are represented using tables, So, there should be a column/attribute that satisfies the conditions of the primary key in every table.
Every primary key has to be unique.
That means the values of the primary key should not be repeated.
The primary key should not be null.
That means every row must contain at least one value in that primary key.
Domain Integrity Constraint -:
It is a restriction on the values that are not in the given valid range. That means,
This constraint ensures that the value which is inserted should not cross the predefined range. It will also ensure the data type of the column. If the column is integer type then you should enter only integers.
Let me explain to you using an example, Whenever you will see a Hackathon event page you can see the terms and conditions to apply for the hackathon. In the terms and conditions they will give a rule like the age of the participant should be between 18 and 25. So, If you cross or below this range, the website will not allow you to apply. If you enter any character or string in place of a number in age it will be also not allowed. This is ensured by the Domain Integrity Constraint.
User-defined Integrity Constraint -:
the set of guidelines that guarantees the consistency of the data when an authorised user modifies the database.
Let me give you an everyday example of this constraint.
is there any cricket lover 💖 reading this blog?
If so, click the like button😉. Because this example is about cricket.
Usually, cricket has predefined rules in internationally cricket. But in gully cricket, every team has their own rules. The own rules are known as user-defined rules. These rules are based on the perspective of the people who are playing and those should be followed by everyone. Some guys give runs to wide and some guys won't give runs to wide and there is no ball in some places and they keep rules like touch for run etc.
Let us assume the cricket teams in gully cricket are like an e-commerce company and assume the user-defined rules by the teams in cricket are user-defined rules for the company. The e-commerce company place a user-defined rule like the number of items purchased by a transaction id should not cross like 5 products. So everyone who is purchasing products on their website should follow this rule.
Referential Integrity Constraint -:
This is the most important integrity constraint.
These are the integrity constraints that are related to the foreign keys.(If you want to know about the foreign key read my previous blog).
The Referential Integrity Constraint demands that every relationship between two relational database tables must always be legitimate. That means it ensures that there should be a valid relationship between the two tables.
But I will explain to you the foreign keys in a simple way. It is key in a table that refers to another table. So basically there are two tables -:
1) Referencing table.
2) Referenced table.
For Example, A database has two tables, one for employees and one for department managers. The employee table has a column called "employee ID" and the manager table has a column called "manager ID." The "employee ID" in the employee table is connected to the "manager ID" in the manager table. This means that the database checks to make sure that each manager has at least one employee assigned to them and it prevents any mistakes where an employee is assigned to a non-existent manager.
Let's take an easy example A referential integrity constraint could be used between the Student table and the Enrollment table in a student information system. A course cannot be deleted if there are students enrolled in it, and the constraint would ensure that each student can only enrol in courses that are present in the course table or real course.
Key Constraint
A key constraint is a type of constraint in DBMS that specifies a set of attributes (columns) that uniquely identify each tuple(row) in a table.
A single entity set may include several keys but there is only one key which uniquely identifies the tuple and that is the primary key. This constraint states that the primary key must only contain unique and not null values.
Conclusion
These are the types of integrity constraints in DBMS.
If you want to learn the basics of DBMS you can read my previous blog.
I think I had explained constraints in an easy way. If you like the blog just like👍 or comment on the blog.