0% found this document useful (0 votes)
14 views6 pages

Lab Manual 09

The lab manual focuses on understanding SQL triggers, including their creation, modification, and practical applications. It outlines the types of triggers (DDL, DML, INSTEAD OF) and provides examples for each, emphasizing their role in data validation, automatic actions, and maintaining consistency. Additionally, it includes lab tasks for students to implement various triggers related to database management.

Uploaded by

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

Lab Manual 09

The lab manual focuses on understanding SQL triggers, including their creation, modification, and practical applications. It outlines the types of triggers (DDL, DML, INSTEAD OF) and provides examples for each, emphasizing their role in data validation, automatic actions, and maintaining consistency. Additionally, it includes lab tasks for students to implement various triggers related to database management.

Uploaded by

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

Database Administration & Management Lab

Lab Manual 09

Session: Fall-2024
Instructor: SHEZA SHABIR

Department of Informatics & Systems


School of System & Technology
University of Management & Technology Lahore Pakistan
Objective: (CLO1, CLO2, CLO3)

In today’s lab, we will:

 Understand what triggers are and why they are used in SQL.
 Learn how to create, modify, and delete triggers.
 Explore practical applications of triggers through examples.
What is a Trigger?

A trigger is a special type of stored procedure in a database that automatically executes when a
specified event occurs in the database. It is used to enforce business rules, maintain audit trails,
and automate repetitive tasks.

Why Do We Use Triggers?

 Data Validation: Enforce complex business rules that go beyond constraints.


 Automatic Actions: Automatically log changes or perform updates based on data
changes.
 Consistency: Maintain referential integrity across multiple tables.
 Auditing: Track changes for security and analysis purposes.

Syntax for Trigger:

CREATE TRIGGER trigger_name


ON table_name
AFTER | INSTEAD OF [INSERT, UPDATE, DELETE]
AS
BEGIN
-- Trigger logic here
END;

 Types of Triggers:

1. DDL Triggers: A DDL trigger executes when schema-level events occur, such as CREATE,
ALTER, or DROP.
Example:
Scenario: Prevent any table from being dropped in the database.
-- Create the DDL trigger
CREATE TRIGGER PreventTableDrop
ON DATABASE
FOR DROP_TABLE
AS
BEGIN
PRINT 'Dropping tables is not allowed.';
ROLLBACK;
END;

How to check?
DROP TABLE Employees; -- Error: Dropping tables is not allowed

2. DML Triggers: A DML trigger executes when a INSERT, UPDATE, or DELETE operation
occurs on a table.
Example:
Scenario: When an employee's salary is updated, print a message indicating the salary
change.
-- Step 1: Create the Employees table

CREATE TABLE Employees (


EmpID INT PRIMARY KEY,
Name NVARCHAR(50),
Salary INT
);
-- Step 2: Create the DML trigger
CREATE TRIGGER SalaryUpdateMessage
ON Employees
AFTER UPDATE
AS
BEGIN
PRINT 'Salary has been updated for one or more employees.';
END;

How to check?
--Step 01: Insert a record
INSERT INTO Employees (EmpID, Name, Salary)
VALUES (1, 'Alice', 50000);
--Step 02: Update the salary (this triggers the message)
UPDATE Employees
SET Salary = 55000
WHERE EmpID = 1;

3. INSTEAD OF Triggers: An INSTEAD OF trigger executes instead of the triggering SQL


statement.
Example:
Scenario: Prevent employees with a salary below 5000 from being inserted, but allow valid
salaries.

-- Step 1: Create the Employees table


CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Name NVARCHAR(50),
Salary INT
);

-- Step 2: Create the INSTEAD OF trigger


CREATE TRIGGER PreventLowSalary
ON Employees
INSTEAD OF INSERT
AS
BEGIN
-- Check salary condition
IF EXISTS (SELECT * FROM INSERTED WHERE Salary < 5000)
BEGIN
PRINT 'Salary must be at least 5000. Insert operation aborted.';
ROLLBACK;
END
ELSE
BEGIN
-- Insert valid data into the Employees table
INSERT INTO Employees (EmpID, Name, Salary)
SELECT EmpID, Name, Salary FROM INSERTED;
END
END;

How to check?
-- Test with an invalid salary
INSERT INTO Employees (EmpID, Name, Salary)
VALUES (1, 'Bob', 4000); -- This will fail
-- Test with a valid salary
INSERT INTO Employees (EmpID, Name, Salary)
VALUES (2, 'Alice', 6000); -- This will succeed

-- Check the table


SELECT * FROM Employees;

Lab Task
1. Create a DML Trigger to log a message when a student’s age is updated.
2. Create an INSTEAD OF Trigger to prevent courses with less than 1 credit from being
inserted.
3. Create a DDL Trigger to prevent any table from being dropped in the StudentsDB database.
4. Create a DML Trigger to prevent deleting students younger than 18.
5. Create a DML Trigger to log a message when a new student enrolls in a course.
6. Create a Trigger to count how many students are currently enrolled whenever a new
enrollment is added.
7. Create a Trigger to calculate the total credits of all courses whenever a new course is added.
8. Create a Trigger to calculate the average age of students whenever a student's age is
updated.
9. Create a Trigger to calculate how many students are enrolled in each course whenever a
new enrollment is added. Use JOIN to group results.
10. Create a Trigger to calculate the total number of students enrolled in all courses whenever
an enrollment is deleted.

You might also like