Docsity

Database Systems: Triggers, Views, and Indexes, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
SQL: Part III
CPS 116
Introduction to Database Systems
2
Announcements
Homework #1 will be graded by next week
Grades will be posted on Blackboard
Homework #2 assigned today (Sep. 16)
Due in 12 days (Sep. 28)
Discussion session next Wednesday?
3
“Active” data
Constraint enforcement: When an operation violates
a constraint, abort the operation or try to “fix” the
data
Example: enforcing referential integrity constraints
Generalize to arbitrary constraints?
Data monitoring: When something happens to the
data, automatically execute some action
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
Triggers
A trigger is an event-condition-action rule
When event occurs, test condition; if condition is
satisfied, execute action
Example:
Event: whenever there comes a new student…
Condition: with GPA higher than 3.0…
Action: then make him/her take CPS116!
5
Trigger example
CREATE TRIGGER CPS116AutoRecruit
AFTER INSERT ON Student
REFERENCING NEW ROW AS newStudent
FOR EACH ROW
WHEN (newStudent.GPA > 3.0)
INSERT INTO Enroll
VALUES(newStudent.SID, ’CPS116’);
Event
Condition
Action
6
Trigger options
Possible events include:
INSERT ON table
DELETE ON table
UPDATE [OF column] ON table
Trigger can be activated:
FOR EACH ROW modified
FOR EACH STATEMENT that performs modification
Action can be executed:
AFTER or BEFORE the triggering event
pf3
pf4
pf5

Partial preview of the text

Download Database Systems: Triggers, Views, and Indexes and more Slides Introduction to Database Management Systems in PDF only on Docsity!

SQL: Part III

CPS 116

Introduction to Database Systems

Announcements

™ Homework #1 will be graded by next week

ƒ Grades will be posted on Blackboard

™ Homework #2 assigned today (Sep. 16)

ƒ Due in 12 days (Sep. 28)

™ Discussion session next Wednesday?

3

“Active” data

™ Constraint enforcement: When an operation violates

a constraint, abort the operation or try to “fix” the

data

ƒ Example: enforcing referential integrity constraints ƒ Generalize to arbitrary constraints?

™ Data monitoring: When something happens to the

data, automatically execute some action

ƒ 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

Triggers

™ A trigger is an event-condition-action rule

ƒ When event occurs, test condition; if condition is satisfied, execute action

™ Example:

ƒ Event: whenever there comes a new student… ƒ Condition: with GPA higher than 3.0… ƒ Action: then make him/her take CPS116!

5

Trigger example

CREATE TRIGGER CPS116AutoRecruit

AFTER INSERT ON Student

REFERENCING NEW ROW AS newStudent

FOR EACH ROW

WHEN (newStudent.GPA > 3.0)

INSERT INTO Enroll

VALUES(newStudent.SID, ’CPS116’);

Event

Condition

Action

6

Trigger options

™ Possible events include:

ƒ INSERT ON table ƒ DELETE ON table ƒ UPDATE [OF column ] ON table

™ Trigger can be activated:

ƒ FOR EACH ROW modified ƒ FOR EACH STATEMENT that performs modification

™ Action can be executed:

ƒ AFTER or BEFORE the triggering event

Transition variables

™ 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

  • Can use only NEW TABLE ƒ BEFORE DELETE row-level triggers
  • Can use only OLD ROW ƒ etc.

Statement-level trigger example

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

BEFORE trigger example

™ 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

Statement- vs. row-level triggers

Why are both needed?

™ Certain triggers are only possible at statement level

ƒ If the average GPA of students inserted by this statement exceeds 3.0, do …

™ Simple row-level triggers are easier to implement

and may be more efficient

ƒ 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

Another statement-level trigger

™ 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

System issues

™ Recursive firing of triggers ƒ Action of one trigger causes another trigger to fire ƒ Can get into an infinite loop

  • Some DBMS restrict trigger actions
  • Most DBMS set a maximum level of recursion (16 in DB2) ™ Interaction with constraints (very tricky to get right!) ƒ When do we check if a triggering event violates constraints?
  • After a BEFORE trigger (so the trigger can fix a potential violation)
  • Before an AFTER trigger ƒ AFTER triggers also see the effects of, say, cascaded deletes caused by referential integrity constraint violations (Based on DB2; other DBMS may implement a different policy!)

An impossible case

CREATE VIEW HighGPAStudent AS

SELECT SID, GPA FROM Student

WHERE GPA > 3.7;

INSERT INTO HighGPAStudent

VALUES(987, 2.5);

™ No matter what you do on Student , the inserted row

will not be in HighGPAStudent

A case with too many possibilities

CREATE VIEW AverageGPA(GPA) AS

SELECT AVG(GPA) FROM Student;

ƒ Note that you can rename columns in view definition

UPDATE AverageGPA SET GPA = 2.5;

™ Set everybody’s GPA to 2.5?

™ Adjust everybody’s GPA by the same amount?

™ Just lower Bart’s GPA?

21

SQL92 updateable views

™ Single-table SFW

ƒ No aggregation ƒ No subqueries

™ Overly restrictive

™ Still might get it wrong in some cases

ƒ See the slide titled “An impossible case”

22

Indexes

™ 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

Examples of using indexes

™ 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

  • Sorting could help ƒ With an index on Enroll. SID : for each Student row, directly look up Enroll rows with matching SID

24

Creating and dropping indexes in SQL

™ CREATE [UNIQUE] INDEX index_name ON

table_name ( column_name 1 , …, column_name n );

ƒ With UNIQUE, the DBMS will also enforce that { column_name 1 , …, column_namen } is a key of table_name

™ DROP INDEX index_name ;

™ Typically, the DBMS will automatically create

indexes for PRIMARY KEY and UNIQUE constraint

declarations

Choosing indexes to create

More indexes = better performance?

™ Indexes take space

™ Indexes have one more level of indirection

™ Indexes need to be maintained when data is updated

) Optimal index selection depends on both query and

update workload and the size of tables

ƒ Automatic index selection is still an area of active research

Summary of SQL features covered so far

™ Query

™ Modification

™ Constraints

™ Triggers

™ Views

™ Indexes

) Next: transactions