The integrity constraints are the set of rules that has to be followed while creating a relation or a table. By following these integrity constraints the data we enter in the tables will have better data integrity. The whole purpose of data integrity is to protect our database from any accidental loss of integrity and hence these integrity roles play an important part in data consistency and integrity. So, in other words, you can also say that the tables that are present in the database are checked with the help of these constraints and therefore their validity is then assured if they follow them.
There are three types of integrity constraints; these are key constraints, domain constraints, and referential constraints. Let’s see them one by one.
The key constraint
The key constraint is used to identify an entity within various relations. And this is done with the help of keys and mainly the primary key and therefore the rules that are followed in the primary key becomes the ruler of the key constraints. There are other keys such as foreign key and candidate key but they are not as valuable as the primary key can alone identify the entire entity and therefore can be successfully used in depicting the relationship between various entities. There are many rules that the primary key has to follow and if one of the rules that the primary key follows is not satisfied then the relation will lose its integrity. The rules that are applied in the primary key are that there must be no element that is repeated in the primary key column and also the primary key is a column in the table and therefore when any element in the column is repeated more than once then the key constraints become invalid. Also, there is another property in the primary key that the element in the primary key should not be a Null value therefore the primary key elements should never be left empty. Therefore the two rules that are very important in the key constraints are that it should not be empty and the element should not repeat twice. Also if there are more than one minimal subsets of the primary keys they are called the candidate keys.
The domain constraint says that every attribute is bound to have a specific range of values and if this condition is not fulfilled then the relationship loses its integrity. Every attribute has some specific value. It can be either character or numeric or it can be alphanumeric and depending on the attributes these values are set. For example, if there is an attribute in a relation and that is the name of the student and in that case, the values that this attribute can store will be a character in nature, so these are the domain constraints that the relation has to follow. The other example can include the attribute of age the domain constraints, in this case, will say that the values that can be stored in this column should be not negative and should not be infractions and these cases hold for the age, so the domain constraints will have the integrity till the right values are entered into the attribute. Also, other examples can include the phone number such as the digits of the phone number should range from only zero to nine, all these are the conditions that have to be fulfilled to store some value in the attribute.
Referential key constraint
As the primary key was used for the key constraint similarly the foreign key is used for the foreign key constraint. The foreign key is a key relation that can be referred to in other relations. With the rules that are followed in the foreign key, the same rules are applied to the foreign key constraint.