You are on page 1of 21

Procedural SQL – Part2

Database Management Systems II


SICT 3308

1st Semester 20192020 (20191)

Iyad H. Alshami – SICT 3308 1


Triggers

Iyad H. Alshami – SICT 3308 2


MySQL - Triggers
• A trigger is a program stored in the database and is called
automatically when a triggering event occurs.

• It is associated with a table, and that activates when a particular


event occurs for the table.
• Some uses for triggers are to perform checks of values to be inserted into
a table or to perform calculations on values involved in an update.

• A trigger is defined to activate when an INSERT, DELETE, or


UPDATE statement executes for the associated table.

• A trigger can be set to activate either before or after the


triggering statement.

Iyad H. Alshami – SICT 3308


MySQL - Triggers

Iyad H. Alshami – SICT 3308 4


MySQL - Triggers
DELIMITER $$
CREATE TRIGGER triggerName
BEFORE|AFTER EVENT ON tableName
#EVENT is: INSERT, UPDATE or DELETE
FOR EACH ROW #default in MySQL
BEGIN
#executable code
END

Iyad H. Alshami – SICT 3308 5


MySQL - Triggers
• BEFORE|AFTER EVENT ON tableName
• BEFORE|AFTER enable the developer to use OLD. and NEW.
values based on the determined EVENT.

• with insert event no OLD values, it just NEW values.


• with update event both of OLD & NEW values are exist.
• with delete event no NEW values, it just OLD values.

• Using BEFORE: means activate this trigger before


executing the SQL event.

• Using AFTER: means activate this trigger after executing


the SQL event.

Iyad H. Alshami – SICT 3308 6


MySQL - Triggers

CREATE TABLE gpaUpdateLog(


user varchar(20),
udate date,
sid int(3),
oldgpa float(5,2), DELIMITER $$
newgpa float(5,2) CREATE TRIGGER gpaUpdate
); BEFORE UPDATE ON students
FOR EACH ROW
BEGIN
INSERT INTO gpaUpdateLog
VALUES(USER(),
NOW(),
OLD.sid,
OLD.gpa,
NEW.gpa);
END;
Iyad H. Alshami – SICT 3308 7
Cursors

Iyad H. Alshami – SICT 3308 8


Cursors
• A cursor is a pointer to a set of records returned by a
SELECT statement.

• It enables developer to take a set of records and deal with it


on a row-by-row basis.

• Cursor properties:
• A cursor is not updatable it is read-only
• A cursor will not reflect changes in its source tables.
• A cursor is not scrollable, it can be traversed in forward direction
only, forward
• Only can fetch the data in the order determined by the SELECT statement
• Can not skip records from fetching.

Iyad H. Alshami – SICT 3308


Cursors

• There are two kinds of Cursors:


• Asensitive where the cursor points to the actual data.
• Insensitive where the cursor uses a temporary copy of data.

• Asensitive cursor performs faster than insensitive because in dose


not need to create a copy of the data.
• Any change made by any other connection affect the data that is being used will slow
down the asensitive cursor.

• Cursors always used in Stored Procedures, Functions and


Triggers.

• MySQL cursors are asensitive cursors.


Iyad H. Alshami – SICT 3308
Defining and Using Cursors

• In order to use cursor, we need to perform the following


steps:
1. Declare cursor
• Cursor always declared after any variable declaration
2. Open cursor
• Initialize the result set for the cursor.
3. Fetch cursor data into variables
• Retrieve the next row pointed by cursor and move the pointer to the
next row in the result set.
4. CLOSE cursor
• It is a good practice to close cursor when it is no longer used.

Iyad H. Alshami – SICT 3308


Defining and Using Cursors

1- Declare cursor:
• DECLARE cursor-name CURSOR FOR SELECT ...;
• DECLARE CONTINUE HANDLER FOR NOT FOUND
• Specify what to do when no more records found
• DECLARE flag INT;
• DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;
2- Open cursor:
• OPEN cursor-name;
3- Fetch cursor data into variables:
• FETCH cursor-name INTO variable [, variable];
4- CLOSE cursor:
• CLOSE cursor-name;
Iyad H. Alshami – SICT 3308
Cursor Example
#using cursor to sum salary for male employees
DELIMITER $$
CREATE Procedure maleSalarySum(OUT sumSalary Decimal(10,2))
BEGIN
DECLARE Sal, sumSal decimal(10,2);
DECLARE continueFlag int default 0;
DECLARE maleCursor CURSOR FOR SELECT Salary FROM salesreps where sex='m';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET continueFlag = 1;
OPEN maleCursor;
SET Sal = 0;
SET sumSal= 0;
WHILE continueFlag = 0 DO
FETCH maleCursor INTO Sal;
IF continueFlag = 0 THEN
SET sumSal = sumSal+Sal;
END IF;
END WHILE;
CLOSE maleCursor;
SET sumSalary = sumSal;
END
Iyad H. Alshami – SICT 3308
A procedure to create email list using
cursor
DELIMITER $$
DROP PROCEDURE IF EXISTS emailgroup;
CREATE PROCEDURE emailgroup (INOUT emailList varchar(4000))
BEGIN
DECLARE continueFlag INT DEFAULT 0;
DECLARE useremail varchar(100) DEFAULT "";
DECLARE email_cursor CURSOR FOR SELECT email FROM users;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET continueFlag = 1;
OPEN email_cursor;
WHILE continueFlag = 0 DO
FETCH email_cursor INTO useremail;
IF continueFlag = 0 THEN
SET emailList = CONCAT(useremail,";",emaillist);
END IF;
END WHILE;
CLOSE email_cursor;
END

Iyad H. Alshami – SICT 3308


Views

Iyad H. Alshami – SICT 3308 16


MySQL - Views

• A database view is a virtual table or logical table which is


defined as a SQL query with joins.
• Because a view is similar to a table, which consists of rows and
columns, so we can query data against it.

Iyad H. Alshami – SICT 3308 17


MySQL - Views

CREATE or Replace VIEW view_name


AS SELECT …

CREATE or Replace VIEW Student_grades


AS SELECT s.sid,s.sname,r.sem,c.cid,c.cname,r.grade
From Student_tbl s, Course_tble c, Reg_tbl r
Where s.sid = r.sid
AND c.cid = r.cid;

Iyad H. Alshami – SICT 3308 18


MySQL - Views

Iyad H. Alshami – SICT 3308 19


MySQL - Views

• Can we insert data via a VIEW object?

CREATE VIEW std SELECT sid, sname, sdid


FROM Student_tbl;

INSERT INTO std (sid, sname, sdid)


VALUE(440,"Ahmad",20);

Iyad H. Alshami – SICT 3308 20


MySQL - Views

• Can we insert data via a VIEW object?

CREATE VIEW std_fullas SELECT sid, sname, sdid,


dname
FROM Student_tbl, Dept_tbl
WHERE Student_tbl.sdid = Dept_tbl.did;

INSERT INTO std_full(sid, sname,sdid,dname) Can not modify more than one
VALUE(440,"Ahmad",20,"dpt"); base table through a join view

INSERT INTO std_full(sid, sname,sdid) Okay


VALUE(440,"Ahmad",20);

Iyad H. Alshami – SICT 3308 21


That is enough for Procedural SQL Programming

• Next Chapter will be Chapter 16


“Disk Storage, Basic File Structures, Hashing, and Modern Storage Architectures”

Iyad H. Alshami – SICT 3308 22

You might also like