Store Procedure & Triggers: Nguyen Thi Kim Tuyen Huynh Nguyen Hong Nhan

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Store

Procedure &
Triggers
Nguyen Thi Kim Tuyen
Huynh Nguyen Hong Nhan
Agenda
Store Procedure
• Definition
• Parts / Parameters
• Structure
• Case study
Trigger

● Generalized Model for Active Databases


● Oracle Triggers
● Trigger Examples
● Types of Triggers
● Demos
● Design and implementation issues for Active Databases
● Potential Applications for Active Databases
What is Stored Procedure

◈ Store Procedure is a group of SQL statements that uses for processing a


data in RDBMS with a particular objective.

◈ It can be understand a sub-program in DBMS which can support us declare


a variable.
Parts of Stored Procedure
◈ Declarative
🞚 It is an optional part.
🞚 It contains declarations of types, variables, …
◈ Execution
🞚 It is an mandatory part.
🞚 It contains statements that perform the designated action.
◈ Exception-handling
🞚 It is an optional part.
🞚 It contains the code that handles runtime errors.
Parameters of Stored Procedure
◈ IN
🞚 IN parameter lets you pass a value to the program.
🞚 It is a read-only parameter.
🞚 It is the default mode of parameter passing.

◈ OUT
🞚 OUT parameter returns a value to the calling program.
🞚 You can change its value and reference the value after assigning it.

◈ IN OUT
🞚 IN OUT parameter passes an initial value to a subprogram and returns an updated value
to the caller.
Structure of Store Procedure

1. Declarative
2. Execution
3. Exception-handling
Sample
Database Schema

A global fictitious company that sells


computer hardware including storage,
motherboard, RAM, video card, and CPU

Oracle Sample Database (oracletutorial.com)


Case Study 1
Case Study 2

Option 1

Option 2
Generalized Model for Active
Databases
● Active database is a database consisting of set of
active rules / triggers.
● Disadvantages
○ Active DBs are very difficult to be maintained of
the complexity that arises in understanding the effect
of these active rules/ triggers.
● Advantages
○ Enhances traditional DB functionalities with powerful
rule processing capabilities
● Avoids redundancy of checking & repair operations
● Enable a uniform & centralized description of business rules
relevant to the information system.
● Suitable platform for building large & efficient
knowledge base & expert systems
Source: https://www.geeksforgeeks.org/active-databases/
Generalized Model for Active
Databases and Oracle Triggers
● Event-Condition-Action (ECA) model specifies active database rules. A
rule has 3 components:
○ Event: triggers the rule. E.g database update operations, temporal events,
external events.
○ Condition: determines whether the rule action should be executed.
○ Action: to be taken. E.g: SQL scripts, a database transaction, external
program.
● Oracle triggers - active rules is a named PL/SQL block stored in the Oracle
Database and executes automatically when a triggering event takes place.
● The event can be:
○ A DML statement executes. E.g. INSERT, UPDATE, DELETE
○ A DDL statement executes. E.g. CREATE or ALTER
○ A system event such as startup or shutdown of the Oracle Database.
○ A user event such as login or logout.
Oracle Triggers
Trigger Examples (1)
The Total_sal attribute
is a derived attribute =
sum of salaries of all
employees belong to
the particular
department

The events may cause a change in Total_sal:


● Inserting ( one or more) new employee tuples
● Changing the salary of (one or more) existing employees.
● Changing the assignment of existing employees from one department to
another
● Deleting (one or more) employee tuples.
Trigger Examples (2)
Types of Trigger
● ROW Triggers and STATEMENT Triggers
○ can specify the number of times the trigger action is to be run
■ Once for every row affected by the triggering statement.
■ Once for the triggering statement, no matter how many rows it affects.
● BEFORE and AFTER Triggers
○ can specify the trigger timing - whether the trigger action is to be run before or after the triggering
statement. BEFORE and AFTER apply to both statement and row triggers.
○ fired by DML statements can be defined only on tables, not on views. However, triggers on the base
tables of a view are fired if an INSERT, UPDATE, or DELETE statement is issued against the view.
○ fired by DDL statements can be defined only on the database or a schema, not on particular tables.
● INSTEAD OF Triggers
○ provide a transparent way of modifying views that cannot be modified directly through DML
statements
○ You can write normal INSERT/UPDATE/DELETE statements against the view and the INSTEAD OF
trigger is fired to update the underlying tables appropriately. Activated for each row of the view that
get modified.
● Triggers on SYSTEM EVENTS and USER EVENTS
ROW Trigger and STATEMENT Trigger
● A row trigger is fired each time the table is affected by the trigger statement.
○ E.g., if an UPDATE statement updates multiple rows of a table, a row trigger is fired once
for each row affected by the UPDATE statement. If a trigger statement affects no rows, a
row trigger is not run.
○ Row triggers are useful if the code in the trigger action depends on the data provided by
the triggering statement or rows that are affected.
○ FOR EACH ROW specifies that the trigger is a row-level trigger.
● A statement trigger is fired on behalf of the trigger statement, regardless of the
number of rows in the table that the trigger statement affects, even if no rows are
affected.
○ E.g., if a DELETE statement deletes several rows from a table, a statement-level DELETE
trigger is fired only once.
○ Statement triggers are useful if the code in the trigger action does not depend on the data
provided by the triggering statement or the rows affected.
○ E.g.:
■ Make a complex security check on the current time or user
■ Generate a single audit record
BEFORE Trigger and AFTER Trigger
● BEFORE triggers run the trigger action before the trigger statement is run.
○ When the trigger action determines whether the triggering statement should be
allowed to complete. → can eliminate unnecessary processing & rollback.
○ To derive specific column values before completing a triggering
INSERT/UPDATE statement.
● AFTER triggers run the trigger action after the trigger statement is run
● Trigger Type Combinations
○ BEFORE statement trigger
○ BEFORE row trigger
○ AFTER statement trigger
○ AFTER row trigger
DEMO: ROW - AFTER Trigger
Suppose we want to record actions against the Customer table whenever a
customer is updated or deleted.
DEMO: STATEMENT-BEFORE Trigger
Suppose, you want to restrict users to update the credit of customers from the 10th to 15th of every
month so that you can close the financial month.

To enforce this rule, you can use this statement-level trigger


INSTEAD OF TRIGGER
● An INSTEAD OF trigger allows you to update data in tables via their view
which cannot be modified directly through DML statements.
● When you issue a DML statement to a non-updatable view, Oracle will
issue an error.
● If the view has an INSTEAD OF trigger, it will automatically skip the DML
statement and execute other DML statements instead.
● In Oracle, you can create an INSTEAD OF trigger for a view only, cannot
create an INSTEAD OF trigger for a table.
● INSTEAD Trigger is fired for each row of the view that gets modified.
DEMO: INSTEAD OF TRIGGER
DEMO: INSTEAD OF TRIGGER
Triggers on System Events or User Events
You can use triggers to publish information about database events to
subscribers.
● System events
○ Database startup and shutdown
○ Data Guard role transitions
○ Server error message events
● User events
○ User logon and logoff
○ DDL statements (CREATE, ALTER, DROP)
○ DML statements (INSERT, DELETE, UPDATE)
Design and Implementation Issues for
Active Databases
● Issues
○ concern activation, deactivation, grouping of rules. Allow users
■ activate command : ENABLE ALTER TRIGGER trigger_name ENABLE;
■ deactivate -> deactivated rule : DISABLE ALTER TRIGGER trigger_name DISABLE;
■ drop command : DROP DROP TRIGGER IF EXISTS trigger_name;
■ group rules into named rule sets
○ concern whether triggered actions should be executed before, after, instead of, concurrently
○ whether the action being executed should be a separate transaction or a part of the same
transaction that triggered the rule.
○ concern Rule considerations. 3 main possibilities:
■ Immediate consideration
● The condition is evaluated as part of the same transaction as the triggering event & is evaluated
immediately ( before, after, instead of executing the triggering event)
■ Deferred consideration
● The condition is evaluated at the end of the transaction that included the triggering event.
■ Detached consideration
● The condition is evaluated as a separate transaction, spawned from the triggering transaction.
Potential Applications for Active
Databases
● Allow Notification of certain conditions that occur.
○ To monitor the temperature of an industrial furnace. The application can periodically insert in the
database the temperature reading records directly from temperature sensors, and active rules
can be written that are triggered whenever a temperature record is inserted, with a condition that
checks if the temperature exceeds the danger level and results in the action to raise an alarm.
● To enforce integrity constraints by specifying the types of events that may cause the constraints to
be violated and then evaluating appropriate conditions that check whether the constraints are actually
violated by the event or not. Hence, complex application constraints, often known as business rules,
may be enforced that way.
● Automatic maintenance of derived data
○ An application is to use active rules to maintain the consistency of materialized views (see
Section 5.3) whenever the base relations are modified.
○ A related application maintains that replicated tables are consistent by specifying rules that
modify the replicas whenever the master table is modified.
References
1. https://www.geeksforgeeks.org/active-databases/
2. https://www.oracletutorial.com/plsql-tutorial/oracle-trigger/
3. https://www.tutorialspoint.com/explain-about-triggers-and-active-databases-in-dbms
4. https://docs.oracle.com/cd/B19306_01/server.102/b14220/triggers.htm
5. https://foregoing-ornament-a63.notion.site/Database-System-Presentation-Oracle-Stored
-Procedures-and-Trigger-78988bd9042141e49e91b5f52c843401

6. https://www.oracletutorial.com/plsql-tutorial/oracle-disable-triggers/
7. https://www.oracletutorial.com/plsql-tutorial/oracle-statement-level-triggers/
8. https://docs.oracle.com/cd/B19306_01/server.102/b14220/triggers.htm
9. Ramez Elmasri , Shamkant Navathe Fundamentals of Database Systems 7th Edition

You might also like