Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with...
-
Upload
shavonne-short -
Category
Documents
-
view
213 -
download
0
Transcript of Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with...
Different Constraint TypesType Where Declared When activated Guaranteed to hold?
Attribute with attribute on insertion not if CHECK or update subquery
Tuple relation schema insertion or not if CHECK update to subquery relation
Assertion database schema on change to Yes any relation mentioned
Giving 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 KEY
CREATE DOMAIN ssnDomain INT CONSTRAINT ninedigits CHECK (VALUE >= 100000000 AND VALUE <= 999999999
CONSTRAINT rightage CHECK (age >= 0 OR status = “dead”)
Altering ConstraintsALTER TABLE Product DROP CONSTRAINT positivePrice
ALTER 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 Trigger
CREATE TRIGGER NoLowerPrices
AFTER UPDATE OF price ON ProductREFERENCING OLD AS OldTuple NEW AS NewTupleWHEN (OldTuple.price > NewTuple.price) UPDATE Product SET price = OldTuple.price WHERE name = NewTuple.name
FOR EACH ROW
Statement Level Trigger CREATE TRIGGER average-price-preserveINSTEAD OF UPDATE OF price ON Product
REFERENCING OLD_TABLE AS OldStuff NEW_TABLE AS NewStuffWHEN (1000 < (SELECT AVG (price) FROM ((Product EXCEPT OldStuff) UNION NewStuff))DELETE FROM Product WHERE (name, price, company) IN OldStuff;INSERT INTO NewStuff (SELECT * FROM NewStuff)
Bad Things Can HappenCREATE TRIGGER Bad-trigger
AFTER UPDATE OF price IN ProductREFERENCING OLD AS OldTuple NEW AS NewTuple
WHEN (NewTuple.price > 50)
UPDATE Product SET price = NewTuple.price * 2 WHERE name = NewTuple.name
FOR EACH ROW
Embedded 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 problem
So: we use cursors.
Programs with SQL
Host language + Embedded SQL
Preprocessor
Host Language + function calls
Host language compiler
Host language program
Preprocessor
Host language compiler
The Impedance Mismatch Problem
The host language manipulates variables, values, pointers
SQL 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 Language
Values 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 Statements
Void 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), Abs(-n).