New version page

UW CSE 444 - Triggers, Impedance Mismatch and Transactions

Documents in this Course
XML

XML

48 pages

SQL

SQL

25 pages

SQL

SQL

42 pages

Recovery

Recovery

30 pages

SQL

SQL

36 pages

Indexes

Indexes

35 pages

Security

Security

36 pages

Wrap-up

Wrap-up

6 pages

SQL

SQL

37 pages

More SQL

More SQL

48 pages

SQL

SQL

35 pages

XML

XML

46 pages

Triggers

Triggers

26 pages

Load more
Upgrade to remove ads

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
Unformatted text preview:

End of SQL: Triggers, Impedance Mismatch and TransactionsTriggersElements of Triggers (in SQL3)Example: Row Level TriggerStatement Level TriggerBad Things Can HappenEmbedded SQLImpedance MismatchThe Impedance Mismatch ProblemPrograms with Embedded SQLInterface: SQL / Host LanguageExampleUsing Shared VariablesSingle-Row Select StatementsCursorsSlide 16Slide 17Slide 18More on CursorsDynamic SQLSlide 21TransactionsMultiple users: single statementsMultiple users: multiple statementsProtection against crashesSlide 26Transactions in SQLTransactions: SerializabilitySerializabilitySlide 30Isolation Levels in SQLEnd of SQL:Triggers, Impedance Mismatch and TransactionsFebruary 6th, 2004TriggersEnable the database programmer to specify:• when to check a constraint,• what exactly to do.A trigger has 3 parts:• An event (e.g., update to an attribute)• A condition (e.g., a query to check)• An action (deletion, update, insertion)When the event happens, the system will check the constraint, and if satisfied, will perform the action.NOTE: triggers may cause cascading effects. Database vendors did not wait for standards with triggers!Elements of Triggers (in SQL3)• Timing of action execution: before, after or instead of triggering event• The action can refer to both the old and new state of the database.• Update events may specify a particular column or set of columns.• A condition is specified with a WHEN clause.• The action can be performed either for• once for every tuple, or• once for all the tuples that are changed by the database operation.Example: Row Level TriggerCREATE TRIGGER NoLowerPricesAFTER UPDATE OF price ON ProductREFERENCING OLD AS OldTuple NEW AS NewTupleWHEN (OldTuple.price > NewTuple.price) UPDATE Product SET price = OldTuple.price WHERE name = NewTuple.nameFOR EACH ROWStatement Level Trigger CREATE TRIGGER average-price-preserveINSTEAD OF UPDATE OF price ON ProductREFERENCING OLD_TABLE AS OldStuf NEW_TABLE AS NewStufWHEN (1000 < (SELECT AVG (price) FROM ((Product EXCEPT OldStuf) UNION NewStuf))DELETE FROM Product WHERE (name, price, company) IN OldStuf;INSERT INTO Product (SELECT * FROM NewStuf)Bad Things Can HappenCREATE TRIGGER Bad-triggerAFTER UPDATE OF price IN ProductREFERENCING OLD AS OldTuple NEW AS NewTupleWHEN (NewTuple.price > 50) UPDATE Product SET price = NewTuple.price * 2 WHERE name = NewTuple.nameFOR EACH ROWEmbedded SQL•direct SQL (= ad-hoc SQL) is rarely used•in practice: SQL is embedded in some application code•SQL code is identified by special syntaxImpedance Mismatch•Example: SQL in C:–C uses int, char[..], pointers, etc–SQL uses tables•Impedance mismatch = incompatible typesThe Impedance Mismatch ProblemWhy not use only one language?•Forgetting SQL: “we can quickly dispense with this idea” [textbook, pg. 351].•SQL cannot do everything that the host language can do.Solution: use cursorsPrograms with Embedded SQLHost language + Embedded SQL PreprocessorHost Language + function callsHost language compilerHost language programPreprocessorHost language compilerCall-levelinterface (CLI):ODBC,JDBC,ADOInterface: SQL / Host LanguageValues get passed through shared variables.Colons precede shared variables when they occur within the SQL statements.EXEC SQL: precedes every SQL statement in the host language.The variable SQLSTATE provides error messages and status reports (e.g., “00000” says that the operation completed with noproblem).EXEC SQL BEGIN DECLARE SECTION; char productName[30];EXEC SQL END DECLARE SECTION;EXEC SQL BEGIN DECLARE SECTION; char productName[30];EXEC SQL END DECLARE SECTION;ExampleProduct (pname, price, quantity, maker)Purchase (buyer, seller, store, pname)Company (cname, city)Person(name, phone, city)Using Shared VariablesVoid simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; /* product-name, company-name */int p, q; /* price, quantity */ char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for name, price and company somehow */ EXEC SQL INSERT INTO Product(pname, price, quantity, maker) VALUES (:n, :p, :q, :c); }Void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; /* product-name, company-name */int p, q; /* price, quantity */ char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for name, price and company somehow */ EXEC SQL INSERT INTO Product(pname, price, quantity, maker) VALUES (:n, :p, :q, :c); }Single-Row Select Statementsint getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; strcpy(n, name); /* copy name to local variable */ EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = :n; return p;}int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; strcpy(n, name); /* copy name to local variable */ EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = :n; return p;}Cursors1. Declare the cursor2. Open the cursor3. Fetch tuples one by one4. Close the cursorCursorsvoid product2XML() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; int p, q; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT pname, price, quantity, maker FROM Product; EXEC SQL OPEN crs;Cursorsprintf(“<allProducts>\n”);while (1) { EXEC SQL FETCH FROM crs INTO :n, :p, :q, :c; if (NO_MORE_TUPLES) break; printf(“ <product>\n”); printf(“ <name> %s </name>\n”, n); printf(“ <price> %d </price>\n”, p); printf(“ <quantity> %d </quantity>\n”, q); printf(“ <maker> %s </maker>\n”, c); printf(“ </product>\n”);}EXECT SQL CLOSE crs;printf(“</allProducts>\n”);}•What is NO_MORE_TUPLES ?#define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”))More on Cursors• cursors can modify a relation as well as read it.• We can determine the order in which the cursor will get tuples by the ORDER BY keyword in the SQL query.• Cursors can be protected against changes to the underlying relations.• The cursor can be a scrolling one: can go forward,


View Full Document
Download Triggers, Impedance Mismatch and Transactions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Triggers, Impedance Mismatch and Transactions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Triggers, Impedance Mismatch and Transactions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?