0% found this document useful (0 votes)
12 views83 pages

Trigger

The document discusses triggers in active database systems, highlighting their role in automatically executing actions based on specific database events. It explains the structure, syntax, and execution process of triggers in Oracle, including differences with DB2 triggers and guidelines for writing them. Additionally, it covers concepts such as execution modes, granularity, and the handling of mutating tables.

Uploaded by

armanalis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views83 pages

Trigger

The document discusses triggers in active database systems, highlighting their role in automatically executing actions based on specific database events. It explains the structure, syntax, and execution process of triggers in Oracle, including differences with DB2 triggers and guidelines for writing them. Additionally, it covers concepts such as execution modes, granularity, and the handling of mutating tables.

Uploaded by

armanalis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Triggers

Triggers
➢Active Database Systems
➢Oracle Triggers
➢Differences between Oracle and DB2 Triggers
➢Guidelides in writing triggers in Oracle
➢Trigger Design

1
Active Database Systems
Triggers

2
Active Database Systems
• Traditional DBMS systems are passive
• Queries and updates are explicitly requested by users
• The knowledge of processes operating on data is typically embedded into
applications
• Active database systems
• Reactivity is a service provided by a normal DBMS
• Specific database events are monitored and trigger actions in response

3
Active Database Systems
• Reactivity is ensured by automatically executing active or ECA rules,
that are rules expressed in the form:
• Event
• Database modification operation
• Condition
• Predicate on the database state
• If the condition is true, the action is executed
• Action
• Sequence of SQL instructions or application procedure

4
Rule engine
• Component of the DBMS, in charge of
• tracking events
• executing rules when appropriate, based on the execution strategy of the
DBMS
• Rule execution is interleaved with traditional transaction execution

5
Example: automatic product reorder
• The active rule manages reorder in an inventory stock
• when the quantity on stock of a product goes below a given threshold
• a new order for the product should be issued

• Event
• Update of the quantity on stock for product x
• Insertion of a new product x
• Condition
• The quantity on stock of product x is below a given threshold
and there are no pending orders for product x
• Action
• Issue a new order of a pre-determined quantity for product x
6
Applications of active rules
• Internal applications
• maintenance of complex integrity constraints
• replication management
• materialized view maintenance
• Business Rules
• Incorporate into the DBMS application knowledge
• E.g., reorder rule
• Alerts
• widely used to issue notifications

7
Integrity constraints – example
• Example: A company that provides IT consulting wants to store the
work done by its employees for each project.
• Triggers could be used to enforce that two periods (StartDate, EndDate) by
the same employee, on the same project, do not overlap

8
Triggers
• Commercial products implement active rules by means of triggers
• SQL provides instructions for defining triggers
• Triggers are defined by means of the DDL instruction CREATE TRIGGER
• Trigger syntax and semantics are covered in the SQL3 standard
• some commercial products implement different features with respect to the
standard

9
Trigger structure
• Event
• Insert, delete, update of a table
• Each trigger can only monitor events on a single table
• Condition
• SQL predicate (it is optional)
• Action
• Sequence of SQL instructions
• Proprietary programming language
• e.g. Oracle PL/SQL
• Java code

10
Execution process
When the events take place [triggering]
If the condition is true [evaluation]
Then the action is executed [execution]

• Seems very simple but…


• Execution modes
• Execution granularity

11
Execution mode
• Immediate
• The trigger is executed immediately before or after the triggering statement
• Deferred
• The trigger is executed immediately before commit
• Only the immediate option is available in commercial systems

12
Execution granularity
• Tuple (or row level)
• One separate execution of the trigger for each tuple affected by the triggering
statement
• Statement
• One single trigger execution for all tuples affected by the triggering statement

13
Granularity: example
• Table T A B
1 5
2 9
8 20

• Update event on table T UPDATE T


SET A=A+1
WHERE B<10;

• A trigger defined on table T is executed once or more times depending on


the granularity
• A row level trigger executes twice
• A statement level trigger executes once

14
Triggers in Oracle
Triggers

15
Trigger syntax
CREATE TRIGGER TriggerName
Mode Event {OR Event }
ON TargetTable
[[ REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block

16
Trigger syntax
CREATE TRIGGER TriggerName
Mode Event {OR Event }
ON TargetTable
[[ REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block
• Mode is BEFORE or AFTER
• Also INSTEAD OF but it should be avoided

17
Trigger syntax
CREATE TRIGGER TriggerName
Mode Event {OR Event }
ON TargetTable
[[ REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block
• Event ON TargetTable is
• INSERT
• DELETE
• UPDATE [OF ColumnName]
18
Trigger syntax
CREATE TRIGGER TriggerName
Mode Event {OR Event }
ON TargetTable
[[ REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block
• FOR EACH ROW specifies row level execution semantics
• If omitted, the execution semantics is statement level

19
Trigger syntax
CREATE TRIGGER TriggerName
Mode Event {OR Event }
ON TargetTable
[[ REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block
• To rename the state variables
• REFERENCING OLD AS OldVariableName
• similarly for NEW

20
Trigger syntax
CREATE TRIGGER TriggerName
Mode Event {OR Event }
ON TargetTable
[[ REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block
• Only for row level execution semantics (i.e., FOR EACH ROW)
• A condition may be optionally specified
• The old and new state variables may be accessed

21
Trigger syntax
CREATE TRIGGER TriggerName
Mode Event {OR Event }
ON TargetTable
[[ REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block
• The action is
• a sequence of SQL instructions
• a PL/SQL block
• No transactional and DDL instructions
22
Trigger semantics
• Execution modes
• immediate before
• immediate after
• Granularity is
• row (tuple)
• statement
• Execution is triggered by insert, delete, or update statements in a
transaction

23
Execution algorithm
1. Before statement triggers are executed
2. For each tuple in TargetTable affected by the triggering statement
a) Before row triggers are executed
b) The triggering statement is executed
+ integrity constraints are checked on tuples
c) After row triggers are executed
3. Integrity constraints on tables are checked
4. After statement triggers are executed

24
Execution algorithm: example
SId SName #Employees City Triggering statement
S1 Smith 20 London
S2 Jones 10 Paris
UPDATE S SET City = ‘Rome’ WHERE
S3 Blake 30 Paris
SId IN (‘S1’, ‘S2’, ‘S3’)
S4 Clark 20 London
S5 Adams 30 Athens

Before statement triggers are executed

25
Execution algorithm: example
SId SName #Employees City Triggering statement
S1 Smith 20 London
S2 Jones 10 Paris
UPDATE S SET City = ‘Rome’ WHERE
S3 Blake 30 Paris
SId IN (‘S1’, ‘S2’, ‘S3’)
S4 Clark 20 London
S5 Adams 30 Athens

Before statement triggers are executed

SId SName #Employees City


S1 Smith 20 London
S2 Jones 10 Paris For each row
S3 Blake 30 Paris - Execute before row trigger
S4 Clark 20 London
S5 Adams 30 Athens
26
Execution algorithm: example
SId SName #Employees City Triggering statement
S1 Smith 20 London
S2 Jones 10 Paris
UPDATE S SET City = ‘Rome’ WHERE
S3 Blake 30 Paris
SId IN (‘S1’, ‘S2’, ‘S3’)
S4 Clark 20 London
S5 Adams 30 Athens

Before statement triggers are executed

SId SName #Employees City


S1 Smith 20 Rome
S2 Jones 10 Paris For each row
S3 Blake 30 Paris - Execute before row trigger
S4 Clark 20 London - Update row
S5 Adams 30 Athens
27
Execution algorithm: example
SId SName #Employees City Triggering statement
S1 Smith 20 London
S2 Jones 10 Paris
UPDATE S SET City = ‘Rome’ WHERE
S3 Blake 30 Paris
SId IN (‘S1’, ‘S2’, ‘S3’)
S4 Clark 20 London
S5 Adams 30 Athens

Before statement triggers are executed

SId SName #Employees City


S1 Smith 20 Rome
S2 Jones 10 Paris For each row
S3 Blake 30 Paris - Execute before row trigger
S4 Clark 20 London - Update row
S5 Adams 30 Athens
- Execure after row trigger
28
Execution algorithm: example
SId SName #Employees City Triggering statement
S1 Smith 20 London
S2 Jones 10 Paris
UPDATE S SET City = ‘Rome’ WHERE
S3 Blake 30 Paris
SId IN (‘S1’, ‘S2’, ‘S3’)
S4 Clark 20 London
S5 Adams 30 Athens

Before statement triggers are executed

SId SName #Employees City


S1 Smith 20 Rome
S2 Jones 10 Rome For each row
S3 Blake 30 Paris - Execute before row trigger
S4 Clark 20 London - Update row
S5 Adams 30 Athens
- Execure after row trigger
29
Execution algorithm: example
SId SName #Employees City Triggering statement
S1 Smith 20 London
S2 Jones 10 Paris
UPDATE S SET City = ‘Rome’ WHERE
S3 Blake 30 Paris
SId IN (‘S1’, ‘S2’, ‘S3’)
S4 Clark 20 London
S5 Adams 30 Athens

Before statement triggers are executed

SId SName #Employees City


S1 Smith 20 Rome
S2 Jones 10 Rome For each row
S3 Blake 30 Rome - Execute before row trigger
S4 Clark 20 London - Update row
S5 Adams 30 Athens
- Execute after row trigger
30
Execution algorithm: example
SId SName #Employees City Triggering statement
S1 Smith 20 London
S2 Jones 10 Paris
UPDATE S SET City = ‘Rome’ WHERE
S3 Blake 30 Paris
SId IN (‘S1’, ‘S2’, ‘S3’)
S4 Clark 20 London
S5 Adams 30 Athens

Before statement triggers are executed

SId SName #Employees City


S1 Smith 20 Rome
S2 Jones 10 Rome For each row, …..
S3 Blake 30 Rome
S4 Clark 20 London
S5 Adams 30 Athens
After statement triggers are executed
31
Trigger semantics
• The execution order for triggers with the same event, mode and
granularity is not specified
• it is a source of nondeterminism
• When an error occurs
• rollback of all operations performed by the triggers
• rollback of the triggering statement in the triggering transaction

32
Non termination
• Trigger execution may activate other triggers
• Cascaded trigger activation may lead to non termination of trigger execution
• A maximum length for the cascading trigger execution may be set
• default = 32 triggers
• If the maximum is exceeded
• an execution error is returned

33
Mutating tables
• A mutating table is the target table modified by the statement (that is,
the event) triggering the trigger
• The mutating table
• cannot be accessed in row level triggers
• may only be accessed in statement triggers
• Limited access on mutating tables only characterizes Oracle applications
• accessing mutating tables is always allowed in SQL3

34
Accessing mutating values
• A trigger executing at row level can access the data in the row that
it is processing by using variables NEW and OLD
• can be renamed if using the REFERENCING CLAUSE
• not available when executing at statement level

Triggering
Statement OLD.field Value NEW.field Value
INSERT NULL Post-insert value
UPDATE Pre-update value Post-update value
DELETE Pre-delete value NULL

35
Accessing current values: example (I)
• INSERT INTO S VALUES (‘S6’, ‘Abbott’,
30, ‘London’)
• NEW.SId: ‘S6’, NEW.SName: ‘Abbott’, …
• OLD: NULL SId SName #Employees City
S1 Smith 20 London
• DELETE FROM S WHERE SId = ‘S2’
S2 Jones 10 Paris
• NEW: NULL S3 Blake 30 Paris
• OLD.SId = ‘S2’, OLD.SName: ‘Jones’, … S4 Clark 20 London
• UPDATE S SET City = “Rome” WHERE S5 Adams 30 Athens
SId = ‘S3’
• NEW.SId: ‘S3’, NEW.City: ‘Rome’
• OLD.SId: ‘S3’, OLD.City: ‘Paris’

36
Accessing current values: example (II)
• UPDATE S SET City = ‘Rome’ WHERE
SId IN (‘S1’, ‘S2’, ‘S3’)
• Trigger executed three times (FOR
EACH ROW) SId SName #Employees City
S1 Smith 20 London
• First execution
S2 Jones 10 Paris
• NEW.SId: ‘S1’, NEW.City: ‘Rome’
S3 Blake 30 Paris
• OLD.SId: ‘S1’, OLD.City: ‘London’
S4 Clark 20 London
• First execution
S5 Adams 30 Athens
• NEW.SId: ‘S2’, NEW.City: ‘Rome’
• OLD.SId: ‘S2’, OLD.City: ‘Paris’
• First execution
• NEW.SId: ‘S3’, NEW.City: ‘Rome’
• OLD.SId: ‘S3’, OLD.City: ‘Paris’

37
Example
• Trigger to manage reorder in an inventory stock
• when the quantity on stock of a product goes below a given threshold
• a new order for the product should be issued
• The following database schema is given
Inventory (Part#, QtyOnStock, ThresholdQty, ReorderQty)
PendingOrders(Part#, OrderDate, OrderedQty)

38
Example
• Trigger to manage reorder in an inventory stock
• when the quantity in stock of a product goes below a given threshold
• a new order for the product should be issued
• Event
• Update of the quantity in stock for product x
• Insert of a new product x
• Trigger semantics
• After the modification event
• Separate execution for each row of the Inventory table

39
Trigger example
CREATE TRIGGER Reorder
AFTER UPDATE OF QtyOnStock OR INSERT ON Inventory
FOR EACH ROW

40
Example
• Event
• Update of the quantity in stock for product x
• Insert of a new product x
• Condition
• The quantity on stock of product x is below a given threshold
• There are no pending orders for product x
• Action
• Issue a new order of a pre-determined quantity for product x

41
Trigger example
CREATE TRIGGER Reorder
AFTER UPDATE OF QtyOnStock OR INSERT ON Inventory
FOR EACH ROW
WHEN (NEW.QtyOnStock < NEW.ThresholdQty)
The quantity on stock of product x is below a given
threshold

42
Example
• Trigger to manage reorder in an inventory stock
• when the stocked quantity of a product goes below a given threshold
• a new order for the product should be issued
• Condition
• The quantity on stock of product x is below a given threshold
• There are no pending orders for product x
• This part cannot be introduced into the WHEN clause
• Action
• Issue an order with given reorder quantity for product x

43
Example: Trigger body
DECLARE
N number;
BEGIN
select count(*) into N
from PendingOrders
where Part# = :NEW.Part#;
If (N=0) then
insert into PendingOrders(Part#,OrderedQty,OrderDate)
values (:NEW.Part#, :NEW.ReorderQty, SYSDATE);
end if;
END; 44
Complete trigger example
CREATE TRIGGER Reorder
AFTER UPDATE OF QtyOnStock OR INSERT ON Inventory
FOR EACH ROW
WHEN (NEW. QtyOnStock < NEW. ThresholdQty)
DECLARE
N number;
BEGIN
select count(*) into N
from PendingOrders
where Part# = :NEW.Part#;
If (N==0) then
insert into PendingOrders(Part#,OrderedQty,OrderDate)
values (:NEW.Part#, :NEW.ReorderQty, SYSDATE);
end if;
45
END;
Guidelines in writing triggers in
Oracle

46
Guidelines in writing triggers in Oracle
• Execution Mode INSTEAD OF is allowed in Oracle but it should be avoided
• Usage of before triggers in Oracle to be compliant with the standard
• Modifications of the NEW variable in tuples affected by the triggering statement are
allowed in before triggers
• Other databases modifications, apart those reported in the previous point, are not
allowed in before triggers
• Before triggers cannot trigger other triggers

47
Concise comparison between
Oracle and DB2 Triggers

48
Differences between Oracle and DB2
Oracle DB2

Reference to Old_Table and New_Table in statement triggers No Yes

When clause in statement triggers No Yes


Execution order between row and statement triggers with same Specified Arbitrary
mode
Execution order between triggers with same event, mode and Unspecified Creation Order
granularity
More than one triggering event allowed Yes No

Forbidden access to the mutating table Yes for row triggers No

Availability of the instead semantics Yes No


Yes Only NEW
Database modifications allowed in before triggers
variables
49
Trigger Design

50
Trigger design
• The design of a single trigger is usually simple
• Identify
• execution semantics
• event
• condition (optional)
• action

51
Trigger design
• Understanding mutual interactions among triggers is more complex
• The action of one trigger may be the event of a different trigger
• Cascaded execution
• If mutual triggering occurs
• Infinite execution is possible

52
Trigger execution properties
• Termination
• For an arbitrary database state and user transaction, trigger execution terminates
in a final state (also after an abort)
• Confluence
• For an arbitrary database state and user transaction, trigger execution terminates
in a unique final state, independently of the execution order of triggers
• Termination is the most important property
• Confluence is enforced by deterministic trigger execution

53
Guaranteeing termination
• Termination is guaranteed at run time by aborting trigger execution after
a given cascading length
• Termination may be verified at design time by means of the triggering
graph
• a node for each trigger
• a directed edge Ti Tj if trigger Ti is performing an action triggering trigger Tj
• A cycle in the graph shows potential non terminating executions

Ti Tj

54
Example
• Trigger managing salary amounts
• When a given average salary value is exceeded, a salary reduction is automatically
enforced
• The following table is given
Employee (Emp#, Ename, …, Salary)
• Execution semantics
• After the modification events
• Separate execution for each update instruction
• No condition for execution

55
Example
CREATE TRIGGER SalaryMonitor
AFTER UPDATE OF Salary ON Employee
FOR EACH STATEMENT
BEGIN
update Employee
set Salary = Salary * K
where 2500 < (select AVG (Salary) from Employee);
END;
The value of K may be:
K = 0.9 execution terminates
SalaryMonitor
K = 1.1 infinite execution
56
Trigger applications
• Internal applications
• maintenance of complex integrity constraints
• replication management
• materialized view maintenance
• Business Rules
• Incorporate into the DBMS application knowledge
• E.g., reorder rule
• Alerts
• widely used for notification

57
Triggers for constraint management
• Triggers are exploited to enforce complex integrity constraints
• Design procedure
1. Write the constraint as a SQL predicate
• It provides a condition for the trigger execution
2. Identify the events which may violate the constraint
• i.e., the condition
3. Define the constraint management technique in the action

58
Design example (1)
• The following tables are given
• Supplier S (S#, SName, …)
• Part P (P#, PName, …)
• Supply SP (S#, P#, Qty)
• Constraint to be enforced
• A part may be supplied by at most 10 different suppliers

59
Design example (1)
• Constraint predicate
select P#
from SP
group by P#
having count(*) > 10

• set of parts violating the constraint


• Events
• insert on SP
• update of P# on SP
• Action
• reject the violating transaction

60
Design example (1)
• Execution semantics
• after the modification
• statement level
• to capture the effect of the entire modification
• (Oracle) to allow access to the mutating table
• (Oracle) No condition
• The condition cannot be specified in the WHEN clause
• It is checked in the trigger body
• Design for Oracle trigger semantics

61
Design example (1)
CREATE TRIGGER TooManySuppliers
AFTER UPDATE OF P# OR INSERT ON SP
DECLARE
N number;
BEGIN
select count(*) into N
from SP
where P# IN (select P# from SP
group by P#
having count(*) > 10);
if (N <> 0) then
raise_application_error (xxx, ‘constraint violated’);
end if;
END; 62
Design example (2)
• The following tables are given
• Supplier S (S#, SName, …)
• Part P (P#, PName, …)
• Supply SP (S#, P#, Qty)
• Constraint to be enforced
• The quantity of a product supply cannot be larger than 1000. If it is larger, trim it
to 1000.
• Check constraints do not allow compensating actions
• Implement with a trigger

63
Design example (2)
• Constraint predicate
• Qty > 1000
• It is also the trigger condition
• Events
• insert on SP
• update of Qty on SP
• Action
• Qty = 1000

64
Design example (2)
• Execution semantics
• before the modification takes place
• its effect can be changed before the constraint is checked
• row level
• each tuple is modified separately

65
Design example (2)
CREATE TRIGGER ExcessiveQty
BEFORE UPDATE OF Qty OR INSERT ON SP
FOR EACH ROW
WHEN (NEW.Qty > 1000)
BEGIN
:NEW.Qty := 1000;
END;

66
Triggers for materialized view maintenance
• Materialized views are queries persistently stored in the database
• provide increased performance
• contain redundant information
• e.g., aggregate computations
• Triggers are exploited to maintain redundant data
• Propagate data modifications on tables to materialized view

67
Design example (3)
• Tables
• Student S (SId, SName, DCId)
• Degree course DC (DCId, DCName)
• Materialized view
• Enrolled students ES (DCId, TotalStudents)
• For each degree course, TotalStudents counts the total number of enrolled students
• Defined by query

SELECT DCId, COUNT(*)


FROM S
GROUP BY DCId;

68
Design example (3)
• Tables
• Student S (SId, SName, DCId)
• Degree course DC (DCId, DCName)
• Materialized view
• Enrolled students ES (DCId, TotalStudents)
• For each degree course, TotalStudents counts the total number of enrolled students
• A new degree course is inserted in materialized view ES when the first student is
enrolled in it
• A degree course is deleted from ES when the last student quits it

69
Design example (3)
• Database schema
S (SId, SName, DCId)
DC (DCId, DCName)
ES (DCId, TotalStudents)
• Propagate modifications on table S to materialized view (table) ES
• Inserting new tuples into S
• Deleting tuples from S
• Updating the DCId attribute in one or more tuples of S

70
Design example (3)
• Design three triggers to manage separately each data modification
• Insert trigger, delete trigger, update trigger
• All triggers share the same execution semantics
• Execution semantics
• after the modification takes place
• Table ES is updated after table S has been modified
• row level
• Separate execution for each tuple of table S
• significantly simpler to implement

71
Insert trigger (3)
• Event
• insert on S
• No condition
• It is always executed
• Action
• if table ES contains the DCId in which the student is enrolled
• increment TotalStudents
• otherwise
• add a new tuple in table ES for the degree course, with TotalStudents set to 1

72
Insert trigger (3)
CREATE TRIGGER InsertNewStudent
AFTER INSERT ON S
FOR EACH ROW
DECLARE
N number;
BEGIN
--- check if table ES contains the tuple for the degree
--- course NEW.DCId in which the student enrolls
select count(*) into N
from ES
where DCId = :NEW. DCId;
73
Insert trigger (3)
if (N <> 0) then
--- the tuple for the NEW.DCId degree course is
--- available in ES
update ES
set TotalStudents = TotalStudents +1
where DCId = :NEW.DCId;
else
--- no tuple for the NEW.DCId degree course is
--- available in ES
insert into ES (DCId, TotalStudents)
values (:NEW.DCId, 1);
end if;
END;
74
Delete trigger (3)
• Event
• delete from S
• No condition
• It is always executed
• Action
• if the student was the only student enrolled in the degree course
• delete the corresponding tuple from ES
• otherwise
• decrement TotalStudents

75
Delete trigger (3)
CREATE TRIGGER DeleteStudent
AFTER DELETE ON S
FOR EACH ROW
DECLARE
N number;
BEGIN
--- read the number of students enrolled on
--- the degree course OLD.DCId
select TotalStudents into N
from ES
where DCId = :OLD.DCId; 76
Delete trigger (3)
if (N > 1) then
--- there are many enrolled students
update ES
set TotalStudents = TotalStudents – 1
where DCId = :OLD.DCId;
else
--- there is a single enrolled student
delete from ES
where DCId = :OLD.DCId;
end if;
END;
77
Update trigger (3)
• Event
• Update of DCId on S
• No condition
• It is always executed
• Action
• update table ES for the degree course where the student was enrolled
• decrement TotalStudents, or delete tuple if last student
• update table ES for the degree course where the student is currently enrolled
• increment TotalStudents, or insert new tuple if first student

78
Update trigger (3)
CREATE TRIGGER UpdateDegreeCourse
AFTER UPDATE OF DCId ON S
FOR EACH ROW
DECLARE
N number;
BEGIN
--- read the number of students enrolled in
--- degree course OLD.DCId
select TotalStudents into N
from ES
where DCId = :OLD.DCId;
79
Update trigger (3)
if (N > 1) then
--- there are many enrolled students
update ES
set TotalStudents = TotalStudents – 1
where DCId = :OLD.DCId;
else
--- there is a single enrolled student
delete from ES
where DCId = :OLD.DCId;
end if;

80
Update trigger (3)
--- check if table ES contains the tuple for the degree
--- course NEW.DCId in which the student is enrolled
select count(*) into N
from ES
where DCId = :NEW. DCId;

81
Update trigger (3)
if (N <> 0) then
--- the tuple for the NEW.DCId degree course is available in ES
update ES
set TotalStudents = TotalStudents +1
where DCId = :NEW.DCId;
else
--- no tuple for the NEW.DCId degree course is available in ES
insert into ES (DCId, TotalStudents)
values (:NEW.DCId, 1);
end if;
END;
82

You might also like