Data integrity ensures the accuracy, consistency, and reliability of data in a database. Constraints are rules applied at the database level to enforce data integrity, ensuring that the data adheres to specific standards and logical relationships. This chapter provides a detailed explanation of Data Integrity and Constraints, progressing from basic concepts to advanced topics.
Data integrity is the practice of maintaining and ensuring the accuracy, consistency, and reliability of data throughout its lifecycle. It ensures that:
Constraints are rules applied to table columns to restrict the type of data that can be stored. These include:
Ensures that a column cannot contain NULL
values.
CREATE TABLE employees (
id INT NOT NULL,
name VARCHAR(100) NOT NULL,
salary DECIMAL(10, 2) NOT NULL
);
Here, id
, name
, and salary
cannot have null values.
Ensures all values in a column are unique.
Example:
CREATE TABLE customers (
customer_id INT UNIQUE,
email VARCHAR(255) UNIQUE
);
customer_id
or email
.Uniquely identifies each record in a table. Combines the properties of NOT NULL
and UNIQUE
.
Example:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT NOT NULL
);
Ensures referential integrity by linking one table to another.
Example:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
The customer_id
in orders
must match an existing customer_id
in customers
.
Enforces a condition on data values.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT NOT NULL
);
price
must always be greater than 0, and quantity
cannot be negative.Specifies a default value if none is provided.
Example:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT NOT NULL
);
If created_at
is not specified, it defaults to the current timestamp.
A primary key that consists of multiple columns.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT NOT NULL
);
Combines student_id
and course_id
to uniquely identify each record.
Cascading actions ensure data consistency when rows in related tables are updated or deleted.
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
ON DELETE CASCADE
: Deletes orders when a customer is deleted.ON UPDATE CASCADE
: Updates related rows if the customer_id
changes.You can enforce complex conditions using CHECK
.
CREATE TABLE employees (
id INT PRIMARY KEY,
age INT CHECK (age >= 18 AND age <= 65),
salary DECIMAL(10, 2) CHECK (salary >= 30000)
);
age
is between 18 and 65 and salary
is at least 30,000.Some databases (like PostgreSQL) allow deferring constraint checks until a transaction commits.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT NOT NULL
);
You can add constraints to existing tables using ALTER TABLE
.
ALTER TABLE employees
ADD CONSTRAINT chk_salary CHECK (salary > 0);
Remove constraints if needed.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT NOT NULL
);
SHOW CREATE TABLE employees;
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE NOT NULL,
customer_id INT NOT NULL
);
Data integrity and constraints are vital for building reliable and robust databases. By enforcing rules at the database level, you minimize data anomalies, maintain consistency, and enhance application reliability. This chapter provided a thorough understanding of constraints, from their basics to advanced applications, with practical examples. Properly using constraints ensures that your database remains a trustworthy source of truth. Happy coding !❤️