0% found this document useful (0 votes)
2 views

Integrity Constraints

The lesson covers integrity constraints in relational databases, defining them as rules that ensure data accuracy and consistency. It distinguishes between static constraints (like primary keys and foreign keys) and dynamic constraints (like check constraints and triggers), emphasizing their importance in maintaining data integrity. Practical activities are included to reinforce the application of these constraints in database design.

Uploaded by

monthec39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Integrity Constraints

The lesson covers integrity constraints in relational databases, defining them as rules that ensure data accuracy and consistency. It distinguishes between static constraints (like primary keys and foreign keys) and dynamic constraints (like check constraints and triggers), emphasizing their importance in maintaining data integrity. Practical activities are included to reinforce the application of these constraints in database design.

Uploaded by

monthec39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lesson Notes: Integrity Constraints

Course Topic: Integrity Constraints (Static, Dynamic, etc.)

1. Introduction to Integrity Constraints (30 Minutes)


Definition of Integrity Constraints
 Integrity constraints are rules that ensure the accuracy and consistency of data in a
relational database.
 They define the conditions that database data must satisfy to preserve its integrity.

Importance of Integrity Constraints


 Maintain data accuracy: Prevent invalid data entry.
 Ensure consistency: Keep data valid across tables.
 Support data security: Restrict unauthorized operations.
 Enhance data reliability: Avoid errors due to faulty user input or design flaws.

Types of Integrity Constraints


1. Static Constraints:
o Rules enforced at the database structure level.
o Examples: Primary keys, foreign keys, unique constraints.
2. Dynamic Constraints:
o Rules enforced during database operations (inserts, updates, or deletes).
o Examples: Check constraints, triggers, and stored procedures.

2. Types of Integrity Constraints (1 Hour)

2.1 Static Constraints


1. Primary Key Constraint
 Uniquely identifies a record in a table.
 Ensures that no two rows can have the same primary key value.
 Example:
StudentI Nam Age
D e
1 Alice 20
 Rule: StudentID cannot be null or duplicate.

2. Unique Constraint
 Ensures that all values in a column are distinct.
 Example: Email addresses in a student database must be unique.

3. Foreign Key Constraint


 Links two tables by enforcing a relationship between a column in one table and the
primary key in another.
 Ensures referential integrity.
Example:
OrderID ProductID
1 101
 ProductID in the Orders table must exist in the Products table.

2.2 Dynamic Constraints


1. Check Constraint
 Ensures that a column’s value meets a specific condition.
 Example: Salary values in an Employees table must be greater than zero.
SQL Implementation:
sql
Copy code
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
Salary DECIMAL CHECK (Salary > 0)
);

2. Default Constraint
 Automatically assigns a default value to a column if no value is specified.
 Example: A Status column defaults to “Pending” in an Orders table.

3. Triggers
 Execute specific actions when certain database events occur (e.g., updates or
deletions).
 Used for enforcing business rules dynamically.
Example:
Automatically log the time of a record update.

3. Comparison of Static and Dynamic Constraints (30 Minutes)


Feature Static Constraints Dynamic Constraints
Definition Enforced on the database schema. Enforced during operations.
Examples Primary keys, foreign keys. Check constraints, triggers.
Scope Structural integrity. Operational integrity.
Flexibilit Less flexible. Highly adaptable.
y

4. Practical Implementation (1 Hour)

Activity 1: Enforcing Static Constraints


Task:
Create a database for a library system with the following constraints:
1. Each book must have a unique BookID.
2. Each BorrowerID must reference an existing record in the Borrowers table.
Steps:
1. Create tables: Books, Borrowers, and Loans.
2. Add primary and foreign key constraints.
Example SQL:
sql
Copy code
CREATE TABLE Borrowers (
BorrowerID INT PRIMARY KEY,
Name VARCHAR(100)
);

CREATE TABLE Books (


BookID INT PRIMARY KEY,
Title VARCHAR(200)
);

CREATE TABLE Loans (


LoanID INT PRIMARY KEY,
BorrowerID INT REFERENCES Borrowers(BorrowerID),
BookID INT REFERENCES Books(BookID)
);

Activity 2: Enforcing Dynamic Constraints


Task:
1. Add a check constraint to ensure borrowed books cannot exceed 5 per borrower.
2. Create a trigger to update a log table every time a book is borrowed.
Example SQL:
sql
Copy code
CREATE TABLE BorrowerLog (
LogID INT PRIMARY KEY,
BorrowerID INT,
BorrowedDate DATETIME
);

CREATE TRIGGER LogBorrower


AFTER INSERT ON Loans
FOR EACH ROW
BEGIN
INSERT INTO BorrowerLog (BorrowerID, BorrowedDate)
VALUES (NEW.BorrowerID, NOW());
END;

5. Wrap-Up (30 Minutes)


Key Points Recap
1. Static constraints enforce structural integrity (e.g., primary keys, foreign keys).
2. Dynamic constraints enforce operational integrity (e.g., check constraints, triggers).
3. Proper use of constraints ensures data consistency and accuracy.

Homework
1. Design a database for a student grading system.
o Include static and dynamic constraints (e.g., grades between 0 and 100).
2. Write SQL scripts to enforce the constraints.
This lesson ensures students understand both theoretical and practical aspects of integrity
constraints, aligning with the Cameroon HND program.

You might also like