11
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

Embed Size (px)

Citation preview

Page 1: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

1

ICS 184: Introduction to Data Management

Lecture Note 11: Assertions, Triggers, and Index

Page 2: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 2

Assertions• So far most constraints are within one table

• Assertions: constraints over a table as a whole or multiple tables.

CREATE ASSERTION <name> CHECK <cond>• An assertion must always be true at transaction

boundaries. – Any modification that causes it to become false is rejected.

• Similar to tables, assertions can be dropped by a DROP command.

Page 3: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 3

Example Dept(dept#, mgr), Emp(name, salary),

• “Each manager must make more than $50000.”

CREATE ASSERTION RichMGR CHECK(NOT EXISTS

(SELECT *FROM dept, empWHERE emp.name = dept.mgr AND emp.sal <

50000));• If someone inserts a manager whose salary is less than 50K, the

insertion/update to dept table will be rejected.

• Furthermore, if a manager’s salary reduced to less than 50K, the corresponding update to emp table will also be rejected.

Page 4: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 4

Example Dept(dept#, mgr), Emp(name, salary),

• “Each manager must make more than $50000.” CREATE ASSERTION RichMGR CHECK(NOT EXISTS

(SELECT *FROM dept, empWHERE emp.name = dept.mgr AND emp.sal <

50000));• When checked? Insert/updates on the two tables. E.g., insertion (‘tom’,

40000) to Emp will be rejected.Emp (ename, dno, sal)

eName Dno Sal Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Dept(dno, dname, mgr)dno dname Mgr 111 Sells Alice 222 Toys Lisa 333 Electronics Mary

Page 5: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 5

Different Constraint TypesType Where Declared When activated Guaranteed to hold?

Attribute with attribute insert/update not if containsCHECK on the attribute subquery

Tuple relation schema insert/update not if containsCHECK to the relation subquery

Assertion database schema any change on always any relation mentioned

Page 6: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 6

Giving Names to Constraints

• Goal: to be able to alter constraints.• Add the keyword CONSTRAINT and then a name: CREATE TABLE Emp ( name CHAR(30), ssn int CONSTRAINT ssnIsKey PRIMARY KEY, age int CONSTRAINT rightage CHECK (age > 18 AND age < 100) );

• Change constraints• ALTER TABLE emp

ADD CONSTRAINT ssnPositive CHECK (ssno > 0);• ALTER TABLE emp DROP CONSTRAINT ssnIsKey;• DROP ASSERTION assert1;

Page 7: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 7

Triggers• Motivation: enable database programmers 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.

– Thus, also called “ECA”

• NOTE: triggers may cause cascading effects.

• Triggers not part of SQL2 but included in SQL3. DB vendors did not wait for standards with triggers!

Page 8: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 8

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

– once for every tuple (row-level), or– once for all the tuples that are changed by the database

operation (statement level)

Page 9: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 9

Row-Level Triggers

emp(name, sal,…)“No update can reduce employees’ salaries.”

CREATE TRIGGER NoLowerSalaryAFTER UPDATE OF sal ON empREFERENCING OLD AS OldTuple NEW AS NewTupleWHEN (OldTuple.sal > NewTuple.sal) UPDATE Emp SET sal= OldTuple.sal WHERE name = NewTuple.nameFOR EACH ROW;

Page 10: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 10

Statement-Level Triggersemp(dno…), dept(dept#, …)

“Whenever we insert employees tuples, make sure that their dno’s exist in Dept.”CREATE TRIGGER deptExistTrigAFTER INSERT ON empREFERENCING OLD_TABLE AS OldStuff NEW_TABLE AS NewStuffWHEN (EXISTS (SELECT * FROM NewStuff

WHERE dno NOT IN (SELECT dept# FROM dept)))

DELETE FROM emp WHERE dno NOT IN (SELECT dept# FROM dept));

Page 11: 1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index

ICS184 Notes 10 11

Index• A data structure for efficient retrieval of tables• Implementation could be different

– E.g. Hash table

• Create an index:– Syntax:

CREATE INDEX <index-name> ON <table-name>(<col1, col2,…>);

– Example:CREATE INDEX sal_index ON emp(sal);

• Drop an index– Syntax: DROP INDEX <index-name>;– Example: DROP INDEX sal_index;