Domain Constraints in DBMS

Domain constraints in DBMS

In this article, we will learn about Domain Constraints in DBMS.

DBMS
  • DBMS table is viewed as a combination of rows and columns. 

For example, if you are having a column called month and you want only [Jan, Feb, March….dec] as values allowed to be entered for that particular column which is referred to as domain for that particular column.

Or For Example, in the age column, it must only accept students greater than 18 age or roll number should be a positive number.

Definition :

Domain constraint ensures  two things 

    • It makes sure that the data value entered for that particular column matches with the data type defined for that column 
    • It shows that the constraints((NOT NULL / UNIQUE / PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)) impose on that column not fulfilled or not 
Domain Constraints

Example :

For example, we want to create a table “student” with “stu_id” field having a value greater than 100,  can create a domain and table like this:

Constraint Creation Template

create domain domain_name int
constraint constraint_name
check(write codition here);

Constraint Creation Example

create domain age_constraint int
constraint age_test
check(value > 18);

Table Example

create table student (
student_id PRIMARY KEY,
student_name varchar(30),
student_age age_constraint int
); 

Example 2

Constraint Creation Example

create domain roll_no_constraint int
constraint roll_test
check(value > 0);

Table Example

create table student (
RollNo roll_no_constraint PRIMARY KEY,
StudentName varchar(30),
StudentPhone int
);