Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Similar documents
PHP Queries and HTML Forms Lecture 23

Minimal Spanning Trees

The Critical-Path Algorithm

Insertions, Deletions, and Updates

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Solving Recursive Sequences by Iteration

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Friends and Unary Operators

The Decreasing-Time Algorithm

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

The Class Construct Part 1

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Sampling Distribution Examples Sections 15.4, 15.5

Scheduling and Digraphs

The Pairwise-Comparison Method

Rotations and Translations

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Density Curves Sections

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

LR Parsing - Conflicts

Implementing Linked Lists

Introduction to Databases

Recursive Descent Parsers

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

The Plurality-with-Elimination Method

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

The Traveling Salesman Problem Brute Force Method

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XQuery FLOWR Expressions Lecture 35

Relational Databases

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Binary Tree Applications

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Displaying Distributions - Quantitative Variables

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Scope and Parameter Passing

The Graphics Pipeline

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

The Traveling Salesman Problem Cheapest-Link Algorithm

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

The Projection Matrix

Magnification and Minification

Scope and Parameter Passing

Street-Routing Problems

Inheritance: The Fundamental Functions

Building the Abstract Syntax Trees

Basic CSS Lecture 17

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Introduction to Databases

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

The CYK Parsing Algorithm

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

The Coefficient of Determination

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

Introduction to Compiler Design

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Function Definition Syntax Tree

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Relational Databases Lecture 2

The Graphics Pipeline

Recognition of Tokens

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

List Iterator Implementation

The Normal Distribution

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

MySQL Creating a Database Lecture 3

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

LR Parsing - The Items

Basic PHP Lecture 17

Programming Languages

Inheritance: The Fundamental Functions

JFlex Regular Expressions

Programming Languages

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Class Construct Part 2

Nondeterministic Programming in C++

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Recursive Linked Lists

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Stacks and their Applications

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Ambient and Diffuse Light

Transcription:

Triggers Lecture 14 Robb T. Koether Hampden-Sydney College Wed, Feb 14, 2018 Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 1 / 22

1 Triggers 2 Cascading Triggers 3 Update and Insert Triggers 4 Displaying and Dropping Triggers 5 Examples Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 2 / 22

Outline 1 Triggers 2 Cascading Triggers 3 Update and Insert Triggers 4 Displaying and Dropping Triggers 5 Examples Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 3 / 22

Triggers Triggers CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW trigger_action A trigger is an event-action pair. When the trigger_event occurs, the trigger_action is taken. The trigger_time is either BEFORE or AFTER. The trigger_event is INSERT, DELETE, or UPDATE. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 4 / 22

Triggers The trigger_action is a statement (query) or series of statements to be executed. If there is more than one statement, then they must be enclosed in a BEGIN-END block and separated by semicolons. The action is executed once for each tuple that is affected by the event. For an insertion, the affected tuples are the inserted tuples. For a deletion, the affected tuples are the deleted tuples. For an update, the affected tuples are the updated tuples. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 5 / 22

Triggers The keyword OLD refers to a deleted or updated tuple. The keyword NEW refers to an added or updated tuple. For an update event, OLD and NEW will always refer to the same tuple, but before and after the update. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 6 / 22

Triggers Triggers CREATE TRIGGER fire_emp AFTER DELETE ON employees FOR EACH ROW DELETE FROM dependents WHERE dependents.ssn = OLD.ssn For example, when an employee is deleted from employees, all corresponding tuples must also be deleted from dependents. What about the table works_on? Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 7 / 22

Triggers Triggers CREATE TRIGGER drop_emp AFTER DELETE ON employees FOR EACH ROW BEGIN DELETE FROM dependents WHERE dependents.ssn = OLD.ssn; DELETE FROM works WHERE works.ssn = OLD.ssn; END; However, the semicolon will prematurely end the CREATE command. What to do? Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 8 / 22

Triggers Triggers DELIMITER # CREATE TRIGGER drop_emp AFTER DELETE ON employees FOR EACH ROW BEGIN DELETE FROM dependents WHERE dependents.ssn = OLD.ssn; DELETE FROM works WHERE works.ssn = OLD.ssn; END# DELIMITER ; To handle that situation, we need to temporarily redefine the delimiter, which has been the semicolon. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 9 / 22

Outline 1 Triggers 2 Cascading Triggers 3 Update and Insert Triggers 4 Displaying and Dropping Triggers 5 Examples Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 10 / 22

Cascading Triggers Suppose that we delete a department from the departments table. Then all employees in that department should be deleted from the employees table. But then all dependents of those employees should be deleted from the dependents table. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 11 / 22

Cascading Triggers Therefore, we need two triggers. One trigger will delete all employees from the employees table when the department is deleted from the departments table. The other trigger will delete all dependents of an employee from the dependents table when the employee is deleted from the employees table. We see from this example that triggers may cascade. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 12 / 22

Cascading Triggers Cascading Triggers CREATE TRIGGER drop_dept AFTER DELETE ON departments FOR EACH ROW DELETE FROM employees WHERE employees.dept = OLD.dept; CREATE TRIGGER drop_emp AFTER DELETE ON employees FOR EACH ROW DELETE FROM dependents WHERE dependents.ssn = OLD.ssn; The trigger drop_dept will automatically invoke the trigger drop_emp. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 13 / 22

Outline 1 Triggers 2 Cascading Triggers 3 Update and Insert Triggers 4 Displaying and Dropping Triggers 5 Examples Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 14 / 22

Update Triggers Updating a Social Security Number CREATE TRIGGER update_ssn AFTER UPDATE ON employee FOR EACH ROW UPDATE dependents SET dependents.ssn = NEW.ssn WHERE dependents.ssn = OLD.ssn; This trigger will update the dependents SSN s when the employees SSN s are updated. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 15 / 22

Update Triggers Updating a Social Security Number CREATE TRIGGER update_ssn AFTER UPDATE ON employee FOR EACH ROW UPDATE dependents SET dependents.ssn = NEW.ssn WHERE dependents.ssn = OLD.ssn; This trigger will update the dependents SSN s when the employees SSN s are updated. Note the use of NEW and OLD. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 15 / 22

Update Triggers Insert a Friend CREATE TRIGGER make_friend AFTER INSERT ON friends FOR EACH ROW INSERT INTO friends VALUES (NEW.user2, NEW.user1); In the Tigerface database, this trigger in intended to insert the same pair in reverse order. It does not work. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 16 / 22

Update Triggers Insert a Friend CREATE TRIGGER make_friend AFTER INSERT ON friends FOR EACH ROW INSERT INTO friends VALUES (NEW.user2, NEW.user1); In the Tigerface database, this trigger in intended to insert the same pair in reverse order. It does not work. Why not? Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 16 / 22

Update Triggers Insert a User CREATE TRIGGER update_stats AFTER INSERT ON users FOR EACH ROW UPDATE user_stats SET user_count = user_count + 1; Suppose we have another table user_stats in which we record, among other things, the number of users. Write a similar trigger for deletions from users. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 17 / 22

Outline 1 Triggers 2 Cascading Triggers 3 Update and Insert Triggers 4 Displaying and Dropping Triggers 5 Examples Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 18 / 22

DisplayingTriggers Displaying Triggers SHOW TRIGGERS; We may display all triggers. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 19 / 22

Displaying and Dropping Triggers Dropping Triggers DROP TRIGGER trigger_name; Triggers are given names so that they can be dropped. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 20 / 22

Outline 1 Triggers 2 Cascading Triggers 3 Update and Insert Triggers 4 Displaying and Dropping Triggers 5 Examples Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 21 / 22

Examples Using the company database, create triggers for Deleting a project. Updating a department number. Robb T. Koether (Hampden-Sydney College) Triggers Wed, Feb 14, 2018 22 / 22