Domain constraints in DBMS - GeeksforGeeks

01 Sep, 2021
In DBMS, constraints are the set of rules that ensures that when an authorized user modifies the database they do not disturb the data consistency and the constraints are specified within the DDL commands like “alter” and “create” command. There are several types of constraints available in DBMS and they are:
Domain constraints
Entity Integrity constraints
Referential Integrity constraints
Key constraints
In this article, we will only discuss domain constraints.
Domain Constraints are user-defined columns that help the user to enter the value according to the data type. And if it encounters a wrong input it gives the message to the user that the column is not fulfilled properly. Or in other words, it is an attribute that specifies all the possible values that the attribute can hold like integer, character, date, time, string, etc. It defines the domain or the set of values for an attribute and ensures that the value taken by the attribute must be an atomic value(Can’t be divided) from its domain.
Domain Constraint = data type(integer / character/date / time / string / etc.) + 
                    Constraints(NOT NULL / UNIQUE / PRIMARY KEY / 
                                FOREIGN KEY / CHECK / DEFAULT)
Type of domain constraints:
There are two types of constraints that come under domain constraint and they are:
1. Domain Constraints – Not Null: Null values are the values that are unassigned or we can also say that which are unknown or the missing attribute values and by default, a column can hold the null values. Now as we know that the Not Null constraint restricts a column to not accept the null values which means it only restricts a field to always contain a value which means you cannot insert a new record or update a record without adding a value into the field.
Example: In the ’employee’ database, every employee must have a name associated with them.
Create table employee
(employee_id varchar(30),
employee_name varchar(30) not null,
salary NUMBER);
2. Domain Constraints – Check: It defines a condition that each row must satisfy which means it restricts the value of a column between ranges or we can say that it is just like a condition or filter checking before saving data into a column. It ensures that when a tuple is inserted inside the relation must satisfy the predicate given in the check clause.
Example: We need to check whether the entered id number is greater than 0 or not for the employee table.
Create table employee
(employee_id varchar(30) not null check(employee_id > 0),
employee_name varchar(30),
salary NUMBER);
The above example creates CHECK constraints on the employee_id column and specifies that the column employee_id must only include integers greater than 0.
Note: In DBMS a table is a combination of rows and columns in which we have some unique attribute names associated with it. And basically, a domain is a unique set of values present in a table. Let’s take an example, suppose we have a table student which consists of 3 attributes as NAME, ROLL NO, and MARKS. Now ROLL NO attributes can have only numbers associated with them and they won’t contain any alphabet. So we can say that it contains the domain of integer only and it can be only a positive number greater than 0.
Example 1:
Creating a table “student” with the “ROLL” field having a value greater than 0.
Domain:
Table:
The above example will only accept the roll no. which is greater than 0.
Example 2:
Creating a table “Employee” with the “AGE” field having a value greater than 18.
Domain:
Table:
The above example will only accept the Employee with an age greater than 18.
Picked
Class 12
DBMS
School Learning
School Programming
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Data Communication - Definition, Components, Types, Channels
What is an IP Address?
Sorting Algorithms in Python
SQL HAVING Clause with Examples
Cardinality in DBMS
SQL | WITH clause
SQL | Join (Inner, Left, Right and Full Joins)
ACID Properties in DBMS
SQL query to find second highest salary?
Normal Forms in DBMS