



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Grades will be posted on Blackboard
Due in 12 days (Sep. 28)
3
Example: enforcing referential integrity constraints Generalize to arbitrary constraints?
Example: When price rises above $20 per share, sell Example: When enrollment is at the limit and more students try to register, email the instructor
4
When event occurs, test condition; if condition is satisfied, execute action
Event: whenever there comes a new student… Condition: with GPA higher than 3.0… Action: then make him/her take CPS116!
5
Event
Condition
Action
6
INSERT ON table DELETE ON table UPDATE [OF column ] ON table
FOR EACH ROW modified FOR EACH STATEMENT that performs modification
AFTER or BEFORE the triggering event
OLD ROW: the modified row before the triggering event NEW ROW: the modified row after the triggering event OLD TABLE: a hypothetical read-only table containing all modified rows before the triggering event NEW TABLE: a hypothetical table containing all modified rows after the triggering event ) Not all of them make sense all the time, e.g. AFTER INSERT statement-level triggers
CREATE TRIGGER CPS116AutoRecruit AFTER INSERT ON Student REFERENCING NEW TABLE AS newStudents FOR EACH STATEMENT INSERT INTO Enroll (SELECT SID, ’CPS116’ FROM newStudents WHERE GPA > 3.0);
9
Never give faculty more than 50% raise in one update
CREATE TRIGGER NotTooGreedy BEFORE UPDATE OF salary ON Faculty REFERENCING OLD ROW AS o, NEW ROW AS n FOR EACH ROW WHEN (n.salary > 1.5 * o.salary) SET n.salary = 1.5 * o.salary;
) BEFORE triggers are often used to “condition” data
) Another option is to raise an error in the trigger body to abort the transaction that caused the trigger to fire
10
If the average GPA of students inserted by this statement exceeds 3.0, do …
Statement-level triggers require significant amount of state to be maintained in OLD TABLE and NEW TABLE However, a row-level trigger does get fired for each row, so complex row-level triggers may be inefficient for statements that generate lots of modifications
11
Give faculty a raise if GPA’s in one update statement are all increasing CREATE TRIGGER AutoRaise AFTER UPDATE OF GPA ON Student REFERENCING OLD TABLE AS o, NEW TABLE AS n FOR EACH STATEMENT WHEN (NOT EXISTS(SELECT * FROM o, n WHERE o.SID = n.SID AND o.GPA >= n.GPA)) UPDATE Faculty SET salary = salary + 1000;
) A row-level trigger would be difficult to write in this case
12
Recursive firing of triggers Action of one trigger causes another trigger to fire Can get into an infinite loop
Note that you can rename columns in view definition
21
No aggregation No subqueries
See the slide titled “An impossible case”
22
An index is an auxiliary persistent data structure Search tree (e.g., B+^ -tree), lookup table (e.g., hash table), etc. ) More on indexes in the second half of this course! An index on R. A can speed up accesses of the form R. A = value R. A > value (sometimes; depending on the index type) An index on ( R. A 1 , …, R. A (^) n ) can speed up R. A 1 = value 1 ∧ … ∧ R. An = valuen ( R. A 1 , … R. An ) > ( value 1 , …, valuen ) (again depends) ) Is an index on ( R. A , R. B ) equivalent to one on ( R. B , R. A )? ) How about an index on R. A plus another index on R. B?
23
SELECT * FROM Student WHERE name = ’Bart’ Without an index on Student.name: must scan the entire table if we store Student as a flat file of unordered rows With index: go “directly” to rows with name = ’Bart’
SELECT * FROM Student, Enroll WHERE Student.SID = Enroll.SID; Without any index: for each Student row, scan the entire Enroll table for matching SID
24
With UNIQUE, the DBMS will also enforce that { column_name 1 , …, column_namen } is a key of table_name
Automatic index selection is still an area of active research