Sei sulla pagina 1di 4

Views: Definition and Objectives

A relational view is a virtual relation derived


from one or several relations. A view is defined
by a query expression.

CSC3064
Database Engineering

Main advantages are :


allows different, independent external
interpretations of a database schema
provides data and application independence

Lecture 5 Views, Triggers

not necessary to restructure the schema


updates through views generally not supported

View Example

Triggers

CREATE view customerDetails AS SELECT


Deposit.custname,custcity,Deposit.branchName,bran
chcity FROM Deposit,Branch,Customer
WHERE deposit.branchname = branch.branchname
AND deposit.custname = customer.custname;

Triggers are PL/SQL programs


Triggers executes automatically upon a triggering
statement or event
Triggers can not be disables without privilege
Commonly used for auditing and business rule
enforcement among others

Select * from customerDetails;

Trigger Execution

Triggers - Syntax
Oracle Server

Application
code
insert table
delete table
update table

CREATE [OR R REPLACE] TRIGGER trigger-name


{ BEFORE | AFTER | INSTEAD OF }
{ INSERT | DELETE | UPDATE [ of column-list] }
ON [ TABLE | DATABASE ]
[WHEN (condition) ]
[declare]
<local variable declaration>
begin
<code>
[exception]
[exception handlers]
End;
}

1. Before triggers
2. Constraints

3. Table updates

4. After triggers

Triggers Before

Events causing Triggers

BEFORE triggers

An INSERT, UPDATE, or DELETE statement on a


specific table (or view, in some cases)
A CREATE, ALTER, or DROP statement on any
schema object
A database startup or instance shutdown

Are executed before the triggering events


Cannot activate other triggers
New values can be modified before
entering tables

A specific error message or any error message


A user logon or logoff

Triggers After

Triggers simple
E.g. Maintain the number of deposit accounts for all

AFTER triggers

branches (using a table TotalCustomers):

Are executed after the triggering events

CREATE TABLE TotalCustomers (noCustomers

INTEGER, dateRecorded DATE PRIMARY KEY);

Can activate other triggers


New values cannot be modified before entering tables

CREATE OR REPLACE TRIGGER keepTotalCustomers

AFTER INSERT ON Deposit


BEGIN
INSERT INTO TotalCustomers
(SELECT COUNT(customerName), SYSDATE()
FROM Deposit);
END;
/
9

10

Triggers - new and old

Triggers - new and old


What does new:Name and old:Name mean??

CREATE TRIGGER addCustomer


AFTER INSERT ON Deposit
FOR EACH ROW
BEGIN
UPDATE Branch SET noCust = noCust + 1
WHERE Branch.branchName = :new.branchName;
END;

new relates to the new values of the table the


trigger is created on.

old relates to the previous values in the table


BEFORE can write to :new value but not :old
value
AFTER cannot write to :new or :old values
BEFORE, AFTER can read both :new and :old
values
11

12

Triggers- When

Triggers When
A withdrawal on a deposit account cannot exceed 100

WHEN restricts when a trigger occurs


E.g. Delete deposits from non-empty deleted branches

CREATE OR REPLACE TRIGGER bigWithdrawal


BEFORE UPDATE OF balance ON Deposit
FOR EACH ROW
WHEN (old.balance - new.balance > 100)
BEGIN
:new.balance := :old.balance;
END;
/

CREATE TRIGGER deleteBranchDeposits


BEFORE DELETE on Branch
FOR EACH ROW
WHEN (new.noCust > 0)
BEGIN
DELETE FROM Deposit
WHERE :new.branchName = Deposit.branchName;
END;
/
13

14

Potrebbero piacerti anche