SlideShare a Scribd company logo
1 of 33
‹#›
CSE 480: Database Systems
Lecture 13: Views, Stored Procedures,
Functions, and Triggers
‹#›
Views in SQL
 A view is a “virtual” table that is derived from other tables
 Allows for limited update operations
– Since the table may not physically be stored
 Allows full query operations
‹#›
SQL Views: An Example
 Create a view for Department Managers:
CREATE VIEW MANAGER AS
SELECT FNAME, LNAME, DName, Dnumber, SALARY
FROM EMPLOYEE, DEPARTMENT
WHERE SSN=MGRSSN AND DNO=DNUMBER;
 Find employees who earn more than their managers
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E, MANAGER M
WHERE E.DNO=M.DNUMBER AND E.SALARY > M.SALARY;
 When no longer needed, a view can be dropped:
DROP VIEW MANAGER;
‹#›
View Implementation
 There are two ways to implement a view:
 Approach 1: Query modification
– Modify the view query into a query on the underlying base tables
– Example:
SELECT * FROM Manager WHERE Salary > 100000
becomes
SELECT Fname, Lname, Dname, Dnumber, Salary
FROM EMPLOYEE, DEPARTMENT
WHERE SSN=MgrSSN AND Salary > 100000
– Disadvantage:
 Inefficient for views defined via complex queries
‹#›
View Implementation
 Approach 2: View materialization
– Involves physically creating and keeping a temporary table
– Concerns:
 Maintaining correspondence between the base table and the view
when the base table is updated
 ORACLE
CREATE MATERIALIZED VIEW or CREATE SNAPSHOT
‹#›
Update Views
 Update on a view can be implemented by mapping it to an update on
the underlying base table
UPDATE MANAGER
SET Salary = 1.1*Salary
WHERE Dname = ‘Research’;
– Becomes:
UPDATE EMPLOYEE
SET Salary = 1.1*Salary
WHERE SSN in (SELECT MgrSSN
FROM DEPARTMENT
WHERE DName = ‘Research’);
 Updating views involving joins are not always possible
– Views defined using groups and aggregate functions are not updateable
 For mySQL, the keyword “WITH CHECK OPTION” must be added to the
view definition if the view is to be updated
‹#›
Stored Procedures in MySQL
 A stored procedure contains a sequence of SQL commands stored in
the database catalog so that it can be invoked later by a program
 Stored procedures are declared using the following syntax:
Create Procedure <proc-name>
(param_spec1, param_spec2, …, param_specn )
begin
-- execution code
end;
where each param_spec is of the form:
[in | out | inout] <param_name> <param_type>
– in mode: allows you to pass values into the procedure,
– out mode: allows you to pass value back from procedure to the calling
program
‹#›
Example
 Suppose we want to keep track of the total salaries of employees
working for each department
We need to write a procedure
to update the salaries in
the deptsal table
‹#›
Example
Step 1: Change the delimiter (i.e., terminating character) of
SQL statement from semicolon (;) to something else (e.g., //)
So that you can distinguish between the semicolon of the SQL
statements in the procedure and the terminating character of
the procedure definition
‹#›
Example
Step 2:
1. Define a procedure called updateSalary which takes as
input a department number.
2. The body of the procedure is an SQL command to update
the totalsalary column of the deptsal table.
3. Terminate the procedure definition using the delimiter you
had defined in step 1 (//)
‹#›
Example
Step 3: Change the delimiter back to semicolon (;)
‹#›
Example
Step 4: Call the procedure to update the totalsalary for each
department
‹#›
Example
Step 5: Show the updated total salary in the deptsal table
‹#›
Stored Procedures in MySQL
 Use show procedure status to display the list of stored
procedures you have created
 Use drop procedure to remove a stored procedure
‹#›
Stored Procedures in MySQL
 You can declare variables in stored procedures
 You can use flow control statements (conditional IF-
THEN-ELSE or loops such as WHILE and REPEAT)
 MySQL also supports cursors in stored procedures.
– A cursor is used to iterate through a set of rows returned by a
query so that we can process each individual row.
 To learn more about stored procedures, go to:
http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx
‹#›
Example using Cursors
 The previous procedure updates one row in deptsal table
based on input parameter
 Suppose we want to update all the rows in deptsal
simultaneously
– First, let’s reset the totalsalary in deptsal to zero
‹#›
Example using Cursors
Drop the old procedure
Use cursor to iterate the rows
‹#›
Example using Cursors
 Call procedure
‹#›
Another Example
 Create a procedure to give a raise to all employees
‹#›
Another Example
‹#›
Another Example
‹#›
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
‹#›
Example of Functions
‹#›
Example of Functions
‹#›
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}
‹#›
SQL Triggers: An Example
 We want to create a trigger to update the total salary of a
department when a new employee is hired
‹#›
SQL Triggers: An Example
 Create a trigger to update the total salary of a department
when a new employee is hired:
 The keyword “new” refers to the new row inserted
‹#›
SQL Triggers: An Example
totalsalary increases by 90K
totalsalary did not change
‹#›
SQL Triggers: An Example
 A trigger to update the total salary of a department when an
employee tuple is modified:
‹#›
SQL Triggers: An Example
‹#›
SQL Triggers: An Example
 A trigger to update the total salary of a department when an
employee tuple is deleted:
‹#›
SQL Triggers: An Example
‹#›
SQL Triggers
 To list all the triggers you have created:
mysql> show triggers;

More Related Content

Similar to lecture13.ppt

Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbmsjain.pralabh
 
SQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVsSQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVsFranklin Yamamoto
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ICarlos Oliveira
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedureftz 420
 
Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.Alexey Furmanov
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Ziaur Rahman
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL IISankhya_Analytics
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Stored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh SinghStored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh Singhimdurgesh
 
Major features postgres 11
Major features postgres 11Major features postgres 11
Major features postgres 11EDB
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX PerformanceScott Wesley
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiMuhammed Thanveer M
 

Similar to lecture13.ppt (20)

Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
SQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVsSQL Server Performance Tuning with DMVs
SQL Server Performance Tuning with DMVs
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
Msql
Msql Msql
Msql
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Stored procedure with cursor
Stored procedure with cursorStored procedure with cursor
Stored procedure with cursor
 
Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Aspects of 10 Tuning
Aspects of 10 TuningAspects of 10 Tuning
Aspects of 10 Tuning
 
Stored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh SinghStored procedure Notes By Durgesh Singh
Stored procedure Notes By Durgesh Singh
 
Major features postgres 11
Major features postgres 11Major features postgres 11
Major features postgres 11
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX Performance
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
Mysql
MysqlMysql
Mysql
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

lecture13.ppt

  • 1. ‹#› CSE 480: Database Systems Lecture 13: Views, Stored Procedures, Functions, and Triggers
  • 2. ‹#› Views in SQL  A view is a “virtual” table that is derived from other tables  Allows for limited update operations – Since the table may not physically be stored  Allows full query operations
  • 3. ‹#› SQL Views: An Example  Create a view for Department Managers: CREATE VIEW MANAGER AS SELECT FNAME, LNAME, DName, Dnumber, SALARY FROM EMPLOYEE, DEPARTMENT WHERE SSN=MGRSSN AND DNO=DNUMBER;  Find employees who earn more than their managers SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, MANAGER M WHERE E.DNO=M.DNUMBER AND E.SALARY > M.SALARY;  When no longer needed, a view can be dropped: DROP VIEW MANAGER;
  • 4. ‹#› View Implementation  There are two ways to implement a view:  Approach 1: Query modification – Modify the view query into a query on the underlying base tables – Example: SELECT * FROM Manager WHERE Salary > 100000 becomes SELECT Fname, Lname, Dname, Dnumber, Salary FROM EMPLOYEE, DEPARTMENT WHERE SSN=MgrSSN AND Salary > 100000 – Disadvantage:  Inefficient for views defined via complex queries
  • 5. ‹#› View Implementation  Approach 2: View materialization – Involves physically creating and keeping a temporary table – Concerns:  Maintaining correspondence between the base table and the view when the base table is updated  ORACLE CREATE MATERIALIZED VIEW or CREATE SNAPSHOT
  • 6. ‹#› Update Views  Update on a view can be implemented by mapping it to an update on the underlying base table UPDATE MANAGER SET Salary = 1.1*Salary WHERE Dname = ‘Research’; – Becomes: UPDATE EMPLOYEE SET Salary = 1.1*Salary WHERE SSN in (SELECT MgrSSN FROM DEPARTMENT WHERE DName = ‘Research’);  Updating views involving joins are not always possible – Views defined using groups and aggregate functions are not updateable  For mySQL, the keyword “WITH CHECK OPTION” must be added to the view definition if the view is to be updated
  • 7. ‹#› Stored Procedures in MySQL  A stored procedure contains a sequence of SQL commands stored in the database catalog so that it can be invoked later by a program  Stored procedures are declared using the following syntax: Create Procedure <proc-name> (param_spec1, param_spec2, …, param_specn ) begin -- execution code end; where each param_spec is of the form: [in | out | inout] <param_name> <param_type> – in mode: allows you to pass values into the procedure, – out mode: allows you to pass value back from procedure to the calling program
  • 8. ‹#› Example  Suppose we want to keep track of the total salaries of employees working for each department We need to write a procedure to update the salaries in the deptsal table
  • 9. ‹#› Example Step 1: Change the delimiter (i.e., terminating character) of SQL statement from semicolon (;) to something else (e.g., //) So that you can distinguish between the semicolon of the SQL statements in the procedure and the terminating character of the procedure definition
  • 10. ‹#› Example Step 2: 1. Define a procedure called updateSalary which takes as input a department number. 2. The body of the procedure is an SQL command to update the totalsalary column of the deptsal table. 3. Terminate the procedure definition using the delimiter you had defined in step 1 (//)
  • 11. ‹#› Example Step 3: Change the delimiter back to semicolon (;)
  • 12. ‹#› Example Step 4: Call the procedure to update the totalsalary for each department
  • 13. ‹#› Example Step 5: Show the updated total salary in the deptsal table
  • 14. ‹#› Stored Procedures in MySQL  Use show procedure status to display the list of stored procedures you have created  Use drop procedure to remove a stored procedure
  • 15. ‹#› Stored Procedures in MySQL  You can declare variables in stored procedures  You can use flow control statements (conditional IF- THEN-ELSE or loops such as WHILE and REPEAT)  MySQL also supports cursors in stored procedures. – A cursor is used to iterate through a set of rows returned by a query so that we can process each individual row.  To learn more about stored procedures, go to: http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx
  • 16. ‹#› Example using Cursors  The previous procedure updates one row in deptsal table based on input parameter  Suppose we want to update all the rows in deptsal simultaneously – First, let’s reset the totalsalary in deptsal to zero
  • 17. ‹#› Example using Cursors Drop the old procedure Use cursor to iterate the rows
  • 19. ‹#› Another Example  Create a procedure to give a raise to all employees
  • 22. ‹#› 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
  • 25. ‹#› 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}
  • 26. ‹#› SQL Triggers: An Example  We want to create a trigger to update the total salary of a department when a new employee is hired
  • 27. ‹#› SQL Triggers: An Example  Create a trigger to update the total salary of a department when a new employee is hired:  The keyword “new” refers to the new row inserted
  • 28. ‹#› SQL Triggers: An Example totalsalary increases by 90K totalsalary did not change
  • 29. ‹#› SQL Triggers: An Example  A trigger to update the total salary of a department when an employee tuple is modified:
  • 31. ‹#› SQL Triggers: An Example  A trigger to update the total salary of a department when an employee tuple is deleted:
  • 33. ‹#› SQL Triggers  To list all the triggers you have created: mysql> show triggers;