Integrity Constraints in DBMS

In DBMS, Integrity constraints can be defined as a set of rules that are used to maintain the information’s quality. This ensures that the data integration is not affected at all by data insertion, updation or other processes. Hence integrity constraints are like insurance to guard the database if there is any accidental damage.

Types of Integrity Constraints

Integrity constraints can be of four types:

  1. Domain constraint
  2. Entity integrity constraint
  3. Referential integrity constraint
  4. Key constraint

Domain constraint

In DBMS, Domain constraints can be defined as a set of values that are valid for an attribute. The domain’s data type includes string, character, integer, time etc. The value must be in the corresponding domain of the attribute.

Example

NameSemesterAge
Adarsh1st19
Kushal4th20
Akash3rd20
Vishal7thH

Now in the table above, we can observe in the column Age, the data type of the domain is an integer but the attributes data type is a character.

Entity integrity constraint

This constraint states that in DBMS we can not make the primary key with the value NULL. Now, this is because if the primary key is NULL, then we won’t b able to determine or identify the tuple in the relation.
But in a relation, there can be NULL values but they must be not the primary key.

Example

S_NoNameSemesterAge
1Adarsh1st19
2Kushal4th20
3Akash3rd20
Vishal7th21

In the above example, we can see, that in the S_No column and last tuple, the attribute is null, so it can not be assigned as the primary key.

Referential integrity constraint

This constraint is defined between two tables. Let us consider tables A and B, so in this constraint, if a foreign key is referred to as a primary key of another table then the contents of the foreign key of table A must be null or available to tp table B.

Example

TABLE 1

S_NoNameSemesterCode
1Adarsh1st1258
2Kushal4th2451
3Akash3rd5758
4Vishal7th9654
5Sankalp5th4178

TABLE 2

CodeCity
4178Patna
2451Delhi
9654Mumbai
1258Pune

Now in the example above, we can see that in table 1, Code 5758 is not valid, as this attribute is not defined in table 2 and also the attribute is assigned as the primary key, and also Code in table 1 is assigned the foreign key.

Key constraint

In DBMS, a key is used to uniquely identify an entity in an entity set. An entity set can have multiple keys but out only one can be a primary key. This primary key should not be null and must be unique. Although it can contain a null and a non-null unique value.

Example

S. NoNameSemesterCode
1Adarsh1st1258
2Kushal4th2451
3Akash3rd5758
4Vishal7th9654
4Sankalp5th4178

In the example above, S_No can not be defined as a primary key because it contains a duplicate value. All the rows must be unique.