Introduction

Constraints in DBMS play a key role in Database Management Systems as they ensure data consistency in the Database. They are a set of rules which ensures that data is stored under certain specific conditions and does not lose its consistency when an authorized user accesses the database to modify the data.

Simply, the Constraints are the restrictions that keep the data reconcilable as per the conditions, and at the time of accessing data, it undergoes some verification to assure it is consistent. Constraints are sometimes confused with Integrity Constraints. But, both are the same. Constraints are also called Integrity Constraints whose target is to ensure data integrity and data consistency.

A real-world example of Integrity constraints is the label of a publishing company on the book. When you purchase a book, you check the label of the publisher. If the label belongs to the desired publisher, you purchase otherwise not. So, the label on the book is a constraint that checks some conditions when you try to access the book.

Types of Domain Constraints

There are various types of constraints in DBMS which are named below.

  • Domain Integrity Constraints
  • Entity Integrity Constraints
  • Referential Integrity Constraints
  • Key Constraints

Diagram

The DBMS  constraints can be understood briefly using the following diagram:

In this article, we will discuss Domain Constraints (also called Domain Integrity Constraints).

Domain Integrity Constraints in DBMS

Domain constraints in DBMS are the set of rules which defines what kind of attributes can be stored in an entity (a table that stores data). Domain Constraints help us to enter the data into the table according to the particular data type.

Domain Constraints specify two things-Data Type and constraints such as NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK, etc.  In other words, these constraints define the set of rules (domain) for an attribute of an entity. That’s why these are called domain constraints. This Constraint also ensures that the value taken by an attribute is an atomic value. This means that it cannot be divided from its domain.

Types of Domain Constraints

The Domain Constraints are broadly classified into two types - Not Null Constraints and Check Constraints which are discussed in detail below.

Not Null Domain Constraint

The entity (data table) may contain some fields which don't have any value. We can say it contains some missing values. These unassigned or missing values are called null values.

But, it is not possible to accept the null values all the time. So, Not Null Constraint is used which restricts a column not to accept the missing null values by default. This means that you cannot insert a new column or record without assigning values in the fields of that column. For example, in the MySQL Server, if you want to assure that a particular field is not null in the table, then Not Null constraints can be implemented as follows:

Check Domain Constraints

This constraint works as a filter that limits the data in some range. It ensures that the data is entered into the table only after satisfying some conditions. Whenever a set of values is inserted into the record, it is verified under the condition written in the check clause.

Suppose, there is a Database in which you are going to insert the salary of an employee. In this case, a salary cannot be negative so you can write the MySQL code for using Domain Constraints as follows:

This is how you can use the Domain Constraints in your database to ensure the data integrity and consistency.