Lecture 1.3 Triggers and Functions
Lecture 1.3 Triggers and Functions
Database
Management
System
UNIT-I [10h]
Overview of Databases: Database concepts, DBMS, Data Base System Architecture (Three
Level ANSI-SPARC Architecture), Advantages and Disadvantages of DBMS, Data Independence,
DBA and Responsibilities of DBA, Relational Data Structure, Keys, Relations, Attributes, Schema and
Instances, Referential integrity, Entity integrity.
Data Models: Relational Model, Network Model, Hierarchical Model, ER Model: Design,
issues, Mapping constraints, ER diagram, Comparison of Models.
UNIT-II [10h]
Functional dependencies and Normalization: Functional dependencies, Decomposition, Full
Functional Dependency (FFD), Transitive Dependency (TD), Join Dependency (JD), Multi-valued
Dependency (MVD), Normal Forms (1NF, 2NF, 3NF, BCNF), De-normalization.
Database Security: Introduction, Threats, Counter Measures.
Control Structures: Introduction to conditional control, Iterative control and sequential control
statements, Cursors, Views.
3
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
UNIT-III [10h]
Package, Procedures and Triggers: Parts of procedures, Parameter modes, Advantages of
procedures, Syntax for creating triggers, Types of triggers, package specification and package body,
developing a package, Bodiless package, Advantages of packages.
Transaction Management and Concurrency Control: Introduction to Transaction Processing,
Properties of Transactions, Serializability and Recoverability, Need for Concurrency Control, Locking
Techniques, Time Stamping Methods, Optimistic Techniques and Granularity of Data items.
Database Recovery of database: Introduction, Need for Recovery, Types of errors, Recovery
Techniques.
4
University Institute of Engineering (UIE)
Department of Computer and Communication Engineering (CCE)
Learning Objective
Learning Outcome
• Understanding the concept of procedures with example.
• Discuss the various parameters and its types.
• Understanding the use of functions in Data Base Management
System.
• Discuss the concepts of Trigger and its various types.
• Discuss the concepts of packages and its advantages
FUNCTIONS
The syntax for creating a function is as follows:
Example
CREATE OR REPLACE FUNCTION show_description
(i_course_no number)
RETURN varchar2
AS v_description varchar2(50);
BEGIN
SELECT description
INTO v_description
FROM course
WHERE course_no = i_course_no;
RETURN v_description;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN('The Course is not in the database');
WHEN OTHERS
THEN
RETURN('Error in running show_description');
END;
SET SERVEROUTPUT ON
DECLARE
v_description VARCHAR2(50);
BEGIN
v_description := show_description(&sv_cnumber);
DBMS_OUTPUT.PUT_LINE(v_description);
END;
In a SQL statement
Functions
• Functions are declared using the following syntax:
Example of Functions
Example of Functions
TRIGGERS
A database trigger is a stored PL/SQL program unit associated with a
specific database table. ORACLE executes (fires) a database trigger
automatically when a given SQL operation (like INSERT, UPDATE or
DELETE) affects the table. Unlike a procedure, or a function, which must be
invoked explicitly, database triggers are invoked implicitly.
TRIGGERS
• Database triggers can be used to perform any of the following:
• Audit data modification
• Log events transparently
• Enforce complex business rules
• Derive column values automatically
• Implement complex security authorizations
• Maintain replicate tables
TRIGGERS
You can associate up to 12 database triggers with a given table. A database
trigger has three parts: a triggering event, an optional trigger constraint,
and a trigger action.
Triggers Syntax
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE|AFTER} triggering_event ON table_name
[FOR EACH ROW]
[WHEN condition]
DECLARE
Declaration statements
BEGIN
Executable statements
EXCEPTION
Exception-handling statements
END;
TRIGGERS
• The trigger_name references the name of the trigger.
• BEFORE or AFTER specify when the trigger is fired (before or after
the triggering event).
• The triggering_event references a DML statement issued against the
table (e.g., INSERT, DELETE, UPDATE).
• The table_name is the name of the table associated with the trigger.
• The clause, FOR EACH ROW, specifies a trigger is a row trigger and
fires once for each modified row.
• A WHEN clause specifies the condition for a trigger to be fired.
• Bear in mind that if you drop a table, all the associated triggers for the
table are dropped as well.
TYPES OF TRIGGERS
• Triggers may be called BEFORE or AFTER the following events:
• INSERT, UPDATE and DELETE.
• The before/after options can be used to specify when the trigger body
should be fired with respect to the triggering statement. If the user
indicates a BEFORE option, then Oracle fires the trigger before
executing the triggering statement. On the other hand, if an AFTER is
used, Oracle fires the trigger after executing the triggering statement.
TYPES OF TRIGGERS
A trigger may be a ROW or STATEMENT type. If the statement FOR
EACH ROW is present in the CREATE TRIGGER clause of a trigger, the
trigger is a row trigger. A row trigger is fired for each row affected by an
triggering statement.
A statement trigger, however, is fired only once for the triggering statement,
regardless of the number of rows affected by the triggering statement
END IF;
END;
/
The above example shows a trigger that limits the DML actions to the
employee table to weekdays from 8.30am to 6.30pm. If a user tries to
insert/update/delete a row in the EMPLOYEE table, a warning message will
be prompted.
University Institute of Engineering (UIE)
Department of Computer and Communication Engineering (CCE)
TYPES OF TRIGGERS
SQL> DELETE FROM employee WHERE emp_last_name = 'Joshi';
1 row deleted.
SQL> SELECT * FROM xemployee;
All triggers can be enabled for a specific table by using the following
command
SQL> ALTER TABLE table_name ENABLE ALL TRIGGERS;
SQL> DROP TRIGGER trigger_name
Triggers
• Trigger: stored program that is executed by the DBMS
whenever a specified event occurs
• To monitor a database
and take a corrective
action when a
SQL
condition occurs
– Examples:
Triggers
• Charge $10
overdraft fee if the
balance of an
account after a
withdrawal
transaction is less
than $500
• Limit the salary
increase of an
employee to no
more than 5% raise
SQL
Triggers • To list
all the triggers
you have
created:
mysql>
show triggers;
5 Return Triggers can not return values Stored procedures can return
values
FAQ
• What is procedures ? Explain with the help of examples.
• What is formal and actual parameter?
• Explain Row trigger with the help of example.
• How we can enable, disable and drop trigger?
• What do you understand by packages and write its advantages.
References
• https://docs.oracle.com/database/121/LNPLS/packages.htm
• https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/p
ackages.htm
• https://www.guru99.com/packages-pl-sql.html
• https://docs.oracle.com/cd/B19306_01/server.102/b14200/st
atements_6009.htm
• http://searchoracle.techtarget.com/definition/stored-procedur
e
• https://www.w3resource.com/sql/sql-procedure.php
• https://docs.oracle.com/cd/B19306_01/server.102/b14220/tri
ggers.htm
• https://docs.oracle.com/cd/A57673_01/DOC/server/doc/SCN7
3/ch15.htm
• https://docs.microsoft.com/en-us/sql/t-sql/statements/create-
trigger-transact-sql
University Institute of Engineering (UIE)