Assignment 10

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

ASSIGNMENT NO.

10

Aim

Write a database trigger (Row level and statement level)

Objective

To study and implement database trigger

Theory

• Stored programs that are executed in respnse to some kind of event that occures in
database.
• Triggers fires in response to a DML statement (insert, update delete) on specified table.
• Powerful mechanism for ensuring the integrity of data

Create trigger trigger_name


{before|after}
{update|insert|delete}
On table_name
For each row
Trigger statements

• Before|after specifies weather triger fires before of after the DML statement itself has
been executed.
• Update|insert|delete specifies DML statement to which trigger is associated
• On table_name associates the trigger with a specific table
• For each row indicates that the trigger will be executed once for every row affected by
the DML statement
• With after we are not able to modify the values about to modify the values about to be
inserted into or updated with the table in question

Example of Row Level Trigger

Create trigger acctbalance


before update on account
For each row
Begin
Declare dummy int;
If new.balance < 0 then
Set new.balance=null;
End if;
End $$
Example of Statement Level Trigger

Create trigger acctbalance


before update on account
when new.balance < 0
Begin
Update account set new.balance = null where accno=new.accno;
End if;
End $$

CREATE TABLE employees_audit (


id int(11) NOT NULL AUTO_INCREMENT,
employeeNumber int(11) NOT NULL,
lastname varchar(50) NOT NULL,
changedon datetime DEFAULT NULL,
action varchar(50) DEFAULT NULL,
PRIMARY KEY (id)
);

DELIMITER $$
CREATE TRIGGER before_employee_update
BEFORE UPDATE ON employees
FOR EACH ROW BEGIN

INSERT INTO employees_audit


SET action = 'update',
employeeNumber = OLD.emp_no,
lastname = OLD.lastname,
changedon = NOW();
END$$
DELIMITER ;

Output

 Row level and statement level trigger.

References:
1. Raghu Ramkrishanan, Johannes Gehrke 4 th Edition “Database Management Systems” 2. Avi
Silberschatz , Henry F. Korth , S. Sudarshan, “Database System Concepts, Sixth Edition”, ISBN-
13: 978-93-3290-138-4, MCGraw Hill
Frequently Asked Questions

Q. Questions BT CO
No
1 Explain trigger concept? 2 2
2 Explain EER features? 2 2

Guidelines for Students


The experiments should be completed and get checked by the concerned teacher in the
lab on or before the date of submission. After which the experiment will not be signed.

Every experiment must be included in the file in following format.


a. Aim: In this section write complete objective of the program you are going to make in
the lab. This section specifies the complete description of the including problem analysis,
input description, method used, fundamental concept and desired output format.
b. Theory: Write brief theory related to practical.
c. Algorithm: Write Algorithm for given task.
d. Input: Write input test data/ or program that are used to test program objective to see
whether program is achieving the given objective or not.
e. Output: describe the results in few lines
f. Conclusion: Write complete conclusion whether what the student has learned from this
experiment.
g. Source Code: Submit in the form of soft copies.

 Marking criteria.
Experiment completion (Timely)
Lab file (neatness and regularity)
Viva (from time to time)
Mock Practical Exam
Exam (end term): Practical + Viva
 Assessment Methodology
Timely completion of assignment- 2marks
Program demonstration- 4 marks
Viva-voce -2 marks
Timely submission of journal- 2 marks

You might also like