Integrity Constraints explain everything in it

5 views 25 slides Apr 27, 2025
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

Integrity Constraints explain everything in it
It's very useful


Slide Content

Integrity Constraints Submitted by: Fatima amir 22011556-010 Dilbaz sultan 22011556-012 Kinza shahzadi 22011556-054 Instructor : Miss Sania Azhar

Integrity Constraints it's a core concept in database management systems (DBMS). They are rules enforced on data in a database to ensure its accuracy consistency Reliability These are enforced automatically by the DBMS ( like MySQL, PostgreSQL , Oracle, etc.).

Types of Integrity Constraints Entity Integrity Referential Integrity Domain Integrity User-Defined Integrity

Entity Integrity Primary Key constraint Primary Key is a special column Ensures each row is uniquely identifiable It can’t be empty (NULL) and must be unique for every row . ID is the Primary Key ID NAME 1 2 3 Alice Bob Charlie

A Student table in a school database: CREATE TABLE Student ( StudentID INT PRIMARY KEY, Name VARCHAR(100 ), ); StudentID uniquely identifies each student No two students can have the same StudentID Every student must have one Maintains data reliability and accuracy

Referential Integrity Ensures relationships between tables remain consistent Enforced using Foreign Keys CREATE TABLE Department ( DeptID INT PRIMARY KEY, DeptName VARCHAR(100) ); CREATE TABLE Employee ( EmpID INT PRIMARY KEY , Name VARCHAR(100), DeptID INT, FOREIGN KEY ( DeptID ) REFERENCES Department( DeptID ) ); Employee.DeptID must match a DeptID in Department Keeps data across tables linked and accurate

Domain Integrity Ensures that each column contains only valid data Defines a “domain” or allowed set of values for a column Data types (e.g., INT, VARCHAR, DATE) CHECK constraints (e.g., Salary > 0) Default values (e.g., status = ‘Active’) CREATE TABLE Product ( ProductID INT PRIMARY KEY, Price DECIMAL(10,2) CHECK (Price > 0), Status VARCHAR(10) DEFAULT 'Available' ); improves data quality and reliability

User-Defined Integrity Rules that are specific to the business or application Not covered by default system constraints (like PK or FK ) Enforce by: Using CHECK constraints CREATE TABLE Employee ( EmpID INT PRIMARY KEY , Name VARCHAR(100), Salary DECIMAL(10,2) CHECK (Salary > 0) ); Helps maintain meaningful and valid data

Constraints in SQL Constraints : are rules applied to columns or tables in a database to ensure that the data stored is accurate , consistent , and valid . Purpose of Constraints: To restrict the type of data that can be inserted into the database. To enforce business rules and maintain data integrity

Types of Constraints: Primary Key: Uniquely identifies each row in a table. Foreign Key: Ensures the relationship between two tables is valid. Check: Validates that data meets specific conditions (e.g., salary > 0). Not Null: Ensures that a column cannot have NULL values. Unique: Ensures all values in a column are unique . SQL code snippets: CREATE TABLE Employee ( ID INT PRIMARY KEY, Name VARCHAR(100), DepartmentID INT, FOREIGN KEY ( DepartmentID ) REFERENCES Department(ID), CHECK (Salary > 0) );

Primary Key constraints Ensures unique identification for each row in a table. Cannot be NULL . CREATE TABLE Employee ( ID INT PRIMARY KEY, Name VARCHAR(100) );

Foreign Key Links one table to another. Ensures data integrity by matching values to a primary key in another table. Example : CREATE TABLE Employee ( EmpID INT PRIMARY KEY, DeptID INT, FOREIGN KEY ( DeptID ) REFERENCES Department( DeptID ) );

Unique constraints ensures all values in a column are unique . Example : CREATE TABLE User ( UserID INT PRIMARY KEY, Email VARCHAR(100) UNIQUE );

Check constraints Validates data against a condition (e.g., positive salary ). Example : CREATE TABLE Employee ( ID INT PRIMARY KEY, Salary DECIMAL(10, 2) CHECK (Salary > 0) );

Constraints Matter Prevents data anomalies Improves data quality Reduces bugs in applications

Real-Life Applications of Integrity Constraints : Integrity constraints aren’t just theory they help run the real world by protecting important data in many systems . Banking Systems Constraint : Balance cannot go below zero . Why? Prevents overdrafts unless allowed(only if overdraft protection is enabled), protects customer accounts . CHECK (Balance >= 0)

why is it important? It prevents errors or fraud , like someone accidentally or purposely trying to take out more money than they have. It keeps the bank’s records safe and accurate , protecting both the customer and the bank .

E_commerce customer Table Imagine an online shopping system where: Every order must be placed by a valid customer Every product must have valid stock Prices and quantities must be logical customerID Name Email 1 2 Alice Bob [email protected] [email protected]

Product Table productID productname stock price 112 113 Keyboard mouse 5 1000 500 Constraints : Stock >= 0 (Check Constraint) Price > 0 (Check Constraint) ProductID is Primary Key

OrderID CustomerID ProductID Quantity TotalAmount 201 202 1 2 101 102 1 1 1000 500 Foreign Key Constraints: CustomerID must exist in Customer Table ProductID must exist in Product Table Check Constraint: Quantity > 0 Order Table

E-commerce Platforms Constraint : Stock quantity cannot be negative . Why ? Ensures you don’t sell products you don’t have. Example: CHECK (Stock >= 0 ) Important : Prevents overselling maintain accurate inventory levels. Protects customer trust

Example Use in SQL: CREATE TABLE Product ( ProductID INT PRIMARY KEY, ProductName VARCHAR(100), Stock INT CHECK (Stock >= 0) ); If someone tries to update Stock to -1 , the database will reject the operation to keep the data valid.

Best Practices Always define necessary constraints Don't overuse constraints that limit flexibility Test constraint rules carefully

Summary Ensure Consistency Avoid Logical Errors Support Business Rules Help in Decision-Making Build Trust

Questions Thank You ! Any questions?