Download presentation
Presentation is loading. Please wait.
Published byErin Parks Modified over 9 years ago
1
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index
2
ICS184Notes 102 Assertions So far most constraints are within one table Assertions: constraints over a table as a whole or multiple tables. CREATE ASSERTION CHECK 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.
3
ICS184Notes 103 Example Dept(dept#, mgr), Emp(name, salary), “Each manager must make more than $50000.” CREATE ASSERTION RichMGR CHECK (NOT EXISTS (SELECT * FROM dept, emp WHERE 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.
4
ICS184Notes 104 Example Dept(dept#, mgr), Emp(name, salary), “Each manager must make more than $50000.” CREATE ASSERTION RichMGR CHECK (NOT EXISTS (SELECT * FROM dept, emp WHERE 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) Dept(dno, dname, mgr)
5
ICS184Notes 105 Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute insert/update not if contains CHECK on the attribute subquery Tuple relation schema insert/update not if contains CHECK to the relation subquery Assertion database schema any change on always any relation mentioned
6
ICS184Notes 106 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;
7
ICS184Notes 107 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!
8
ICS184Notes 108 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)
9
ICS184Notes 109 Row-Level Triggers emp(name, sal,…) “No update can reduce employees’ salaries.” CREATE TRIGGER NoLowerSalary AFTER UPDATE OF sal ON emp REFERENCING OLD AS OldTuple NEW AS NewTuple WHEN (OldTuple.sal > NewTuple.sal) UPDATE Emp SET sal= OldTuple.sal WHERE name = NewTuple.name FOR EACH ROW;
10
ICS184Notes 1010 Statement-Level Triggers emp(dno…), dept(dept#, …) “Whenever we insert employees tuples, make sure that their dno’s exist in Dept.” CREATE TRIGGER deptExistTrig AFTER INSERT ON emp REFERENCING OLD_TABLE AS OldStuff NEW_TABLE AS NewStuff WHEN (EXISTS (SELECT * FROM NewStuff WHERE dno NOT IN (SELECT dept# FROM dept))) DELETE FROM emp WHERE dno NOT IN (SELECT dept# FROM dept));
11
ICS184Notes 1011 Index A data structure for efficient retrieval of tables Implementation could be different –E.g. Hash table Create an index: –Syntax: CREATE INDEX ON ( ); –Example: CREATE INDEX sal_index ON emp(sal); Drop an index –Syntax:DROP INDEX ; –Example: DROP INDEX sal_index;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.