Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assertions and Triggers

Similar presentations


Presentation on theme: "Assertions and Triggers"— Presentation transcript:

1 Assertions and Triggers 7.4-7.5
By Cyndra Graves And Tyre Pickett

2 Assertions An assertion is a Boolean-valued SQL expression that must always be true. Assertions are easy for a programmers to use. Hard to implement efficiently because the DBMS must deduce if the modification could change the truth value of the assertion.

3 Creating Assertions The form for creating an assertion is…
The assertion condition must be true when the assertion is created and must stay true. Any modifications that causes it to become false will be rejected. **MySQL does not support Assertions

4 Example of Assertions

5 7.5 Triggers A trigger is a series of actions that are associated with certain events, and are performed whenever that event arises Triggers (ECA Rules) Triggers execute when certain events (specified by the programmer) occur. Examples of common events are inserts, deletes, updates, or a transaction end Once awakened by its triggering event, the trigger tests a condition. If the condition does not hold then nothing else associated with the trigger happens If the condition is satisfied, the action associated with the trigger is performed. Examples of possible actions is to modify in someway, abort the transactions, or any sequence of database operations, even those that are not connected to the triggering event

6 Triggers in SQL Main features:
Checking the trigger’s condition and the action of the trigger may be executed either on the current state of the database that exists before the triggering event is executed or after it is executed The condition and action can refer to both old and/or new values of tuples that were updated during the event It is possible to define update events that are limited to a particular attribute or set of attributes The programmer can specify that the trigger executes either Once for each modified tuple (row-level trigger) Once for all the tuples that are changed in and SQL statement (statement- level trigger)

7 Trigger Syntax and Semantics
CREATE TRIGGER statement We need a clause to indicate the triggering event and specify whether the trigger uses the database state before or after the event A REFERENCING clause to be able to refer to the tuple being modified. It allows us to give names to the tuple both before and after the change such as NewTuple, OldTuple A clause specifying row-level or statement-level trigger The condition using WHEN and a Boolean expression Then the action, one or more SQL statements

8 A Trigger Example The trigger is fired after a delete on the table of employees The data that is deleted is inserted into the Audit table A deletion of the employee with the ID 6 would cause the following information to be inserted into the Audit table 6 Chris Deleted -- After Delete Trigger :52:13.867

9 Different Options for Trigger Design
Where we have the keyword AFTER in the previous example may be replaced with BEFORE or INSTEAD OF Other possible triggering events besides DELETE are UPDATE and INSERT. UPDATE can have UPDATE OF clause to specify certain attributes. (Shown in book’s example pg.333) WHEN clause is optional. The previous example does not have a when clause so the action is executed whenever the trigger is awakened. In the book’s example, the WHEN clause tests a condition and the trigger is executed only when it is true Any number of SQL statements can represent the action The trigger can specify FOR EACH ROW or the default is FOR EACH STATEMENT Statement-level can’t refer to old and new tuples directly

10 Possible Reasons for Triggers
If an insertion, deletion, or update violates a constraint, triggers can specify new actions to take when this occurs BEFORE triggers could be used to fix inserted tuples in some way before they are inserted Disallow or undo modifications To move deleted information to a new table

11 References Garcia-Molina, Hector, et al. Database Systems: the Complete Book 2nd Edition. Pearson, 2014.


Download ppt "Assertions and Triggers"

Similar presentations


Ads by Google