Page 1 of 1

What is data integrity? Name and explain three types of data integrity constraints.

Posted: Tue May 20, 2025 10:35 am
by muskanislam44
Data integrity refers to the overall quality, accuracy, consistency, and reliability of data stored in a database. It ensures that the data is valid and adheres to predefined rules and constraints, maintaining its trustworthiness throughout its lifecycle. High data integrity is crucial for making informed decisions, ensuring the smooth operation of applications, and complying with regulatory requirements.



If data lacks integrity, it can lead to incorrect reports, faulty business decisions, and operational disruptions. Database management systems (DBMS) provide mechanisms to enforce data integrity through various constraints, which are rules that restrict the type of data that can be entered into a table or define relationships between tables.


Three Types of Data Integrity Constraints:
Entity Integrity:
Entity integrity ensures that each row in a table is uniquely identified bahrain number database and that this identification is always present. It is enforced by the Primary Key constraint.


Explanation:

Uniqueness: Every value in the primary key column(s) must be unique across all rows in the table. This prevents duplicate records and ensures that each entity (e.g., a specific customer, a unique product) can be distinguished from all others.

Non-Nullability: The primary key column(s) cannot contain NULL values. This means that every record must have a valid and defined identifier. If a primary key were allowed to be NULL, it would violate its purpose of unique identification, as NULL represents an unknown or undefined value.

Example: In a Customers table, CustomerID is typically the primary key. Entity integrity dictates that:

No two customers can have the same CustomerID.
No customer can have a NULL value for their CustomerID.
Importance: This constraint is fundamental as it guarantees that every individual entity represented in the table can be uniquely identified and located. Without it, operations like updating or deleting specific records would be ambiguous and unreliable.


Referential Integrity:
Referential integrity maintains the consistency of relationships between tables. It is enforced by the Foreign Key constraint.


Explanation:

A foreign key in one table (the "child" table) refers to the primary key in another table (the "parent" table).
Referential integrity ensures that for every value in the foreign key column(s) of the child table, there must be a matching primary key value in the parent table, or the foreign key value must be NULL (if the foreign key is optional).
This prevents "orphaned" records in the child table that refer to non-existent records in the parent table.
It also defines rules for what happens when a referenced primary key in the parent table is updated or deleted (e.g., CASCADE, SET NULL, RESTRICT).
Example: Consider an Orders table (child) and a Customers table (parent). The Orders table has a CustomerID column, which is a foreign key referencing the CustomerID (primary key) in the Customers table.


Referential integrity ensures you cannot place an order for a CustomerID that does not exist in the Customers table.
If a customer record is deleted from the Customers table, referential integrity rules might prevent the deletion (if orders exist for that customer), set the CustomerID in related orders to NULL, or automatically delete the related orders (depending on the defined action).
Importance: Referential integrity is crucial for maintaining accurate relationships between data, preventing inconsistencies that can arise from changes in related tables, and ensuring that all cross-table references are valid.

Domain Integrity:
Domain integrity ensures that all values in a column are valid and adhere to a predefined set of allowable values or data types. It is enforced by constraints such as data types, CHECK constraints, and NOT NULL constraints (when applied to non-primary key columns).

Explanation:

Data Types: Each column is assigned a specific data type (e.g., INT, VARCHAR, DATE, BOOLEAN), which restricts the kind of data that can be stored in it. For example, a DATE column will only accept valid date formats.
CHECK Constraints: These allow you to define a specific rule or condition that all data entered into a column must satisfy. For instance, a CHECK constraint can ensure that an Age column only accepts values greater than 0, or a Status column only accepts 'Active', 'Inactive', or 'Pending'.

NOT NULL Constraints: While primary keys are implicitly NOT NULL, any other column can also have a NOT NULL constraint, ensuring that a value must always be provided for that specific column.
Example: In a Products table:

The Price column might have a DECIMAL(10, 2) data type, ensuring only numeric values with two decimal places are accepted.
A CHECK constraint on the StockQuantity column might ensure that StockQuantity >= 0.
The ProductName column might have a NOT NULL constraint, meaning every product must have a name.
Importance: Domain integrity guarantees that the data stored in each column is meaningful, accurate, and consistent with its intended purpose, preventing illogical or erroneous data entries.