![]() |
VOOZH | about |
Integrity constraints are a set of rules used in DBMS to ensure that the data in a database is accurate, consistent and reliable. These rules helps in maintaining the quality of data by ensuring that the processes like adding, updating or deleting information do not harm the integrity of the database. Integrity constraints also define how different parts of the database are connected and ensure that these relationships remain valid. They play an essential role in making sure the data is meaningful and follows the logical structure of the database.
Integrity constraints in a Database Management System are rules that help keep the data in a database accurate, consistent and reliable. They act like a set of guidelines that ensure all the information stored in the database follows specific standards.
Example: Making sure every customer has a valid email address & ensuring that an order in the database is always linked to an existing customer.
Note: These rules prevent mistakes, such as adding incomplete or incorrect data, and make sure the database is secure and well-organized.
There are Different types of Integrity Constraints used in DBMS, these are:
Domain constraints are a type of integrity constraint that ensure the values stored in a column (or attribute) of a database are valid and within a specific range or domain. In simple terms, they define what type of data is allowed in a column and restrict invalid data entry. The data type of domain include string, char, time, integer, date, currency etc. The value of the attribute must be available in comparable domains.
Example: Below table demonstrates domain constraints in action by enforcing rules for each column
Student_Id | Name | Semester | Age |
|---|---|---|---|
21CSE100 | Aniket Kumar | 6th | 20 |
21CSE101 | Shashwat Dubey | 7th | 21 |
21CSE102 | Manvendra Sharma | 8th | 22 |
21CSE103 | Ashmit Dubey | 5th | 20 |
Types of Domain Constraints:
Why Domain Constraints Are Important :
Example: Let, the not-null constraint be specified on the "Semester" attribute in the relation/table given below, then the data entry of 4th tuple will violate this integrity constraint, because the "Semester" attribute in this tuple contains null value. To make this database instance a legal instance, its entry must not be allowed by database management system.
Student_id | Name | Semester | Age |
|---|---|---|---|
21CSE1001 | Sonali Rao | 5th | 20 |
21CSE1012 | Anjali Gupta | 5th | 21 |
21CSE1023 | Aastha Singh | 5th | 22 |
21CSE1034 | Ayushi Singh | NULL | 20 |
Read more about Domain Constraints and its types, Here.
Entity integrity constraints state that primary key can never contain null value because primary key is used to determine individual rows in a relation uniquely, if primary key contains null value then we cannot identify those rows. A table can contain null value in it except primary key field.
Key Features of Entity Integrity Constraints:
Example: It is not allowed because it is containing primary key (Student_id) as NULL value.
Student_id | Name | Semester | Age |
|---|---|---|---|
21CSE101 | Ramesh | 5th | 20 |
21CSE102 | Kamlesh | 5th | 21 |
21CSE103 | Aakash | 5th | 22 |
NULL | Mukesh | 5th | 20 |
Key constraints ensure that certain columns or combinations of columns in a table uniquely identify each row. These rules are essential for maintaining data integrity and preventing duplicate or ambiguous records.
Why Key Constraints Are Important ?
Example: It is now acceptable because all rows must be unique.
Student_id | Name | Semester | Age |
|---|---|---|---|
21CSE101 | Ramesh | 5th | 20 |
21CSE102 | Kamlesh | 5th | 21 |
21CSE103 | Aakash | 5th | 22 |
21CSE102 | Mukesh | 5th | 20 |
3.1 Primary Key Constraints
It states that the primary key attributes are required to be unique and not null. That is, primary key attributes of a relation must not have null values and primary key attributes of two tuples must never be same. This constraint is specified on database schema to the primary key attributes to ensure that no two tuples are same.
Example: Here, in the below example the Student_id is the primary key attribute. The data entry of 4th tuple violates the primary key constraint that is specifies on the database schema and therefore this instance of database is not a legal instance.
Student_id | Name | Semester | Age |
|---|---|---|---|
101 | Ramesh | 5th | 20 |
102 | Kamlesh | 5th | 21 |
103 | Akash | 5th | 22 |
3.2 Unique Key Constraints
The Unique key constraint in DBMS ensures that all values in a specified column (or group of columns) are distinct across the table. It prevents duplicate entries, maintaining data integrity, but unlike the primary key, it allows one NULL value.
Example: Here, in the below example the Email column has NULL value in 2nd record.
| Employee_ID | Name | |
|---|---|---|
| 1 | aniket@example.com | Aniket Kumar |
| 2 | NULL | Shashwat Dubey |
| 3 | shashwat@example.com | Manvendra Sharma |
Referential integrity constraints are rules that ensure relationships between tables remain consistent. They enforce that a foreign key in one table must either match a value in the referenced primary key of another table or be NULL. This guarantees the logical connection between related tables in a relational database.
Why Referential Integrity Constraints Are Important ?
Example: Here, in below example Block_No 22 entry is not allowed because it is not present in 2nd table.
Student_id | Name | Semester | Block_No |
|---|---|---|---|
22CSE101 | Ramesh | 5th | 20 |
21CSE105 | Kamlesh | 6th | 21 |
22CSE102 | Aakash | 5th | 20 |
23CSE106 | Mukesh | 2nd | 22 |
Block_No | Block Location |
|---|---|
20 | Chandigarh |
21 | Punjab |
25 | Delhi |
To read about SQL FOREIGN KEY Constraint Refer, Here.
An assertion is a declarative mechanism in a database that ensures a specific condition or rule is always satisfied across the entire database. It is a global integrity constraint, meaning it applies to multiple tables or the entire database rather than being limited to a single table or column. An assertion in SQL-92 takes the form:
create assertion <assertion-name> check <predicate>
When an assertion is made, the system tests it for validity. This testing may introduce a significant amount of overhead; hence assertions should be used with great care.
Example of an Assertion:
CREATE ASSERTION sum_constraint
CHECK (
NOT EXISTS (
SELECT *
FROM branch
WHERE (
SELECT SUM(amount)
FROM loan
WHERE loan.branch_name = branch.branch_name
) >= (
SELECT SUM(amount)
FROM account
WHERE account.branch_name = branch.branch_name
)
)
);
Explanation:
The following SQL statement creates an assertion to ensure that the total loan amount at each branch is always less than the total account balances at the same branch.
A trigger is a procedural statement in a database that is automatically executed in response to certain events such as INSERT, UPDATE, or DELETE. Triggers are often used to enforce complex integrity constraints or implement business rules that cannot be captured using standard constraints like primary keys or foreign keys.
Example SQL Trigger:
CREATE TRIGGER handle_overdraft
AFTER UPDATE ON account
FOR EACH ROW
BEGIN
-- Check if the balance has become negative after the update
IF NEW.balance < 0 THEN
-- Set the account balance to zero
UPDATE account
SET balance = 0
WHERE account_number = NEW.account_number;
-- Create a loan record with the same account number as the loan number
INSERT INTO loan (loan_number, loan_amount)
VALUES (NEW.account_number, ABS(NEW.balance)); -- ABS to ensure positive loan amount
END IF;
END;
Explanation: