0
0
DBMS Theoryknowledge~6 mins

Integrity constraints in DBMS Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine entering data into a database without any rules. This could lead to mistakes like duplicate entries or missing important information. Integrity constraints help keep the data accurate and reliable by setting rules that the data must follow.
Explanation
Entity Integrity
Entity integrity ensures that every row in a table can be uniquely identified. This is usually done by having a primary key, which is a column or set of columns that cannot be empty or duplicated. This prevents confusion between records.
Entity integrity guarantees each record is unique and identifiable by a primary key.
Referential Integrity
Referential integrity makes sure that relationships between tables stay consistent. For example, if one table refers to another through a foreign key, the foreign key value must exist in the related table. This prevents broken links between data.
Referential integrity ensures foreign keys correctly reference existing records in related tables.
Domain Integrity
Domain integrity restricts the type and range of data that can be stored in a column. For example, a column for age might only allow numbers between 0 and 120. This keeps data meaningful and prevents errors like entering text where numbers are expected.
Domain integrity enforces valid data types and acceptable values for each column.
User-Defined Integrity
User-defined integrity includes custom rules set by users to meet specific needs. These rules can be more complex, like ensuring a discount is only applied if a customer is a member. They help tailor data accuracy to particular business logic.
User-defined integrity applies custom rules to enforce specific business requirements.
Real World Analogy

Think of a library where books must be organized properly. Each book has a unique ID (like a primary key), references to authors must match actual author records (like foreign keys), the book's genre must be from a set list (like domain rules), and special rules might apply, such as only allowing rare books to be borrowed by senior members (custom rules).

Entity Integrity → Each book having a unique ID number so it can be found without confusion
Referential Integrity → Ensuring every author listed for a book actually exists in the author records
Domain Integrity → Restricting book genres to a predefined list like Fiction, Non-fiction, or Science
User-Defined Integrity → Rules like only senior members can borrow rare books
Diagram
Diagram
┌───────────────────────┐       ┌───────────────────────┐
│       Customers       │       │       Orders          │
│ ┌───────────────┐     │       │ ┌───────────────┐     │
│ │ Customer_ID PK│─────┼──────▶│ │ Customer_ID FK│     │
│ └───────────────┘     │       │ └───────────────┘     │
└───────────────────────┘       └───────────────────────┘

Domain Integrity: Age must be between 0 and 120
User-Defined Integrity: Discount only if Customer is VIP
Diagram showing two tables with primary key and foreign key relationship, plus domain and user-defined integrity rules.
Key Facts
Primary KeyA column or set of columns that uniquely identifies each row in a table and cannot be null.
Foreign KeyA column in one table that refers to the primary key in another table to maintain relationships.
Domain ConstraintA rule that limits the type, format, or range of values allowed in a column.
Entity IntegrityThe rule that primary key values must be unique and not null.
Referential IntegrityThe rule that foreign key values must match existing primary key values or be null.
Code Example
DBMS Theory
CREATE TABLE Customers (
  CustomerID INT PRIMARY KEY,
  Name VARCHAR(100) NOT NULL,
  Age INT CHECK (Age BETWEEN 0 AND 120)
);

CREATE TABLE Orders (
  OrderID INT PRIMARY KEY,
  CustomerID INT,
  FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

-- User-defined integrity example: discount only for VIP customers
CREATE TABLE VIPCustomers (
  CustomerID INT PRIMARY KEY,
  DiscountRate DECIMAL(3,2) CHECK (DiscountRate <= 0.5)
);

-- This setup enforces entity, referential, domain, and user-defined constraints.
OutputSuccess
Common Confusions
Believing that foreign keys must always have a value.
Believing that foreign keys must always have a value. Foreign keys can be null if the relationship is optional; referential integrity only requires that non-null foreign keys match existing primary keys.
Thinking domain constraints only check data type.
Thinking domain constraints only check data type. Domain constraints also enforce valid ranges, formats, and sets of allowed values, not just data types.
Assuming user-defined integrity is automatically enforced by the database.
Assuming user-defined integrity is automatically enforced by the database. User-defined integrity often requires custom code or triggers to enforce rules beyond standard constraints.
Summary
Integrity constraints are rules that keep database data accurate and consistent.
Entity, referential, domain, and user-defined integrity cover unique identification, relationships, valid data, and custom rules.
These constraints prevent errors like duplicates, broken links, invalid data, and business rule violations.