Sunteți pe pagina 1din 23

Advanced Structured Query

Language (SQL) – Part 1


Lecture 8

Carlos Coronel, Steven Morris, Peter Rob, Database Systems: Design,


Implementation, and Management, 9th Edition, Cengage Learning
Learning Outcomes
 In this chapter, students will learn:
 What is procedural SQL
 Why procedural SQL is important
 How to create and use triggers

2
Procedural SQL
 Shortcomings of SQL
 SQL doesn’t support execution of a stored set of procedures
based on some logical condition
 IF <condition>
THEN <perform procedure>
ELSE <perform alternate procedure>

 SQL fails to support the looping operations


 DO WHILE
<perform procedure>
END DO
3
Procedural SQL
 Solutions:
 SQL statements can be inserted within the procedural
programming language
 Procedural SQL (PL/SQL)
 Triggers

 Stored Procedures
 PL/SQL Functions (in DB2, it is known as user-
defined function)

 Embedded SQL

4
Procedural SQL
 Procedural SQL (PL/SQL):
 A language to store procedural code and SQL statements in
database
 Merge SQL and traditional programming constructs (e.g., IF-
THEN-ELSE, FOR and WHILE loops, etc.)
 PL/SQL is executed by DBMS when invoked by the end user

5
Triggers
 A procedural SQL code that is automatically invoked
by RDBMS on data manipulation event
 Example of use:
 Automatically update the vendor when product inventory
drops below its minimum quantity on hand

 Trigger is invoked before/after a data row is


 Inserted
 Updated
 Deleted

6
Trigger - Insert Trigger

A C+1
B D+1

Table A Table B
After data is inserted to Table A then
do something to Table B

7
Triggers
 TWO types:
 Row-level
 Executed once for each row in a transaction
 E.g., if you update 10 rows, the trigger executes 10 times, once
for each row
 Statement-level
 Executed once for each transaction
 E.g., if you update 10 rows, the trigger will only be executed
once, regardless of how many rows it modifies

8
Trigger
 Role of triggers
 To enforce constraints that cannot be enforced at the design
and implementation levels.
 Add functionality:
 automatie critical actions
 providing appropriate warnings and suggestions
 Can be used to update table values, insert records in tables,
and call other stored procedures

 Drawback
 Add processing power

9
Trigger Syntax

1 2
CREATE TRIGGER <triggername> BEFORE/AFTER
3 4
DELETE/INSERT/UPDATE columnname ON <tablename>
5 FOR EACH ROW/STATEMENT mode db2sql
6 [REFERENCING OLD/NEW AS <variable/table name>]
UPDATE <tablename>
7
SET <conditions>

10
Trigger Syntax
When creating a trigger, it requires:
1. Trigger name - the name of trigger
2. Activation - trigger fires before/after data modification
3. Triggering event - SQL statement (insert, update, delete)
that causes the trigger to fire
4. Triggering table - the table for which the trigger exists
5. Row-level/statement-level - whether trigger fires for each
row or for each statement
6. Transition table/variable - the names to be used to
reference the information prior to and after data modification
7. Trigger condition - SQL code that runs when trigger is fired

11
The PRODUCT Table

PCode Name QOH MinQty Reorder


P100 Pencil 8 5 0
P101 Ruler 18 15 0
P102 Pen 23 10 0
P103 Eraser 11 5 0

12
Trigger Example (Row-Level)
 Example:
 CREATE TRIGGER trgProduct
AFTER INSERT ON Product
FOR EACH ROW mode db2sql
UPDATE Product
SET Reorder = 1
WHERE QOH <= MinQty

 To execute:
INSERT INTO PRODUCT
VALUES ('P104', 'Hammer', 8, 10, 0)

13
Trigger Example (Row-Level)
 Example:
 CREATE TRIGGER trgProduct
AFTER UPDATE OF QOH ON Product
FOR EACH ROW mode db2sql
UPDATE Product
SET Reorder = 1
WHERE QOH <= MinQty

 To execute:
UPDATE PRODUCT SET QOH = 4
WHERE P_CODE = 'P100'
14
Trigger Example (Row-Level)
CREATE TRIGGER trgProduct
AFTER INSERT or UPDATE OF QOH ON Product
FOR EACH ROW mode db2sql
BEGIN
IF INSERTING THEN
UPDATE Product SET Reorder = 1
WHERE QOH <= MinQty;
ELSEIF UPDATING THEN
UPDATE Product SET Reorder = 2
WHERE QOH <= MinQty;
END IF;
END@
15
Transition Table in Trigger
Transition table is used to reference the information prior to
and after data modification
1. OLD transition variables
 contain the values of columns before triggering SQL statement
updates them
 useful if you need to access the prior value of a column before a
triggering update/delete statement
2. NEW transition variables
 contain the values of columns after the triggering SQL statement
updates them
 can define NEW transition variables for update/insert triggers

16
Transition Table in Trigger - Example
 Example:
CREATE TRIGGER trgOrder
Using NEW
AFTER INSERT ON OrderItem transition variable
REFERENCING NEW AS N
FOR EACH ROW mode db2sql
UPDATE Order
SET Total = (SELECT sum (quantity * price)
FROM OrderItem
WHERE OrderID = N.OrderID)
WHERE OrderID = N.OrderID;

17
Transition Table in Trigger - Example
 Example:
CREATE TRIGGER trgOrder2
Using OLD
AFTER DELETE ON OrderItem transition variable
REFERENCING OLD AS O
FOR EACH ROW mode db2sql
UPDATE Order
SET Status = 'Inactive'
WHERE Order.OrderID = O.OrderID;

 To execute:
DELETE FROM OrderItem WHERE OrderID = ‘W22'
18
Permitted Trigger Transition Variables
Activation Triggering Row/Statement Transition Transition Tables
SQL -Level Variables
BEFORE INSERT ROW NEW N/A
UPDATE ROW OLD,NEW N/A
DELETE ROW OLD N/A
AFTER INSERT ROW NEW NEW_TABLE
STATEMENT N/A NEW_TABLE
UPDATE ROW OLD,NEW OLD_TABLE,
NEW_TABLE
STATEMENT N/A OLD_TABLE,
NEW_TABLE
DELETE ROW OLD OLD_TABLE,
STATEMENT N/A OLD_TABLE,
NEW_TABLE

19
Trigger Example (Statement-level)
CREATE TRIGGER trgDropStudent
AFTER DELETE ON Student
REFERENCING OLD TABLE AS oldRecord
FOR EACH STATEMENT mode db2sql
INSERT INTO RemovedStudentTable
SELECT * FROM oldRecord
Using OLD
transition table

20
Trigger Example (Statement-level)
 To execute:
DELETE FROM STUDENT
WHERE STUDENTID = 12345

Student
StudentID Name CGPA
12345 Ali 1.98 
12346 John 2.22

RemovedStudentTable
StudentID Name CGPA
12345 Ali 1.98

21
Trigger Example (Statement-level)
CREATE TRIGGER trgAddNew
AFTER INSERT ON Student
REFERENCING NEW TABLE AS newStudent
FOR EACH STATEMENT
INSERT INTO Enrollment
(SELECT StudentID FROM newStudent)
Using NEW
transition table

22
Trigger Example (Statement-level)
 To execute:
 INSERT INTO STUDENT VALUES (12347, 'James', 2.3)

newStudent
StudentID Name CGPA
12345 Ali 1.98
12346 John 2.22
12347 James 2.3 
Enrollment
StudentID
12347

23

S-ar putea să vă placă și