C20.0046: Database Management Systems Lecture #21: M.P. Johnson Stern School of Business, NYU Spring, 2008
C20.0046: Database Management Systems Lecture #21: M.P. Johnson Stern School of Business, NYU Spring, 2008
0046: Database
Management Systems
Lecture #21
M.P. Johnson
Stern School of Business, NYU
Spring, 2008
DECLARE
DECLARE
l_book_count
l_book_countINTEGER;
INTEGER;
BEGIN
BEGIN
SELECT
SELECTCOUNT(*)
COUNT(*)
INTO
INTOl_book_count
l_book_count
FROM
FROMbooksbooks
WHERE
WHEREauthor
authorLIKE
LIKE'%FEUERSTEIN,
'%FEUERSTEIN,STEVEN%';STEVEN%';
DBMS_OUTPUT.PUT_LINE
DBMS_OUTPUT.PUT_LINE((
'Steven
'Stevenhas
haswritten
written(or
(orco-written)
co-written)' '||||l_book_count
l_book_count||||
' 'books.');
books.');
END;
END;
Here’s an e.g.:
CREATE
CREATEOR ORREPLACE
REPLACEprocedure
procedure
dropproc(procname
dropproc(procnameininvarchar2)
varchar2) as
as
begin
begin
execute
executeimmediate
immediate'drop
'dropprocedure
procedure' '||||procname;
procname;
end;
end;
//
Directory of examples:
http://pages.stern.nyu.edu/~mjohnson/dbms/plsql/
M.P. Johnson, DBMS, Stern/NYU, Spring 2008 6
SPs in MySQL (5.0)
Generally similar, though technical diffs
Need to temporarily redefine ; delimiter
mysql>
mysql> drop
dropprocedure
procedureififexists
existshello;
hello;
myslq>
myslq> delimiter
delimiter//
mysql>
mysql> create
createprocedure
procedurehello()
hello()
->
->begin
begin
->
->select
select'hi';
'hi';
->
->end;
end;
->
->//
mysql>
mysql> delimiter
delimiter;;
mysql>
mysql> call
call hello();
hello();
Data updates:
Delete: maybe delete related rows
Inserts
Updates: maybe make other rows consistent
Delete: maybe prevent
DDL statements
Log creation of all objects, e.g.
Three aspects:
An event (e.g., update to an attribute)
A condition (e.g., a test of that update value)
An action (the trigger’s effect) (deletion, update, insertion)
When the event occurs, DBMS checks the
constraint, and if it is satisfied, performs the action
It may be triggered by
INSERTs
UPDATEs
DELETEs
1. statement_vs_row.sql
INSERT INTO to_table SELECT * FROM from_table;
2. before_vs_after.sql
INSERT INTO to_table SELECT * FROM from_table;
3. one_trigger_per_type.sql
INSERT INTO to_table VALUES (1);
UPDATE to_table SET col1 = 10;
DELETE to_table;
CREATE
CREATEOR ORREPLACE
REPLACETRIGGER
TRIGGER
valid_when_clause
valid_when_clause
BEFORE
BEFOREINSERT
INSERT ON
ONframe
frame
FOR
FOREACH
EACHROW
ROW
WHEN
WHEN((TO_CHAR(SYSDATE,'HH24')
TO_CHAR(SYSDATE,'HH24')
BETWEEN
BETWEEN99ANDAND1717 ))
...
...
DELETE
DELETEgrades
grades
WHERE
WHEREstudent_id
student_id==22
AND
ANDclass_id
class_id== 101;
101;
UPDATE
UPDATEgrades
gradesSET
SET student_id
student_id== 11
WHERE
WHEREstudent_id
student_id==22AND
ANDclass_id
class_id==101;
101;
UPDATE
UPDATEgrades
gradesSET
SET student_id
student_id== 22
WHERE
WHEREstudent_id
student_id==33AND
ANDclass_id
class_id==101;
101;
uninformed_town_crier.sql
informed_town_crier.sql
No BEFORE STARTUP/LOGON/SERVERERROR
No AFTER SHUTDOWN/LOGOFF
drop
drop table
table junk;
junk;
Examples: rollback;
rollback;
truncate
truncate table
table junk;
junk;
rollback;
rollback;
--changes
emp example: --changes
SAVEPOINT
SAVEPOINT sp1;
sp1;
--changes
Can skip savepoints --changes
SAVEPOINT
SAVEPOINT sp2;
sp2;
But can ROLLBACK --changes
--changes
only backwards SAVEPOINT
SAVEPOINT sp3
sp3
Can ROLLBACK --changes
--changes
only to last COMMIT ROLLBACK
ROLLBACK TO
TO SAVEPOINT
SAVEPOINT sp2;
sp2;
ROLLBACK
ROLLBACK TO
TO SAVEPOINT
SAVEPOINT sp1;
sp1;
How to recover?