explain integrity constraints with its type and examples.
Integrity constraints are rules that ensure the accuracy and consistency of data within a relational database. They are essential for maintaining the integrity of data, preventing errors, and ensuring that the database reflects real-world scenarios effectively. There are several types of integrity constraints, each serving a distinct purpose. Here’s an overview of the main types along with examples:
### 1. **Entity Integrity Constraint**
This constraint ensures that each entity (or row) in a database table is unique and can be uniquely identified by a primary key. It prohibits null values for primary key attributes.
- **Example:**
In a `Customer` table, the `CustomerID` could be the primary key. Each `CustomerID` must be unique and cannot be null.
```
CustomerID | Name | Email
------------|-----------|-------------------
1 | Alice | This email address is being protected from spambots. You need JavaScript enabled to view it.
2 | Bob | This email address is being protected from spambots. You need JavaScript enabled to view it.
```
### 2. **Referential Integrity Constraint**
This constraint maintains the consistency between two tables by ensuring that a foreign key in one table corresponds to a valid primary key in another table. It prevents orphan records.
- **Example:**
In a `Order` table, the `CustomerID` field is a foreign key that links to the `CustomerID` primary key in the `Customer` table. If a `CustomerID` is deleted in the `Customer` table, the corresponding entries in the `Order` table should also be deleted or set to null.
```
OrderID | CustomerID | OrderDate
---------|-------------|------------
101 | 1 | 2023-01-05
102 | 2 | 2023-01-06
```
### 3. **Domain Integrity Constraint**
This type of constraint ensures that all values in a particular column fall within a certain domain, such as data type, formats, or specific value ranges.
- **Example:**
If the `Age` column in a `Person` table is supposed to hold integer values between 0 and 120, a check constraint can be added.
```
PersonID | Name | Age
----------|-------|-----
1 | John | 25
2 | Jane | 150 -- This would violate the domain integrity constraint
```
### 4. **Unique Constraint**
This constraint ensures that all values in a column are distinct from one another, allowing for null values (unless specified otherwise). A unique constraint can be applied to a single column or a combination of columns.
- **Example:**
In a `User` table, the `Email` column can have a unique constraint to ensure no two users have the same email address.
```
UserID | Name | Email
--------|--------|---------------
1 | Alice | This email address is being protected from spambots. You need JavaScript enabled to view it.
2 | Bob | This email address is being protected from spambots. You need JavaScript enabled to view it.
3 | Alice | This email address is being protected from spambots. You need JavaScript enabled to view it. -- This would violate the unique constraint
```
### 5. **Check Constraint**
This constraint ensures that a specific condition is met for all values in a column. It helps enforce business rules at the database level.
- **Example:**
In a `Product` table, a check constraint can ensure that the `Price` of a product is always greater than zero.
```
ProductID | Name | Price
-----------|-----------|--------
1 | Widget | 25.00
2 | Gadget | -5.00 -- This would violate the check constraint
```
### Summary
By implementing these integrity constraints, databases can maintain high-quality, reliable data that reflects reality and adheres to business rules. They help enforce data validity, reduce redundancy, and ensure that relationships between different entities remain consistent.