Chapter 6 DAatabaseTrigger 1 1
Chapter 6 DAatabaseTrigger 1 1
PL/SQL – Triggers
• Triggers are stored programs, which are automatically executed or
fired when some events occur.
• Triggers can be defined on the table, view, schema, or database with
which the event is associated.
• Triggers are used to specify certain integrity constraints and
referential constraints that cannot be specified using the constraint
mechanism of SQL.
Advance Database using PL/SQL
Unit 6 : Database Triggers
Advantage of Triggers
• Automation: Triggers automate tasks, reducing manual effort and ensuring consistent actions in
response to specific events.
• Enforcing Rules: Triggers enforce business rules, validating data and maintaining consistency within
the database.
• Data Integrity: Triggers prevent invalid changes, preserving data integrity and safeguarding against
corruption.
• Audit Trail: Triggers create an audit trail, tracking modifications and ensuring accountability for data
changes.
• Security: Triggers enhance security, validating user actions and restricting access to sensitive data.
• Simplicity: Triggers simplify complex operations, encapsulating intricate logic within the database.
• Performance: Triggers can enhance performance by reducing the number of interactions with the
database in certain scenarios.
Advance Database using PL/SQL
Unit 6 : Database Triggers
Types of Triggers
There are two main types of triggers based on the timing of their execution: AFTER Triggers and INSTEAD OF Triggers.
AFTER Triggers: These triggers execute after the triggering action (INSERT, UPDATE, DELETE)
has occurred. They are commonly used for tasks such as auditing, enforcing business rules, and
cascading changes to related tables.
Syntax:
CREATE TRIGGER trigger_name ON table_name
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
-- Trigger logic
END;
Advance Database using PL/SQL
Unit 6 : Database Triggers
Types of Triggers
There are two main types of triggers based on the timing of their execution: AFTER Triggers and INSTEAD OF Triggers.
INSTEAD OF Triggers: These triggers replace the triggering action with the logic defined inside the trigger. They are
often used with views and allow you to customize how data modifications are handled.
Syntax:
Types of Triggers
Triggers can also be classified based on the level at which they operate:
Row-Level Triggers: These triggers operate on each row affected by the triggering statement.
They can access the data of the row being modified and are defined using the FOR EACH
ROW clause (which is the default behavior in most databases).
Statement-Level Triggers: These triggers operate once for each triggering statement,
regardless of the number of rows affected. They do not have access to the individual rows
being modified and are defined without the FOR EACH ROW clause.
Advance Database using PL/SQL
Unit 6 : Database Triggers
Syntax of - Triggers
CREATE TRIGGER trigger_name ON table_name
AFTER INSERT, UPDATE, DELETE -- Specify the event(s) that will
activate the trigger
AS
BEGIN
-- Trigger logic goes here
END;
Advance Database using PL/SQL
Unit 6 : Database Triggers
Syntax,
CREATE TRIGGER trigger_name
ON table_name
AFTER INSERT
AS
-- Trigger logic here
Advance Database using PL/SQL
Unit 6 : Database Triggers
The trigger retrieves the CustomerName of the newly inserted row from the INSERTED
table and prints a message indicating the new customer's name.
Advance Database using PL/SQL
Unit 6 : Database Triggers
Example Based on MYSTORE-Customer table
we will create a trigger that stores transaction records of each insert operation on the Customer
To Enable a Trigger:
Advance Database using PL/SQL
Unit 6 : Database Triggers
To Delete or remove trigger
Advance Database using PL/SQL
Unit 6 : Database Triggers
Q5: If you have table customer , then write a trigger to prevent data to update and
delete.
Q6: Write a trigger to generate primary key value using a Lookup table
Q7: Write different between trigger and Procedures
Advance Database using PL/SQL
Unit 6 : Database Triggers
Q6: Write a trigger to generate primary key value using a Lookup table
CREATE TABLE lookup_table (
id INT PRIMARY KEY
);
CREATE TRIGGER generate_primary_key
BEFORE INSERT ON main_table
FOR EACH ROW
BEGIN
DECLARE next_id INT;
SELECT MAX(id) + 1 INTO next_id FROM lookup_table;
IF next_id IS NULL THEN
SET next_id = 1;
END IF;
SET NEW.id = next_id;
INSERT INTO lookup_table (id) VALUES (next_id);
End