Sign in
Log inSign up

Domain Integrity Constraint

Radhika mohan's photo
Radhika mohan
·Feb 18, 2022·

3 min read

Domain Integrity Constraint

Before jumping directly to domain integrity constraints, let’s discuss what Integrity Constraints are in DBMS.

Integrity constraints in DBMS are used to ensure consistency and accuracy of data in any relational database. Integrity constraints are basically pre-defined sets of rules to maintain the data integrity of the table. Now we need to understand what data integrity means. So data integrity can be defined as the overall accuracy or completeness of the data. To create better databases, we need to enforce data integrity wherever it is possible. If we make any changes to our database, then Integrity Constraints ensure us that any changes, like deletion, update, or insertion, do not result in any loss in data consistency. Integrity Constraints are of four types, which are mentioned below:

  1. Domain Constraint
  2. Entity Integrity Constraints
  3. Referential Integrity Constraint
  4. Key constraint

In this article, we only discuss Domain Constraints.

Domain Constraints

As the name suggests, Domain Constraints define the domain or, in simple words, the valid set of values for an attribute. For example, if age is a constraint in our table, then it should be positive as age cannot be negative. Also, age should be an integer, so the valid set, in this case, will be positive integers. The data type of domain can be integers, strings, time, date, characters, characters, etc. and the value of an attribute that we add in the table must be available in their corresponding domain. Now let’s see some examples of domain constraints.

Example: For example, if I want to create a table “student” with an “age” field having a value greater than 0 and less than 25, Domain:

Create domain age int
Constraint age_test
Check (0<value<25)

Table:

Create table student (
Age,
Student_id NUMBER,
Name varchar ( 20 ),
Class varchar ( 10 ),
Age NUMBER PRIMARY_KEY
);

Screenshot 2022-02-18 at 4.07.45 PM.png

So, here, as Deepak’s age is given in this table is s, here domain constraint is violated, so this is not allowed as age must be a positive integer.

Another example

We take another table, “Employee”, with the “salary” field having a value greater than 20,000.

Domain:

Create domain salary int
Constraint salary_test
Check (value > 20,000)

Table:

Create table employee (
Salary NUMBER,
Employee_id NUMBER,
Name varchar ( 20 ),
Age NUMBER,
Salary NUMBER PRIMARY_KEY
);

Screenshot 2022-02-18 at 4.06.26 PM.png

Types of Domain Constraints

There are two types of domain constraints as mentioned below:

Not Null Domain Constraint: Null values are those values that are unassigned or, in simple words, missing values. Like in any table, if any value of an attribute is missing, then we can say that it has a null value. So, in this type, we can restrict a column to not accept any missing values. Let’s understand this by an example. For example, in a student table, the student_name column cannot be empty because every student must have a name.

Create table student ( 
Student_id varchar (10),
Student_name varchar (20) not null
 );

Check Domain Constraint: It is simply a condition that we want to apply to any column. For example, the age of every student in a class must be less than 25, then simply apply a condition check (age < 25).

 Create table student ( 
Student_id varchar ( 10 ),
Student_name varchar ( 20 ) not null,
Student_age NUMBER check ( Student_age < 25 )
     );

For understanding all types of integrity constraints, refer to Scaler Topics

Author: Arnav Bhardwaj