DOC PREVIEW
UW CSE 444 - Different Constraint Types

This preview shows page 1-2-3-4-5 out of 16 pages.

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

Unformatted text preview:

Different Constraint TypesGiving Names to ConstraintsAltering ConstraintsTriggersElements of Triggers (in SQL3)Example: Row Level TriggerStatement Level TriggerBad Things Can HappenEmbedded SQLPrograms with SQLThe Impedance Mismatch ProblemInterface: SQL / Host LanguageUsing Shared VariablesSingle-Row Select StatementsCursorsMore on CursorsDifferent Constraint TypesType Where Declared When activated Guaranteed to hold?Attribute with attribute on insertion not if CHECK or update subqueryTuple relation schema insertion or not if CHECK update to subquery relationAssertion database schema on change to Yes any relation mentionedGiving Names to ConstraintsWhy give names? In order to be able to alter constraints.Add the keyword CONSTRAINT and then a name:ssn CHAR(50) CONSTRAINT ssnIsKey PRIMARY KEYCREATE DOMAIN ssnDomain INT CONSTRAINT ninedigits CHECK (VALUE >= 100000000 AND VALUE <= 999999999CONSTRAINT rightage CHECK (age >= 0 OR status = “dead”)Altering ConstraintsALTER TABLE Product DROP CONSTRAINT positivePriceALTER TABLE Product ADD CONSTRAINT positivePrice CHECK (price >= 0)ALTER DOMAIN ssn ADD CONSTRAINT no-leading-1s CHECK (value >= 200000000)DROP ASSERTION assert1.TriggersEnable 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 NewStuf (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 is rarely used: usually, SQL is embedded in someapplication code.We need some method to reference SQL statements. But: there is an impedance mismatch problemSo: we use cursors.Programs with SQLHost language + Embedded SQL PreprocessorHost Language + function callsHost language compilerHost language programPreprocessorHost language compilerThe Impedance Mismatch ProblemThe host language manipulates variables, values, pointersSQL manipulates relations.There is no construct in the host language for manipulating relations.Why not use only one language?• Forgetting SQL: definitely not a good idea!• SQL cannot do everything that the host language can do.Interface: 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;Using Shared VariablesVoid simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for productName and company somehow */ EXEC SQL INSERT INTO Product(name, company) VALUES (:productName, :company); }Single-Row Select StatementsVoid getPrice() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; integer price; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;/* read value of product name */ EXEC SQL SELECT price INTO :price FROM Product WHERE Product.name = :productName; /* print out value of price */ }CursorsEXEC SQL DECLARE cursorName CURSOR FOR SELECT …. FROM …. WHERE …. ;EXEC SQL OPEN cursorName;while (true) { EXEC SQL FETCH FROM cursorName INTO :variables; if (NO_MORE_TUPLES) break; /* do something with values */ } EXEC SQL CLOSE cursorName;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, backward +n, -n, Abs(n),


View Full Document

UW CSE 444 - Different Constraint Types

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
Download Different Constraint Types
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 Different Constraint Types 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 Different Constraint Types 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?