Fundamentals of Database Systems: (SQL - V)
Fundamentals of Database Systems: (SQL - V)
Malay Bhattacharyya
Assistant Professor
2 Cursors
3 Problems
Outline Advanced Integrity Preservation Cursors Problems
Triggers in MySQL
One can define at most six triggers for each table. These are
activated in coordination with the following events.
BEFORE INSERT - before data is inserted into the table.
AFTER INSERT - after data is inserted into the table.
BEFORE UPDATE - before data in the table is updated.
AFTER UPDATE - after data in the table is updated.
BEFORE DELETE - before data is removed from the table.
AFTER DELETE - after data is removed from the table.
Note: For MySQL version 5.7.2+, one can define multiple triggers
for the same trigger event and action time.
Outline Advanced Integrity Preservation Cursors Problems
Triggers in MySQL
To display all the triggers in the database, the following SQL query
is used:
show triggers;
Table: FACULTY
ID NAME EMAIL AGE
1 Debasis Sengupta [email protected] 60
2 Debapriyo Majumdar [email protected] 40
3 Malay Bhattacharyya [email protected] 37
Outline Advanced Integrity Preservation Cursors Problems
Table: FACULTY
ID NAME EMAIL AGE
1 Debasis Sengupta [email protected] 60
2 Debapriyo Majumdar [email protected] 40
3 Malay Bhattacharyya [email protected] 37
This will make the following entry to the FACULTY LOG table:
if <condition> then
<statement>;
else
<statement>;
end if;
Outline Advanced Integrity Preservation Cursors Problems
Consider the following table that stores names, ages and course
names of some students.
Note: We use the NEW keyword to access the new values inserted
in the table.
Outline Advanced Integrity Preservation Cursors Problems
Table: STUDENT
AGE NAME COURSE
25 Hemanth PGDBA
24 Vemula PGDBA
26 Sidhant PGDBA
24 Hitesh PGDBA
Outline Advanced Integrity Preservation Cursors Problems
Basics of cursors
A cursor allows to iterate a set of rows returned by a query and
process each row accordingly. It is used to handle a result set
inside a stored procedure.
MySQL cursors have the following properties:
Read-only: Cursors cannot be used to update data in the
underlying table.
Non-scrollable: Rows can only be fetched in the order
determined by the select statement. One cannot skip rows or
jump to a specific row in the result set.
Asensitive: MySQL cursors are asensitive. It is often faster
because it does not need to make a temporary copy of data.
Outline Advanced Integrity Preservation Cursors Problems
Basics of cursors
A cursor allows to iterate a set of rows returned by a query and
process each row accordingly. It is used to handle a result set
inside a stored procedure.
MySQL cursors have the following properties:
Read-only: Cursors cannot be used to update data in the
underlying table.
Non-scrollable: Rows can only be fetched in the order
determined by the select statement. One cannot skip rows or
jump to a specific row in the result set.
Asensitive: MySQL cursors are asensitive. It is often faster
because it does not need to make a temporary copy of data.
Creating cursors
To initialize the result set for the cursor, before fetching rows from
the result set, the following SQL query is used:
open <cursor name>;
To retrieve the next row pointed by the cursor and move the cursor
to the next row in the result set, the following SQL query is used:
fetch <cursor name> into <list variables>;
Problems