Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared Statements Function and Triggers

Similar presentations


Presentation on theme: "Prepared Statements Function and Triggers"— Presentation transcript:

1 Prepared Statements Function and Triggers
SQL prepared statement SQL Functions SQL Triggers

2 SQL prepared statements
Create prepared statement and execute it: DELIMITER // create procedure p30(search_value int) begin = concat('select * from t where s1 = ', search_value); prepare st execute st; end; // //DELIMITER ;

3 SQL prepared statements
Exercise: Create a prepared statement that will search and return the row with employee ID number 8?

4 SQL Functions What is the different between procedure and functions?
Functions look a lot like procedures. The only noticeable syntax differences are that we say create function instead of create procedure, and we need a RETURNS clause to say what the data type of the function is, and we need a RETURN statement because a function must return a value.

5 SQL Functions Functions are declared using the following syntax:
function <function-name> (param_spec1, …, param_speck) returns <return_type> [not] deterministic allow optimization if same output for the same input (use RAND not deterministic ) Begin -- execution code end; where param_spec is: [in | out | in out] <param_name> <param_type> You need ADMIN privilege to create functions on mysql-user server

6 SQL Functions CREATE FUNCTION DELIMITER //
CREATE FUNCTION giveRaise (oldVal double, amount double) Return double Deterministic BEGIN declare newVal double; set newVal = oldVal * (1 + amount); return newVal; END; // DElMITER ; Now lets use the function that we have created SELECT emp_name, salary, giveRaise(salary, 0.1) as newSalary FROM employee;

7 SQL Triggers To monitor a database and take a corrective action when a condition occurs Examples: Charge $10 overdraft fee if the balance of an account after a withdrawal transaction is less than $500 Limit the salary increase of an employee to no more than 5% raise CREATE TRIGGER trigger-name trigger-time trigger-event ON table-name FOR EACH ROW trigger-action; trigger-time  {BEFORE, AFTER} trigger-event  {INSERT,DELETE,UPDATE}

8 Triggers Example Triggers Example:
We want to create a trigger to update the total salary of a department when a new employee is hired delimiter // create trigger update_salary after insert on employee for each row begin if new.dept_no is not null then update deptsal set totalsalary = totalsalary + new.salary where dept_no = new.dept_no; end if; end ; // delimiter ; The keyword new refers to the new row inserted

9 Triggers Example continued
First lets update the deptsal table and set the totalsalry to 0 update deptsal set totalsalary = 0; Call the updateSalary procedure to calculate the totalsalary of all rows call updateSalary(); Lets verify the totalsalary of each department in detpsal table the insert the following rows insert into employee values (9, 'Fred', null, 1, ); Now it time to verify if the trigger works Select * from deptsal; Try to insert an other row and see what happens?

10 Triggers continued Exercise:
Create a trigger to update the total salary of a department when an employee row is modified. Try modify a row in the employee table and verify the changes in totalsalary. Create a trigger to update the total salary of a department when an employee tuple is deleted Try to modify a row in the employee table and verify the changes in totalsalary.


Download ppt "Prepared Statements Function and Triggers"

Similar presentations


Ads by Google