Artificial Intelligence
Please fill the required field.

what is integrity constraints in dbms? explain with examples.


Integrity constraints in Database Management Systems (DBMS) are rules that ensure the accuracy and consistency of data within a database. They enforce certain conditions on the data stored in the database to maintain its integrity over time. These constraints help prevent the entry of invalid data, ensuring that the data adheres to defined standards. Here are the main types of integrity constraints, along with explanations and examples: ### 1. **Entity Integrity Constraint** This constraint ensures that each table has a primary key and that the primary key is unique and not null. This guarantees that each record within the table can be uniquely identified. **Example:** If you have a `Students` table: ```plaintext | StudentID | Name | Age | |-----------|---------|-----| | 1 | Alice | 20 | | 2 | Bob | 21 | | NULL | Charlie | 22 | <-- This violates entity integrity (StudentID cannot be NULL) ``` In this example, `StudentID` must be unique and cannot be NULL. ### 2. **Referential Integrity Constraint** This constraint ensures that a foreign key in one table points to a valid primary key in another table. It maintains relationships between tables. **Example:** Suppose you have two tables: `Courses` and `Enrollments`: - `Courses` Table: ```plaintext | CourseID | CourseName | |----------|--------------| | 101 | Mathematics | | 102 | Biology | ``` - `Enrollments` Table: ```plaintext | EnrollmentID | CourseID | StudentID | |--------------|----------|-----------| | 1 | 101 | 1 | | 2 | 103 | 2 | <-- This violates referential integrity (CourseID 103 does not exist in Courses) ``` In this example, `CourseID` in `Enrollments` must reference a valid `CourseID` in the `Courses` table. ### 3. **Domain Integrity Constraint** This constraint ensures that data values are of a specific type and fall within a defined range. It enforces rules on what values can be stored in a field. **Example:** In a `Employees` table: ```plaintext | EmployeeID | Name | Salary | |-------------|---------|--------| | 1 | Alice | 50000 | | 2 | Bob | -1000 | <-- This violates domain integrity (Salary cannot be negative) ``` The `Salary` field should only contain non-negative values. You might define a rule that enforces this constraint. ### 4. **User-Defined Integrity Constraint** These are custom rules defined by users based on specific business requirements. They might involve a combination of rules that do not fall into the other categories. **Example:** Assume an online store database with an `Orders` table: ```plaintext | OrderID | CustomerID | Amount | |---------|------------|--------| | 1 | 501 | 150.00 | | 2 | 502 | 50.00 | | 3 | 501 | 200.00 | ``` A user-defined constraint might specify that a customer can only place a maximum of two orders per day. This rule would need to be enforced through custom application logic, as it's beyond basic database constraints. ### Summary Integrity constraints are crucial in maintaining the quality of the data within a database. They help prevent data anomalies and ensure that the data in the database reflects the real-world scenarios they are modeling. By implementing these constraints, organizations can ensure that their databases are reliable, consistent, and valid.