MIS 301- Database

download report

Transcript MIS 301- Database

MIS 385/MBA 664
Systems Implementation with DBMS/
Database Management
Dave Salisbury
[email protected] (email)
http://www.davesalisbury.com/ (web site)
Routines and Triggers

Routines




Triggers


Program modules that execute on demand
Functions – routines that return values and take
input parameters
Procedures – routines that do not return values
and can take input or output parameters
Routines that execute in response to a database
event (INSERT, UPDATE, or DELETE)
PL/SQL

SQL programming language
Oracle Triggers



Triggers are procedures associated with a
table
Fire upon meeting of trigger condition
Used in Oracle Server to react to specific
situations



enforcing data integrity rules
deriving specific values
Simple example of a trigger might be the
autonum datatype in Access
One Sample Trigger for Class

This sample will store the following
fields in a table (customer history) on
the occurrence of the event (deletion of
a customer (NOTE: need to create the
tables first).



Customer Last Name
First Name
Customer ID
The Code for Customer History
CREATE OR REPLACE TRIGGER DeleteCustomer
BEFORE DELETE ON Customer
FOR EACH ROW
BEGIN
INSERT INTO CustomerHistory
VALUES(:Old.CUSTOMERID, :Old.LastName,
:Old.FirstName);
END DeleteCustomer;
Another Class Sample Trigger
(To display system time)
--PL/SQL program to display the current date
--First set output on
SET SERVEROUTPUT ON;
--Next do the procedure
DECLARE
TodaysDate DATE;
BEGIN
TodaysDate := SYSDATE;
DBMS_OUTPUT.PUT_LINE( 'Todays date is ');
DBMS_OUTPUT.PUT_LINE( TodaysDate);
END;
/
The one from the Book
First define the ORDERID_SEQUENCE
CREATE SEQUENCE
ORDERID_SEQUENCE
START WITH 1012
NOMAXVALUE
NOCACHE;
Now do the Trigger
CREATE TRIGGER ORDER_ID_BIR
BEFORE INSERT ON ORDER_T
FOR EACH ROW
BEGIN
SELECT ORDERID_SEQUENCE.NEXTVAL
INTO :NEW.ORDER_ID
FROM DUAL;
END ORDER_ID_BIR;