×

Constraints in Database Management System

In a database management system, certain constraints are applied while performing any operation on the database to ensure that the integrity and quality of the data are maintained throughout the process. There are several types of constraints that can be implemented on the database during the CRUD operations and other processes. They limit the entry of data or the data type of the data or both. These Constraints are also known as Integrity Constraints.

Types of Constraints

  • Domain Constraints
  • Entity Integrity Constraints
  • Referential Integrity Constraints
  • Key Constraints
  • NOT NULL
  • UNIQUE
  • DEFAULT
  • CHECK
  • Mapping Constraints

Domain constraint: This constraint specifies a set of values that can be entered as the valid input for the attribute. Datatypes of domain attributes include string, integer, date, time etc. A particular column of a table in a database allows only a single type of data. We cannot have any other datatype value in that column.

Domain constraints are also called user-defined data types. Additionally, it enforces some basic rules that are defined by the user on the column.

Domain Constraint is a combination of the datatypes and special constraints and can be expressed as “data type + Constraints (NOT NULL / UNIQUE /                          PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)”

Entity Integrity Constraint: This constraint specify that a primary key cannot hold null as a value as it is used to uniquely identify a record but if null, we won’t be able to identify the entity.

Referential Integrity Constraint: This constraint is applied among different tables. According to this constraint if a foreign key of a table refers to the primary key of that table, then all the values of that foreign key either should be a value from the primary key of that other table or should be a null value. 

Key Constraint: They are two types of key constraints. These are primary key constraints and foreign key constraints.

  • PRIMARY KEY

As discussed earlier, the Primary key can uniquely identify each record in a table. It must have unique values and cannot contain nulls. In the example below we have declared WORKER_ID as the Primary Key of the table. Now no two values of the WORKER_ID can hold the same value and neither can it hold NULL as a value.

CREATE TABLE WORKER(
WORKER_ID INT NOT NULL,
WORKER_NAME VARCHAR (30) NOT NULL,
WORKER_AGE INT NOT NULL,
WORKER_PHONE_NUMBER INT NOT NULL UNIQUE,
WORKER_ADDRESS VARCHAR (200),
PRIMARY KEY (WORKER_ID)
);
  • FOREIGN KEY

Foreign keys are primarily used to refer to the values of another table in the table. It is a key that refers to the primary key of any other table and is allowed to have any value from that primary key column or it should be NULL. They are used to cross-refer the tables.

Not null: This is used to check that no value entered in the particular table should be null. When no value is entered while entering the value of any column a null value is automatically assigned to it. But when we specify this constraint we cannot leave any value empty as the particular column should not have a null value. In such a case, we usually assign a DEFAULT value which is assigned when no value is entered.

Example:

CREATE TABLE WORKER(
WORKER_ID INT,
WORKER_NAME VARCHAR (30) NOT NULL,
WORKER_AGE INT NOT NULL,
WORKER_PHONE_NUMBER INT NOT NULL,
WORKER_ADDRESS VARCHAR (200),
PRIMARY KEY (WORKER_ID)
);

UNIQUE:

UNIQUE as the name suggests this keyword is used to ensure that no two values of the particular column have the same values. In the example below, WORKER_PHONE_NUMBER is limited with this constraint. No, two employees can have the same value in this database.

CREATE TABLE WORKER(
WORKER_ID INT NOT NULL,
WORKER_NAME VARCHAR (30) NOT NULL,
WORKER_AGE INT NOT NULL,
WORKER_PHONE_NUMBER INT NOT NULL UNIQUE,
WORKER_ADDRESS VARCHAR (200),
PRIMARY KEY (WORKER_ID)
);

DEFAULT:

In the DEFAULT constraint, we can assign a specified value to a column, this value is assigned where the value is left by the user empty. If there is no value assigned to WORKER_SALARY, then instead of a NULL value 10000 will be assigned as that employee’s salary.

CREATE TABLE WORKER(
WORKER_ID INT NOT NULL,
WORKER_NAME VARCHAR (30) NOT NULL,
WORKER_SALARY INT DEFAULT 10000,
WORKER_AGE INT NOT NULL,
WORKER_PHONE_NUMBER INT NOT NULL,
WORKER_ADDRESS VARCHAR (200),
PRIMARY KEY (WORKER_ID)
);

CHECK:

This keyword is used to enclose the values of the particular column in a finite set. When we can implement this constraint to determine the extreme values of the column. The value feed to the column should hold to this condition, if not the value won’t be accepted as an entry. Here an error will be raised if the WORKER_SALARY is more than the specified range that is 100000.

CREATE TABLE WORKER(
WORKER_ID INT NOT NULL,
WORKER_NAME VARCHAR (30) NOT NULL,
WORKER_SALARY INT CHECK (WORKER_SALARY <100000),
WORKER_AGE INT NOT NULL,
WORKER_PHONE_NUMBER INT NOT NULL,
WORKER_ADDRESS VARCHAR (200),
PRIMARY KEY (WORKER_ID)
);

Related Topics

B Tree in DBMS

How to perform Insertion operation in B Tree A new value is inserted at the leaf node. Same like in a binary search tree we traverse from starting root node to...

10 minutes read.

Entity and Entity set in DBMS

What is DBMS? A database management system (DBMS) is a software application that interacts with end-users, other applications, and the database itself to capture and analyze the data. A DBMS allows...

3 minutes read.

Levels of Abstraction in DBMS

Data abstraction is a way to hide unwanted or irrelevant information from the end user in DBMS. It helps in enhancing the security of data, and simplifies database design using...

4 minutes read.

BCNF in DBMS

BCNF stands for Boyce–Codd Normal Form. What is the Normal form? The normal form is mainly used to reduce the redundancy of the database tables. Or we can say that the normal form...

4 minutes read.

Er Diagram Symbols and Notations in DBMS

Entity Relationship Diagram Entity Relationship Diagrams, or ER Diagrams for short, are diagrams that show the relationships among entity sets that are stored in databases. Alternatively said, ER diagrams assist in...

6 minutes read.

ER Model: Entity Relationship Diagram (ERD) with Examples

ER model stands for Entity-Relationship Model. It is a high-level data model diagram which defines the conceptual view of the database. It is a blueprint or design of a database that will...

2 minutes read.

Applications of DBMS

There are various fields where a database management system is used. Following are some applications which make use of the database management system: 1. Railway Reservation System: In the railway reservation...

7 minutes read.

Multivalued Dependency

Multivalued Dependency exists in a relation when two attributes depend on the third attribute but independent to each other. It is a full constraint between two sets of attributes in a relation. It...

2 minutes read.

Redundancy in Database Management System

Redundancy in a database management system (DBMS) refers to the duplication of data within the database. This duplication can occur in multiple ways, such as having multiple copies of the...

13 minutes read.

How to draw ER-Diagram in DBMS?

Entity Relationship Diagrams, or ERDs, are diagrams that assist you in visualizing your database design. An ERD, also called anER diagram or ER model,describes data and how parts of the data interact. ERDs are crucial in database architecture and projects that call for...

9 minutes read.

Specialization in DBMS

Specialization in DBMS In the database management system, specialization breaks the higher-level entity into two or more than two lower entities. The main motive of this concept is to share the...

2 minutes read.

Hashing in DBMS: Static and Dynamic

Hashing in DBMS: Hashing is the technique of the database management system, which directly finds the specific data location on the disk without using the concept of index structure. In the...

4 minutes read.

Types of Data Abstraction in DBMS

What Is Data Abstraction? To ship an email, you want to know the address. But to send the email, you don't need to see where the email is physically stored. You...

6 minutes read.

What is Advanced Database Management System

Before diving into the Advance Database management system, we need to learn about the database management system and its usage. Database management system Database Management System or DBMS in short implies the...

3 minutes read.

Fragmentation in Distributed DBMS

Fragmentation is a course of isolating the entire or full information base into different sub tables or sub relations with the goal that information can be put away in various...

4 minutes read.

Query optimization in DBMS

Choosing an effective execution strategy to execute a query is a process known as query optimization. After the decision-making process of query parsing, which determines how many distinct ways a given...

5 minutes read.

ER Diagram for University Database in DBMS

What is an ER diagram? ER diagram, short for Entity Relationship Diagram, also known as ERD, is a diagram that shows the relationships of a set of entities stored in a...

9 minutes read.

Components of Relational Database Management System

A relational database management system compromises various component. Tables, records, attributes, instances, schemas and keys together form a relational database. In this page, we will discuss each component of RDBMS in...

4 minutes read.

File Organization in DBMS

File Organization in DBMS: A database contains a huge amount of data, which is stored is in the physical memory in the form of files. A file is a set...

9 minutes read.

Fundamentals Of Distributed DBMS

Fundamentals of DDBMS Here we are going to talk about the Fundamentals of DDBMS. But first, we need to know about Distributed databases to understand the Distributed Database Management System. Distributed Database: It is...

4 minutes read.