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

DBMS Experiment 7

The document outlines the implementation of triggers in a database management system (DBMS), detailing their purpose, syntax, and examples for INSERT, UPDATE, and DELETE operations. It includes specific SQL commands for creating triggers, modifying them, and logging actions in associated tables. Additionally, it provides examples of creating triggers for employee and bus tables, along with instructions for displaying existing triggers in the database.

Uploaded by

sathwikgoundla1
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)
3 views

DBMS Experiment 7

The document outlines the implementation of triggers in a database management system (DBMS), detailing their purpose, syntax, and examples for INSERT, UPDATE, and DELETE operations. It includes specific SQL commands for creating triggers, modifying them, and logging actions in associated tables. Additionally, it provides examples of creating triggers for employee and bus tables, along with instructions for displaying existing triggers in the database.

Uploaded by

sathwikgoundla1
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/ 6

EXPERIMENT 7: TRIGGERS

AIM: To implement the concept of triggers -Insert, Update, Delete


TRIGGERS: In DBMS, a trigger is a special type of stored procedure that automatically
executes in response to specific events on a table or view. These events can include INSERT,
UPDATE, or DELETE operations on the table. Triggers are often used to maintain data integrity,
audit changes, or automate related tasks.
Syntax:
BEGIN
-- Define the trigger
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
-- Trigger logic or actions to be performed
-- Example: Updating a related table, logging, validating data, etc.

IF (condition) THEN
-- Perform specific actions based on conditions
-- Example: Update a record, insert into another table, etc.
END IF;

END;

END;

 CREATE TRIGGER: This is the SQL command used to create a trigger.

 trigger_name: This is the name of the trigger. You can choose any name you like, as long as
it follows the naming conventions of your DBMS.

 [BEFORE|AFTER]: This specifies whether the trigger should be activated before or after
the triggering event.

 [INSERT|UPDATE|DELETE]: This specifies the event that will trigger the execution of
the trigger. You can choose one or more of these options depending on the specific trigger
you want to create.

 ON table_name: This specifies the table that the trigger will be associated with.

 [FOR EACH ROW]: This specifies that the trigger will be executed once for each row that
is affected by the triggering event.
 BEGIN and END: This is where you will put the code that should be executed when the
trigger is activated. The code inside the BEGIN and END blocks can be any SQL statements
that you want to execute when the trigger is activated. The exact syntax and functionality of
triggers can vary depending on the specific DBMS you are using.

Example-1:

CREATE A TABLE EMPLOYEE USING


CREATE TABLE employees (
employee_id INT,
name VARCHAR(100),
department VARCHAR(100)
);

CREATE A TABLE EMPLOYEE_LOG USING


CREATE TABLE employees_log (
employee_id INT,
name VARCHAR(100),
action VARCHAR(100)
);

INSERT SOME TUPLES INTO EMPLOYEE TABLE USING

INSERT INTO employees (employee_id, name, department)


VALUES (1, 'Alice', 'HR'), (2, 'Bob', 'IT'), (3, 'Charlie', 'Sales'), (4, 'David', 'IT');

1. Create the trigger to log Insertions

Creating a trigger involves defining when it should be executed and what actions it should
perform. The example above shows how to make a trigger that logs inserts newly added
employee details into an employees_log table.

CREATE TRIGGER after_employee_insert


AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_log (employee_id, name, action)
VALUES (NEW.employee_id, NEW.name, 'INSERTED SUCCESSFULLY');
END;

Perform an INSERT operation on employees table:


INSERT INTO EMPLYEES VALUES(5,”Kumar”,”HR”) ;
2. Modifying triggers

To modify a trigger, you must drop the existing one and create a new one with the desired
changes. Here’s how you can do it:

DROP TRIGGER IF EXISTS log_changes;


CREATE TRIGGER log_changes
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_log (employee_id, name, action)
VALUES (OLD.employee_id, OLD.name, 'UPDATED SUCCESSFULLY ');
END;

Perform an UPDATE operation on the Emplyees table:


UPDATE Emplyees SET name='Ramu' WHERE employee_id=3 ;

3. Create the trigger to log deletions:

CREATE TRIGGER after_employee_delete


AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_log (employee_id, name, action)
VALUES (OLD.employee_id, OLD.name, 'deleted');
END;

Perform DELETE operation on bus:


DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID=5;

4. Displaying existing triggers

You can view existing triggers in a database using specific queries based on your SQL database
management system (DBMS). For instance, in MySQL:

SHOW TRIGGERS;

OUTPUT:

Select * from EMPLOYEE_LOG;


Example-2:

1. Create a table with the schema Bus(busno, source, destination, capacity)


CREATE TABLE BUS(BUSNO VARCHAR(10) NOT NULL, SOURCE VARCHAR(10),
DESTINATION VARCHAR(10), CAPACITY INT(2), PRIMARY KEY(BUSNO));

2. Insert values
INSERT INTO BUS VALUES('AP123','HYD','CHENNAI','40');
// Insert at least 10 records//
3. Create an Audit table for the bus to track the actions on the table using Triggers
concept. ( Schema : Bus_Audit1(ID, Source, Changedone, Action))
CREATE TABLE BUS_AUDIT1 (ID INT NOT NULL AUTO_INCREMENT,
SOURCE VARCHAR (10) NOT NULL, CHANGEDONE DATETIME DEFAULT
NULL, ACTION VARCHAR (10) DEFAULT NULL, PRIMARY KEY (ID));

4. Creating UPDATE Trigger:


DELIMITER $$
CREATE TRIGGER BEFORE_BUS_DELETE
BEFORE DELETE ON BUS
FOR EACH ROW
BEGIN
INSERT INTO BUS_AUDIT1
SET action='delete',
source=OLD.source,
changedone=NOW();
END$$

Perform an UPDATE operation on the bus table:


UPDATE BUS SET SOURCE='KERALA' WHERE BUSNO='AP123' ;
5. Creating INSERT Trigger:
CREATE TRIGGER BEFORE_BUS_INSERT
BEFORE INSERT ON BUS
FOR EACH ROW
BEGIN
INSERT INTO BUS_AUDIT1
SET action='Insert',
source=NEW.source,
changedone=NOW();
END$$

Perform an INSERT operation on bus:


INSERT INTO BUS VALUES('AP789','VIZAG','HYDERABAD',30) ;

6. Create DELETE Trigger:


CREATE TRIGGER BEFORE_BUS_DELETE
BEFORE DELETE ON BUS
FOR EACH ROW
BEGIN
Insert into bus_audit1
SET action='delete’,
source=old.source,
changedone=NOW();
END$$

Perform DELETE operation on bus:


DELETE FROM BUS WHERE SOURCE=’HYDERABAD’;

4. Displaying existing triggers


You can view existing triggers in a database using specific queries based on your SQL database
management system (DBMS). For instance, in MySQL:

SHOW TRIGGERS;
OUTPUT:

Select * from bus_audit1;

RESULT: The Student is able to work on Triggers to create an active database.


VIVA- VOCE
1. DefineTRIGGER?
2. List the types of triggers?
3. List the trigger timings?
4. Is it possible to create a trigger on views?
5. Outline row and statement trigger?

You might also like