SQL Stored Procedures, Triggers - USNA [PDF]

'John') then. INSERT INTO Students(Alpha, LastName, FirstName, Email, ClassYear, Major). VALUES (Alphavar, LastNamevar,

9 downloads 18 Views 170KB Size

Recommend Stories


Stored Procedures
Learning never exhausts the mind. Leonardo da Vinci

Adding Logging to DB2 Native SQL Stored Procedures
The wound is the place where the Light enters you. Rumi

Triggers
In every community, there is work to be done. In every nation, there are wounds to heal. In every heart,

2018 USNA SHIP LIST
Learning never exhausts the mind. Leonardo da Vinci

PDF Download Learning SQL
When you talk, you are only repeating what you already know. But if you listen, you may learn something

Oracle sql bible pdf
Don't watch the clock, do what it does. Keep Going. Sam Levenson

PDF Download Learning SQL
You miss 100% of the shots you don’t take. Wayne Gretzky

SQL DENGAN POSTGRES [PDF]
5.3 Query = Himpunan. Hasil query sebenarnya merupakan suatu himpunan sebagaimana yang sering kita temui dalam pelajaran matematika. (ingat diagram Venn). Buatlah dua buah tabel berikut: CREATE TABLE tabel1 (id INT);. INSERT INTO tabel1 SELECT 1;. IN

Event Triggers
You have survived, EVERY SINGLE bad day so far. Anonymous

SQL
Life is not meant to be easy, my child; but take courage: it can be delightful. George Bernard Shaw

Idea Transcript


IT360: Applied Database Systems

Stored Procedures and Triggers (Chapter 7, 10b – Kroenke, Chapter 13 p 316 - PHP and MySQL Web Development) 1

Today  Stored procedures  Triggers

2

1

Stored Procedures  A stored procedure or a stored function is a program that is stored within the database and is compiled when used  Stored procedures can receive input parameters and they can return results  Stored procedures can be called from:  Standard languages  Scripting languages  SQL command prompt 3

Stored Procedure Advantages  Greater security as store procedures are always stored on the database server  SQL can be optimized by the DBMS compiler  Code sharing resulting in:  Less work  Standardized processing  Specialization among developers

4

2

Create And Execute Stored Procedures  CREATE PROCEDURE proc_name (param_list) proc_code  call proc_name(value1, …)

5

Stored Procedure Example  Students (Alpha, LastName, FirstName, Email, ClassYear, Major)

 Procedure: Insert a student only if ClassYear < 2015

6

3

Students (Alpha, LastName, FirstName, Email, ClassYear, Major) - MySQL DELIMITER $$ CREATE PROCEDURE insertStudents (Alphavar char(6), LastNamevar varchar(50), FirstNamevar varchar(50), Emailvar varchar(100), ClassYearvar int, Majorvar char(4)) BEGIN if ClassYearvar < 2015 then INSERT INTO Students(Alpha, LastName, FirstName, Email, ClassYear, Major) VALUES (Alphavar, LastNamevar, FirstNamevar, Emailvar, ClassYearvar, Major);

end if; END $$ DELIMITER ; To run: call insertStudents(‘111111’, ’Doe’, ‘John’, ’[email protected]’, 2013, null) 7

Class Exercise  Add code to the previous procedure to prevent anyone named ‘Doe, John’ to be inserted into the DB.  Test the procedure (call ….)

8

4

Class Exercise Solution DELIMITER $$ CREATE PROCEDURE insertStudents (Alphavar char(6), LastNamevar varchar(50), FirstNamevar varchar(50), Emailvar varchar(100), ClassYearvar int, Majorvar char(4)) BEGIN if ClassYearvar < 2015 AND (LastNamevar != ‘Doe’ OR FirstNamevar != ‘John’) then INSERT INTO Students(Alpha, LastName, FirstName, Email, ClassYear, Major) VALUES (Alphavar, LastNamevar, FirstNamevar, Emailvar, ClassYearvar, Major);

end if; END $$ DELIMITER ; To run: call insertStudents(‘111112’, ’Doe’, ‘John’, ’[email protected]’, 2013, null) 9

Triggers  Trigger: stored program that is executed by the DBMS whenever a specified event occurs  Associated with a table [or view]  Two [or three] trigger types: BEFORE, AFTER, [and INSTEAD OF]  Each type can be declared for INSERT, UPDATE, or DELETE 10

5

Uses for Triggers    

Provide complex default values Enforce data constraints Update views – not in MySQL Perform referential integrity actions

11

Create Trigger – MySQL Syntax CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW trigger_code trigger_time: BEFORE, AFTER trigger_event: INSERT, DELETE, UPDATE

12

6

Trigger for Enforcing a Data Constraint – Update CQPR - MySQL DELIMITER $$

/* change default delimiter */

CREATE TRIGGER UpdateCQPR /* provide trigger name */ AFTER UPDATE ON Enrolled /* specify when trigger should be invoked */ FOR EACH ROW BEGIN DECLARE varNewCQPR double; IF (new.Grade is not null) THEN /* create a variable @newGPA to compute and store the new gpa value */ set varNewCQPR = (select avg(PointGrade) from Enrolled E, Grade G where E.Grade = G.LetterGrade and E.Alpha = new.Alpha); /* update the Students table with the new GPA value */ update Students set CQPR = varNewCQPR where Alpha = new.Alpha; END IF; END; $$ DELIMITER ;

/* restore the default delimiter */ 13

Class Exercise  Students (SNb, SName, Email, Gender, ClassYear, GPA)

 Define a trigger: if inserted email is null, change the Email to [email protected], where xxx is the SNb.  Hint: in MySQL, CONCAT(a,b) will return the concatenate the two string a and b

 Insert rows to test the trigger

14

7

Triggers vs. Stored Procedures

15

8

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.