What are integrity constraints in DBMS?
EasyConceptKeys & Constraints
Answer
Integrity constraints are rules declared on tables that the DBMS checks on every insert, update and delete, so that invalid data never enters the database.
- Domain constraints: every value must belong to its column's type and allowed range, enforced with data types,
NOT NULL,CHECKandDEFAULT. - Entity integrity: every table has a primary key, and primary key values are unique and never
NULL. - Referential integrity: a foreign key value must match an existing key in the parent table or be
NULL. - Key constraints: a
UNIQUEcolumn cannot contain repeated values.
MySQL ignored CHECK constraints before version 8.0.16; newer versions enforce them.
Example:
CREATE TABLE accounts (
acc_no INT PRIMARY KEY, -- entity integrity
owner VARCHAR(50) NOT NULL, -- domain
balance DECIMAL(10,2) CHECK (balance >= 0), -- domain
branch INT,
FOREIGN KEY (branch) REFERENCES branches(id) -- referential
);
INSERT INTO accounts VALUES (1, 'Asha', -500, 1); -- rejected by CHECK
More in Keys & Constraints
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What are super, candidate, primary, alternate and composite keys? | Concept | Easy | Read the answer to What are super, candidate, primary, alternate and composite keys? | |||
| 2 | What is the difference between a primary key and a unique key? | Concept | Easy | Read the answer to What is the difference between a primary key and a unique key? | |||
| 3 | What are integrity constraints in DBMS? | Concept | Easy | Read the answer to What are integrity constraints in DBMS? | |||
| 4 | What is a foreign key, and what do ON DELETE CASCADE and ON DELETE SET NULL do? | Concept | Medium | Read the answer to What is a foreign key, and what do ON DELETE CASCADE and ON DELETE SET NULL do? |