US Trends

what are integrity constraints in dbms

Integrity constraints in DBMS are rules that ensure the data in a database stays accurate , consistent, and logically valid so that no wrong or contradictory data can be inserted, updated, or deleted.

Quick Scoop: Core Idea

  • They are rules attached to tables/columns.
  • The DBMS automatically checks these rules whenever you insert, update, or delete data.
  • If a rule is violated, the operation is rejected, protecting the database from bad data.

In short: integrity constraints are like strict gatekeepers for your database.

Why Integrity Constraints Matter

  • They prevent invalid data (like negative age, wrong formats, non‑existent foreign keys).
  • They keep relationships between tables consistent.
  • They help maintain long‑term reliability of the database, even as many users and apps interact with it.

Think of a university database:

  • You should never have a student without a valid roll number.
  • You should never have a course registration pointing to a course that doesn’t exist.
    Integrity constraints make sure such “impossible” situations never occur.

Main Types of Integrity Constraints in DBMS

1. Domain Integrity

Domain constraints restrict what kind of values a column can store.

  • Based on:
    • Data type (INT, VARCHAR, DATE, etc.).
    • Range (e.g., marks between 0 and 100).
    • Allowed set (e.g., status: 'Active', 'Inactive', 'Pending').

Example:

sql

CREATE TABLE Employee (
    EmpID      INT,
    EmpName    VARCHAR(50),
    Age        INT CHECK (Age >= 18 AND Age <= 60),
    Gender     VARCHAR(10) CHECK (Gender IN ('Male', 'Female', 'Other'))
);

Here:

  • Age must be between 18 and 60.
  • Gender must be one of the listed values.

2. Entity Integrity

Entity integrity ensures each row in a table is uniquely and clearly identifiable.

  • Implemented using PRIMARY KEY.
  • Rules:
    • Primary key values must be unique.
    • Primary key values cannot be NULL.

Example:

sql

CREATE TABLE Students (
    RollNo    INT PRIMARY KEY,
    Name      VARCHAR(50),
    Class     VARCHAR(10)
);
  • No two students can have the same RollNo.
  • RollNo cannot be NULL.

If you try:

sql

INSERT INTO Students (RollNo, Name, Class)
VALUES (NULL, 'Rahul', '10A');

→ This will fail because it violates entity integrity.

3. Referential Integrity

Referential integrity ensures that relationships between tables stay valid.

  • Implemented using FOREIGN KEY.
  • A foreign key in one table must:
    • Either be NULL, or
    • Match an existing primary key value in another (parent) table.

Example:

sql

CREATE TABLE Department (
    DeptID   INT PRIMARY KEY,
    DeptName VARCHAR(50)
);

CREATE TABLE Employee (
    EmpID    INT PRIMARY KEY,
    EmpName  VARCHAR(50),
    DeptID   INT,
    FOREIGN KEY (DeptID) REFERENCES Department(DeptID)
);
  • You cannot insert an employee with DeptID = 999 if no department with DeptID = 999 exists.
  • You also may be prevented from deleting a department if employees are still referencing it (unless you use ON DELETE CASCADE or similar options).

4. Key Constraints (Uniqueness)

Key constraints ensure that certain columns (or combinations of columns) have unique values. Common ones:

  • PRIMARY KEY : unique + not null, one per table.
  • UNIQUE : unique but can usually have one NULL (depends on DBMS).

Example:

sql

CREATE TABLE Users (
    UserID    INT PRIMARY KEY,
    Email     VARCHAR(100) UNIQUE,
    Username  VARCHAR(50) UNIQUE
);
  • No two users can share the same Email or Username.

5. NOT NULL Constraint

NOT NULL ensures a column must always have a value ; it cannot be left empty. Example:

sql

CREATE TABLE Orders (
    OrderID    INT PRIMARY KEY,
    OrderDate  DATE NOT NULL,
    Amount     DECIMAL(10,2) NOT NULL
);
  • You must give OrderDate and Amount every time you insert a row.

6. CHECK Constraint

CHECK allows you to specify a custom condition that each row must satisfy. Example:

sql

CREATE TABLE Product (
    ProductID INT PRIMARY KEY,
    Name      VARCHAR(50),
    Price     DECIMAL(10,2) CHECK (Price >= 0),
    Stock     INT CHECK (Stock >= 0)
);
  • Negative price or stock is not allowed.

7. DEFAULT Constraint

DEFAULT provides a default value for a column if no value is supplied. Example:

sql

CREATE TABLE Product (
    ProductID INT PRIMARY KEY,
    Name      VARCHAR(50),
    Price     DECIMAL(10,2) DEFAULT 0.0,
    Status    VARCHAR(20) DEFAULT 'Active'
);
  • If you don’t specify Price, it becomes 0.0.
  • If you don’t specify Status, it becomes 'Active'.

Tiny Story to Lock the Concept

Imagine a shopping app’s database:

  • Domain integrity :
    Price must be positive; email must be in valid format; order date must be a valid date.

  • Entity integrity :
    Every user has a unique UserID. No row without a UserID.

  • Referential integrity :
    Every Order must belong to a valid UserID that exists in the Users table.

  • Key & NOT NULL:
    Email must be unique, and cannot be NULL.

  • CHECK & DEFAULT:
    Quantity must be ≥ 1; default order status is Pending.

If the app tries to insert an order for a user that doesn’t exist, or with quantity −5, the DBMS will reject it. That’s integrity constraints silently protecting the system.

Mini Table: Types at a Glance (HTML as requested)

Constraint Type Main Purpose Typical SQL Feature Simple Example
Domain integrity Limit allowed values for a column Data type, CHECK Age between 18 and 60
Entity integrity Uniquely identify each row PRIMARY KEY Roll number of student
Referential integrity Keep cross-table references valid FOREIGN KEY Employee must belong to an existing department
Key constraints Ensure uniqueness of values PRIMARY KEY, UNIQUE No two users share same email
NOT NULL Disallow missing values NOT NULL Order date is always required
CHECK Enforce custom conditions CHECK Salary > 0
DEFAULT Provide automatic value when omitted DEFAULT Status defaults to 'Active'

Forum‑Style Take (as if answering on a discussion thread)

Q: “What are integrity constraints in DBMS, and do I really need to care about them in 2026 systems like microservices + NoSQL + relational mixes?” A: Yes, absolutely. Even though we now use a mix of SQL and NoSQL, relational databases still power critical parts like payments, user accounts, and logs. Integrity constraints are the built‑in rules that stop your production DB from filling up with junk — duplicate keys, orphaned records, inconsistent references. If you skip them and try to enforce everything only at the application level, one buggy release or one forgotten validation in a microservice can corrupt your data. Fixing code is easy; fixing corrupted production data is painful. Integrity constraints exist exactly to prevent that nightmare.

TL;DR

  • Integrity constraints are rules in DBMS that keep data correct , consistent, and meaningful.
  • Major types:
    • Domain, Entity, Referential, Key, NOT NULL, CHECK, DEFAULT.
  • They are enforced automatically by the DBMS to block invalid operations.
  • They are still crucial in modern systems where reliable data is non‑negotiable.

Information gathered from public forums or data available on the internet and portrayed here.