Integrity Constraints in DBMS

Integrity-Constraints-in-DBMS-feature-image.jpg

Integrity constraints in DBMS are a set of rules that control how data is added, changed, or removed from database tables. These rules help avoid mistakes, incorrect values, and unexpected loss or damage to data. They are important in relational databases because they keep the system safe, consistent, and working as expected. The primary purpose of integrity constraints in DBMS is to ensure the accuracy and reliability of the data. In this blog, you will learn about integrity constraints in DBMS along with their types and best practices in detail.

Table of Contents:

What are Integrity Constraints in DBMS?

Integrity constraints in DBMS are fixed rules that make sure the data in a relational database stays correct, consistent, and valid. These rules are set on table columns and links to stop wrong data entry and keep the data clean. When actions like insert, update, delete, or change happen, the database checks the data using these rules. If any rule is broken, the action is stopped. This process protects the database from invalid operations, and the data will follow proper logic and structure.

Key Features of Integrity Constraints in DBMS

1. Data Accuracy: Integrity constraints are used to make sure that only correct and proper data is added to the database. This stops mistakes like adding letters in number fields or using the wrong data formats.

2. Consistency Enforcement: They keep related tables consistent by setting rules that make sure foreign key values match the correct primary key values in the linked tables.

3. Automatic Validation: The DBMS runs all integrity checks by itself, making sure every INSERT, UPDATE, or DELETE action follows all the rules and stops actions that break them.

4. Prevention of Invalid Data: Wrong inputs like repeat primary keys, blank values in needed fields, or values not allowed are blocked by constraints to keep data clean and right.

5. Support for Relationships: Rules for referential integrity define table links clearly so that linked records stay correct and match throughout the database.

6. Defined at Schema Level: Constraints are defined at the schema level during the table creation. and applied consistently across the entire database.

7. Improved Data Reliability: Strict rules used in constraints help keep the data correct, steady, and useful to people and tools using the data.

8. Minimal Manual Intervention: Once set, the constraints work on their own, reducing the need for manual checks and cutting human mistakes while improving performance.

Become a Certified SQL Expert – Enroll Now!
Get hands-on training, real-time projects, and industry-recognized certification.
quiz-icon

Importance of Integrity Constraints in DBMS

  1. Data Accuracy: Ensures only valid and meaningful information is stored in the database.
  2. Duplicate Prevention: Stops the entry of repeated or incorrect data that could damage the database.
  3. Referential Integrity: Keeps the link between tables correct by ensuring that the foreign key values match the corresponding primary key
  4. Unique Identification: Primary key constraints help identify each record clearly and avoid confusion.
  5. Less Manual Work: Reduces the need to manually fix or check the data for errors.
  6. Data Security and Trust: This makes the data more reliable and secure for both users and applications.
  7. Efficient Operations: Automatically enforces rules, making database tasks faster and more consistent.

Types of Integrity Constraints in DBMS

Integrity constraints in DBMS are rules that help maintain data accuracy and consistency. There are four main types:

  1. Domain Constraints in DBMS
  2. Entity Integrity Constraints in DBMS
  3. Referential Integrity Constraints in DBMS
  4. Key Constraints in DBMS
Types of Integrity Constarints in DBMS

1. Domain Constraints in DBMS

Domain constraints control the type of data that can be stored in a column. They allow only certain data types like number, text, or date, and accept values that fit within a set range. For example, a column called Class may only accept numeric values and will not allow letters or invalid data to be stored.

Example:
Consider a Products table with columns: Product_ID, Product_Name, Price, and Manufacture_Date.

Product_ID Product_Name Price Manufacture_Date
101 Laptop 1500 2024-03-10
102 Mouse 25 2024-04-15
103 Keyboard 40 2024-05-05
104 Monitor 200 2024-06-01
105 Speaker Twenty 2024-07-12

In this table, the value “Twenty” under the Price column violates the domain constraint because the Price should be a numeric value (integer or decimal), but here a string is entered.

2. Entity Integrity Constraints in DBMS

Entity integrity is violated when a primary key column has a NULL value. Since the primary key must be unique to find a specific record, having NULL in this key means some records cannot be found, which violates the principles of entity integrity.

Example:
Consider a Customer table with Customer_ID, Customer_Name, and Contact_Number.

Customer_ID Customer_Name Contact_Number
201 Alice 9876543210
202 Bob 8765432109
203 Charlie 7654321098
NULL David 6543210987
205 Eva 5432109876

Here, the Customer_ID is the primary key. The NULL value in the fourth row violates the entity integrity constraint because the primary key cannot be null.

3. Referential Integrity Constraints in DBMS

Referential constraints keep the links between tables valid. They check that a foreign key in one table matches a primary key in another, which helps in preventing orphaned records or mismatched references.. For example, all department IDs in the employee table must also be in the department table.

Example:
Consider two tables, Orders and Customers, where Customer_ID in Orders is a foreign key referencing Customer_ID in Customers.

Orders Table

Order_ID Order_Date Customer_ID
1001 2024-06-01 201
1002 2024-06-15 202
1003 2024-07-01 206
1004 2024-07-12 203
1005 2024-07-20 NULL

Customers Table

Customer_ID Customer_Name Contact_Number
201 Alice 9876543210
202 Bob 8765432109
203 Charlie 7654321098
204 Diana 6543210987
205 Eva 5432109876

In this case, the Order with Customer_ID = 206 violates the referential integrity constraint because Customer_ID 206 does not exist in the Customers table.

4. Key Constraints in DBMS

Key constraints ensure that primary keys are unique and non-null. They stop records in a table from having the same key and ensure that each key is present so the table can clearly find each record.

Example:
Consider an Employees table with Employee_ID, Name, and Department.

Employee_ID Name Department
301 John HR
302 Emma Finance
303 Liam Marketing
304 Olivia IT
302 Noah Sales

Here, the Employee_ID 302 is repeated twice, violating the key constraint, which requires primary key values to be unique.

Get 100% Hike!

Master Most in Demand Skills Now!

Common Violations of Integrity Constraints and How to Resolve Them

The integrity constraints also ensure the accuracy of data, but some common violations may occur during the operation of the database. Below are some of the common violations and their possible resolution, which help in preventing data issues and system errors.

1. Domain Constraint Violation

Violation: Saving the wrong type of data, like entering text in a field meant for numbers, is a common example of invalid data in a numeric field.

Solution: Check that the entered data matches the set data type and allowed values of the column. Always validate inputs before adding or updating records.

2. Entity Integrity Violation

Violation: Entering a null value in the primary key column.

Solution: A correct and unique primary key value must always be given. Use auto-increment or sequence tools to stop NULL or repeated values.

3. Referential Integrity Violation

Violation: Inserting or modifying a foreign key with a value that does not exist in the primary key column.

Solution: Before inserting and updating, ensure that the foreign key value is present in the parent table. Use cascading options like ON DELETE CASCADE or ON UPDATE CASCADE when needed to keep data consistent across related tables.

4. Key Constraint Violation

Violation: Duplicate value in a primary key or unique key column.

Solution: Make sure that the records have unique key values. Prevent duplicates by creating unique identifiers in the database and checking the existing data before inserting it.

Best Practices for Using Integrity Constraints in DBMS

Integrity constraints help keep data accurate and consistent. Following the best practices below ensures clean, reliable, and well-structured data in the database:

1. Define Constraints Early:  Add all necessary constraints during the design stage. This blocks incorrect data from being stored right from the beginning.

2. Use Appropriate Data Types: Choose the correct data types for each column to reduce errors and automatically limit invalid entries.

3. Enforce Primary Keys Strictly: Make sure every table has a clear primary key. This helps uniquely identify records and prevents duplicate or missing values.

4. Maintain Referential Integrity: Set up foreign key relationships properly between tables to prevent disconnected or orphaned records and maintain data consistency.

5. Validate Data at Multiple Layers: Use both DBMS constraints and extra checks in the application to catch mistakes early and give clear feedback when data is invalid.

Real-World Applications of Integrity Constraints in DBMS

1. Banking Industry: Integrity constraints ensure that the account numbers, transaction IDs, and customer details are valid and unique. This helps to prevent errors like creating invalid transactions or duplicating accounts, keeping the banking data accurate and reliable.

2. Healthcare Systems: Medical records need unique identifiers to maintain entity integrity, and appointment bookings must connect to valid doctor records to follow referential integrity. These rules help to keep the patient data correct across the system.

3. E-commerce Platforms: In e-commerce systems, orders must be linked to valid customer accounts and correct product IDs. Integrity constraints ensure that prices are within a realistic range and quantities are not negative or illogical, helping to prevent errors and fraud in transactions.

4. Educational Institutions: Student databases in schools and colleges use constraints to maintain unique student IDs, valid course codes, and accurate enrollment details. This helps ensure that each student’s academic information is reliable, up-to-date, and easy to manage.

5. Inventory Management: In inventory systems, product codes and serial numbers must be unique to avoid mix-ups. Also, quantity values need to stay within logical and predefined limits, so that stock levels reflect actual inventory and support smooth business operations.

Start Learning SQL for Free!
Master SQL fundamentals at your own pace with our beginner-friendly course.
quiz-icon

Conclusion

Integrity constraints in DBMS help in maintaining the accuracy, consistency, and reliability of data in any database system. These constraints ensure that invalid or inconsistent data do not get stored in the database, as these constraints define rules for data types, uniqueness, relationships, and NULL values. Applying integrity constraints is very useful in avoiding database mistakes and ensuring that the system operates smoothly and gives accurate results. This plays a key role in real applications like banking, health care, e-commerce, education, and many other sectors that need secure and correct data management.

Take your skills to the next level by enrolling in the SQL Course today and gaining hands-on experience. Also, prepare for job interviews with SQL Interview Questions prepared by industry experts.

Integrity Constraints in DBMS – FAQs

1. Why are integrity constraints important in a DBMS?

Integrity rules make sure that data in a database is correct, consistent, and logically valid. They make invalid data recall and insertions impossible, hence providing security of the database quality and reliability.

2. Can a table have more than one constraint?

Yes, a table can contain several integrity constraints on its columns, including domain, primary, and foreign keys, and unique constraints according to the rules and data relationships imposed on the system.

3. What happens when a constraint is violated during an operation?

In case any such operation, such as insert, update, or delete, violates some defined integrity constraint, the DBMS refuses to allow it; it results in an error being raised, and guaranteed data integrity is preserved.

4. Is it possible to modify constraints after a table is created?

Yes, in most DBMSs, you are able to append, change, or remove constraints on a created table using SQL statements such as the SQL statement ALTER TABLE. This should, however, be done with caution so as not to have conflicts of data.

5. How do integrity constraints support database relationships?

Referential constraints help to make relationships among tables valid, therefore, integrity constraints are important. They will require the foreign keys to be the same as the existing primary keys, thereby ensuring consistency in related tables.

About the Author

Data Engineer, Tata Steel Nederland

As a skilled Data Engineer, Sahil excels in SQL, NoSQL databases, Business Intelligence, and database management. He has contributed immensely to projects at companies like Bajaj and Tata. With a strong expertise in data engineering, he has architected numerous solutions for data pipelines, analytics, and software integration, driving insights and innovation.

business intelligence professional