1 / 18

Active Database Concepts

Active Database Concepts. Active Databases. Active Rules – rules that are automatically triggered by events in the database. Event-Condition-Action. if(event && condition) { action; } Event – an event occurs that triggers a rule Condition – check to see if the rule should be executed

corbin
Télécharger la présentation

Active Database Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Active Database Concepts

  2. Active Databases • Active Rules – rules that are automatically triggered by events in the database.

  3. Event-Condition-Action if(event && condition) { action; } • Event – an event occurs that triggers a rule • Condition – check to see if the rule should be executed • Action – the action to be taken

  4. Event • e.g. INSERT, UPDATE, DELETE. • Types of Events • isolated event • transaction or chain reaction • Event granularity • Row level - tuples • Statement level – statements

  5. Condition • When do you consider the condition? • Immediate – when the event happens. • Deferred – at the end of a transaction. • Detached – in a separate transaction spawned by the trigger.

  6. Immediate Consideration • Three flavors • Before – e.g. date modified • After – e.g. transaction log • Instead of – e.g. view • Oracle uses this model.

  7. Deferred Consideration • Check all of the conditions at the end of a transaction. • You could have transient data that you don't want triggering an event. e.g. Two students switching classes.

  8. SQL3 Trigger Syntax CREATE TRIGGER name {BEFORE|AFTER} <event> ON table [REFERENCING <alias list> ] [FOR EACH [ROW|STATEMENT]] [WHEN (condition)] <body>

  9. Oracle Syntax CREATE [OR REPLACE] TRIGGER name {BEFORE|AFTER|INSTEADOF} {DELETE|INSERT|UPDATE[OF column_list]} ON table_name [ REFERENCING [ OLD AS old_var ] [ NEW AS new_var ] ] [FOR EACH ROW [ WHEN (condition)] trigger PL/SQL body;

  10. Timing Options {BEFORE|AFTER|INSTEADOF} • BEFORE – before the triggering event makes any changes to the database. You can alter the triggering event. • AFTER – executes after the triggering event is processed. Can't alter the triggering event • INSTEAD OF – do something other than the triggering event. Map an insertion on a view to physical tables.

  11. Triggering Statement {DELETE|INSERT|UPDATE[OF column_list]} ON table_name • The type of SQL statement that fires the trigger body. • The name of the table • UPDATE can limit the firing scope to just columns.

  12. FOR EACH ROW Option [FOR EACH ROW [ WHEN (condition)] • FOR EACH ROW - determines if you are using a statement or row level trigger • WHEN clause– a boolean condition to further restrict the trigger. • You can't use methods or stored procedures.

  13. REFERENCING Option [ REFERENCING [ OLD AS old_var ] [ NEW AS new_var ] ] • If you have a row level trigger you can use :old and :new, or your alias, to reference the pre-change and post-change values respectively • You can only do this with INSERT, UPDATE, and DELETE tuples • :old for INSERT? • :new for DELETE?

  14. Trigger Body • Trigger bodies can contain DML SQL statements (INSERT, DELETE, UPDATE) • SELECT INTO or SELECT w/ cursors • No DDL allowed (CREATE, DROP, ALTER) • Conditional Predicate • IF INSERTING THEN … END IF; • IF UPDATING ('EID') THEN … END IF; • IF DELETING THEN … END IF;

  15. Mutating tables • Mutating table = table that is currently being modified by an INSERT, UPDATE, or DELETE • You can't look at a table as it is mutating because you can get inconsistent data. • Statement-triggers don't have this problem as long as they aren't fired from a DELETE CASCADE.

  16. Constraining Tables • Constraining table is a table that a triggering statement reads using SQL or referential integrity. • Triggers can't change PRIMARY, FOREIGN, OR UNIQUE KEY columns of a constraining table. • One exception – BEFORE ROW and AFTER ROW single row INSERT statements.

  17. Trigger Firing Order • BEFORE statement trigger • For each row • BEFORE row trigger • Triggering statement • AFTER row trigger • AFTER statement trigger

  18. Consistency and Termination • Trigger failure results in a data rollback. • Triggers can create a chain reaction of cascading triggers. • Cascading triggers can create loops. CREATE OR REPLACE TRIGGER loop_ais AFTER INSERT ON loop BEGIN INSERT INTO loop values(1); END;

More Related