×

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

Serializability in DBMS

Serializability in DBMS Serializability is the concept in a transaction that helps to identify which non-serial schedule is correct and will maintain the database consistency. It relates to the isolation property of transaction in...

3 minutes read.

ER Diagram for Banking System 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...

3 minutes read.

Trigger in DBMS

Trigger in DBMS: A trigger is a procedure of SQL statements, which is automatically fired when the DML statements are executed on the table of the database. Triggers are the...

3 minutes read.

Integrity Constraints in DBMS

Integrity constraints in DBMS (Database Management System) are used to ensure the accuracy and consistency of data in a database. They are a set of rules that define the allowed...

1 minute 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.

Meta Data in DBMS

Metadata: Metadata is simply described as information about information. It indicates that the data is described as well as its context. It makes data easier to find, interpret, and organize. I'll...

4 minutes read.

Conflict Serializability in DBMS

Conflict Serializability A schedule is said to be conflict serializable if it can transform into a serial schedule after swapping of non-conflicting operations. It is a type of serializability that can be used...

3 minutes read.

Concurrency Control Protocols

Concurrency Control Protocols Concurrency control protocols ensure the atomicity, serializability and isolation of the concurrent transactions. The Concurrency control protocols can be broadly classified into the following categories: Lock Based ProtocolTimestamp protocol Lock Based Protocol In this protocol,...

5 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.

DBMS Schedule

A schedule is a process of combining the multiple transactions into one and executing the operations of these transactions in a predefined order. A schedule can have multiple transactions in it, each transaction...

2 minutes read.

Components of an ER Diagram

An ER Diagram consists of the following components: Entity AttributesRelationships 1. Entity An entity may be an object, place, person, or an event which stores data in the database. In an entity-relationship diagram, an entity...

4 minutes read.

Codds Rule of DBMS

Codd’s Rule of DBMS Database having certain constraints and tables, need not to be a relational database system always. For perfect database management system, there are certain rules for the database,...

3 minutes read.

Difference between Dropbox and Amazon Drive

What is Dropbox? It is a file organising company that is owned by the American Company Dropbox Inc. Itsheadquarters is situated in San Francisco, California, U.S. Dropbox offers personal cloud, client...

3 minutes read.

Disavantages of RDBMS

RDBMS offers many features when handling the data, but it also has certain limitations that can be overcome by choosing an alternative data model. The limitations of the Relational Data...

3 minutes read.

Evaluating DBMS

There are various database management systems available in the market. Each type has its features and can be used for varied purposes. The large number of DBMS makes it difficult...

4 minutes read.

Data Models in DBMS

Data Models in DBMS Data models are the models explaining the logical structure of the database systems. They describe the entities, attributes, and the relationship among the data elements of the...

8 minutes read.

B+ (Plus) Tree in DBMS

What is a B+ tree? The B+ tree is known as a balanced binary search tree. in this tree, we can store the data in the form of nodes. B+ tree...

9 minutes read.

Data Abstraction in DBMS

Introduction Data abstraction is a fundamental concept in database management systems (DBMS). It refers to the process of hiding the details of how data is stored and retrieved from the user,...

3 minutes read.

Conversion of ER Diagram into Relational Model

After designing the Entity-Relationship diagram, you need to convert it into tables in the relational model. Because the relational model can be easily implemented by the Relational DBMS like Oracle, MySQL, etc....

2 minutes read.

Redundancy in DBMS

Data redundancy is a situation that is created in the database in which the same amount of data is stored in two different places. The different places are found in a...

3 minutes read.