02-View, Stored Procedure, Function, and Trigger
02-View, Stored Procedure, Function, and Trigger
Lê Hồng Hải
UET-VNUH
Today’s Overview
1 • View
2 • Stored Procedure,
Function
3 • Trigger
2
View Definition
3
Advantages of Views
4
View in MySQL & PostgreSQL
5
Create a View
6
UsingView
7
Today’s Overview
1 • View
2 • Stored Procedure,
Function
3 • Trigger
8
Stored procedure
9
Stored Procedure Example
10
Stored Procedure Example
CREATE FUNCTION inventory_in_stock(p_inventory_id INT) RETURNS BOOLEAN
READS SQL DATA
BEGIN
DECLARE v_rentals INT;
DECLARE v_out INT;
IF v_rentals = 0 THEN
RETURN TRUE;
END IF;
12
Stored Procedure
13
Stored Procedures Advantages
14
Stored Procedures Disadvantages
Lack of Portability
SQLServer uses T-SQL
15
SP Syntax in MySQL
BEGIN
◼ DECLARE variables;
◼ DECLARE cursors;
◼ DECLARE conditions;
◼ DECLARE handler;
◼ other SQL commands;
END;
16
IF THENclause
IF condition THEN
commands;
[ELSE IF condition THEN
commands;]
[ELSE commands;]
END IF;
17
CASEclause
CASE expression
WHEN value1 THEN commands;
[WHEN value2 THEN commands;]
[ELSE commands;]
END CASE;
18
REPEAT UNTILclause
[loopname:]
REPEAT commands;
UNTIL condition
END REPEAT [loopname];
19
WHILEclause
[loopname:]
WHILE condition DO commands;
END WHILE [loopname];
20
Cursor
21
Cursor Syntax
22
Today’s Overview
1 • View
2 • Stored Procedure,
Function
3 • Trigger
23
Trigger
24
Trigger Example
25
Trigger
26
Advantages of Triggers
27
Disadvantages of Triggers
28
THANKS YOU