Download presentation
Presentation is loading. Please wait.
Published byAlberta Neal Modified over 9 years ago
1
7 1 Constraints & Triggers Chapter 7
2
7 2 Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary key constraints: no duplicates, no nulls Triggers: actions executed only when a specified condition occurs, e.g. when a tuple is inserted, or when the value of an attribute exceeds a specified value.
3
7 3 Types of constraints Primary key Foreign-keys = referential integrity constraints. Value-based constraints: constraints on values of a particular attribute Tuple-based constraints = constraints within relations. SQL Assertions = global constraints.
4
7 4 Keys: Fundamental Constraint In the CREATE TABLE statement, use: –PRIMARY KEYor UNIQUE keywords CREATE TABLE titles ( title_id CHAR(8) PRIMARY KEY, title VARCHAR(30), pub_id CHAR(7), …. ); Or, list at end of CREATE TABLE e.g. PRIMARY KEY (title_id) PRIMARY KEY (title_id, au_id)
5
7 5 Composite Primary Key CREATE TABLE titleauthor ( title_id CHAR(8), au_idCHAR(8), au_ord INT, royaltyshare REAL, PRIMARY KEY (title_id, au_id) );
6
7 6 Keys: Fundamental Constraint Primary key Vs Unique attributes: –No duplicates are allowed in both –A table may have more than one UNIQUE declaration but only one PRIMARY KEY –NULL not allowed in primary key but allowed in unique keys –Foreign key only references Primary key
7
7 7 Referential Integrity Constraints Two rules for Foreign Keys: –publishers(pub_id, pub_name, ….) –titles(title_id, title, type, pub_id, …..) 1)Foreign Key Must be a PRIMARY KEY in the referenced table. 2) Foreign Key must be a reference to a valid value in the referenced table: e.g. the pub_id in titles must be a valid id number of a publisher in the publishers table.
8
7 8 Declaring FK Constraints Use REFERENCES keyword... CREATE TABLE titles ( title_id CHAR(8) PRIMARY KEY, title CHAR(30), pub_id CHAR(7) REFERENCES publishers(pub_id) ); Or, summarize at end of CREATE TABLE FOREIGN KEY pub_id REFERENCES publishers(pub_id)
9
7 9 Enforcing Referential Integrity If there is a referential integrity constraint from a set of attributes A of relation R (FK) to the primary key attributes of relation S, then two violations are possible: 1.An insert or update to relation R introduces new values of the foreign key attributes that are not in S. e.g. new pub_id that is not in publishers 2.A delete or update in S causes foreign key values in R to “dangle” By default, SQL will reject any modification that violates referential integrity constraints
10
7 10 Enforcing Referential Integrity Recall, titles has FK pub_id... publishers(pub_id, pub_name,….) (1212, Prentice Hall,…..) titles(title_id, title, pub_id, ….) (111111, database systems, 1212)
11
7 11 Enforcing Referential Integrity Policies for handling the change… –Reject the update (default) –Cascade (update/deletes) –Set NULL E.g. Update pub_id of Prentice Hall in publishers publishers(pub_id, pub_name,….) (5555, Prentice Hall) Cascade Update: tiltes(title_id, title, pub_id, ….) (111111, database systems, 5555,…) Set NULL Update: (111111, database systems, NULL)
12
7 12 Enforcing Referential Integrity Suppose we delete Prentice Hall from publishers publishers(pub_id, pub_name,….) (1212, Prentice Hall) deleted Cascade on delete: delete every tuple with pub_id = 1212 from titles Set NULL on delete: set every occurrence pub_id = 1212 to NULL e.g. (111111, database systems, NULL)
13
7 13 Choosing Policy Can set update and delete actions independently. Follow foreign-key declaration with ON [DELETE|UPDATE] [CASCADE| SET NULL] If no policy is specified, default (reject) used Example: CREATE TABLE titles ( tile_id CHAR(7) PRIMARY KEY, title VARCHAR(20) not null, pub_id CHAR(7) REFERENCES publishers(pub_id) ON DELETE SET NULL ON UPDATE CASCADE);
14
7 14 Constraining Attribute Values Put a constrain on the values of an attribute –NOT NULL Add the following to attribute declaration: –CHECK ( ) –Condition: Simple Include a sub-query
15
7 15 Examples CREATE TABLE authors ( au_id CHAR(9), au_name VARCHAR(20), gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)), age int CHECK (age > 0 and age <150), Specialty VARCHAR(30) CHECK (Specialty IN (SELECT Specialty_name FROM Specialties)) ); e.g. Specialties: (Specialty_name, Description)
16
7 16 Timing of Checks Attribute-based constraints is checked whenever any tuple gets a new value for this attribute. On insertion and updates: –E.g. if insert a new author with age < 0, insertion is rejected Not on deletions (unlike foreign key checks) –E.g. delete from Specialties table a specialty_name that is used in authors table. Operation NOT REJECTED
17
7 17 Constraining Values with User Defined ‘Types’ Can define new domains to use as the attribute type... CREATE DOMAIN GenderDomain CHAR(1) CHECK (VALUE IN (‘F’, ‘M’)); Then use new domain as a data type for attribute definition... gender GenerDomain
18
7 18 Tuple-based checks Involves several attributes in one table R E.g.: Price of books should not be more than $100 except for CS books: CREATE TABLE titles ( title_id CHAR(8) PRIMARY KEY, title VARCHAR(30), pub_id CHAR(7), REFERENCES publishers(pub_id), price real, type VARCHAR(20), CHECK (type = ‘CS’ OR price <= 100.00) ); Checked every time a tuple of R is updated or a tuple is inserted into R.
19
7 19 Naming Constraints CREATE TABLE authors ( au_id CHAR(9), au_name VARCHAR(20), gender CHAR(1) CONSTRAINT GenderConstraint CHECK (gender IN (‘F’, ‘M’)), age int CONSTRAINT AgeConstraint CHECK (age > 0 and age <150), Specialty VARCHAR(30) CONSTRAINT SpecialtyConstraint CHECK (Specialty IN (SELECT Specialty_name FROM Specialties)) );
20
7 20 Dropping and Adding constriaints ALTER TABLE authors DROP CONSTRAINT Ageconstraint ALTER TABLE authors ADD CONSTRAINT Ageconstraint CHECK (age >= 0 and age <=150);
21
7 21 Assertions These are global constraints that involve multiple relations Assertions are schema-based elements, like tables and views They are defined by: CREATE ASSERTION CHECK ( ) ; Condition may refer to any relation or attribute in the database schema
22
7 22 Example There should not be more publishers than books CREATE ASSERTION too_many_publishers CHECK ( (Select count(*) From publishers) < (Select count(*) From titles) );
23
7 23 Timing of Assertions Check In principle, DBMS must check every assertion after each modification to any relation to the database. Optimization
24
7 24 Triggers: Motivation Attribute and tuple based checks have limited capabilities: –check when a condition is violated –Reject operation that caused the violation Assertions are hard/expensive to implement: DBMS supposed to check for violations every time the database is modified.
25
7 25 Triggers A trigger allows the user to specify when the checks occur Like an assertion, a trigger has a general purpose condition and can perform a sequence of SQL modifications on the database.
26
7 26 Triggers Enable the database programmer to specify: when to check a constraint, what exactly to do. A trigger has 3 parts: An event (e.g., update an attribute) A condition (e.g., a query to check) An action (deletion, update, insertion) When the event happens, the system will check the condition, and if satisfied, will perform the action. NOTE: triggers may cause cascading effects.
27
7 27 Triggers CNTND Timing of action execution: before, after or instead of the triggering event Triggering event is a change to database: insert/update/delete Update events may be limited to a particular attribute or set of attributes. The action can refer to both the old and new state of the database. A condition is specified with a WHEN clause. The action can be performed either once for every tuple, or once for all the tuples that are changed by the database operation.
28
7 28 Example: Row Level Trigger CREATE OR REPLACE TRIGGER NoPriceGauging AFTER UPDATE OF price ON titles REFERENCING OLD ROW AS OldTuple NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.price > 1.1*OldTuple.price) UPDATE titles SET price = 1.1*OldTuple.price WHERE titles.title_id = NewTuple.title_id;
29
7 29 OPTIONS: Create trigger CREATE TRIGGER CREATE OR REPLACE TRIGGER useful if there is a trigger with same name and you want to modify its definition
30
7 30 OPTIONS: Trigger Event AFTER can be BEFORE Also can be INSTEAD OF –A good way to execute view modification UPDATE/INSERT/DELETE [OF attribute-name] ON table-name –UPDATE can be on a particular attribute –INSERT and DELETE must be on tuples
31
7 31 OPTIONS: For Each Row Triggers are either row-level or statement level FOR EACH ROW indicates row-level. Default is statement-level Row-level triggers are execute once for each modified row Statement-level triggers are execute once for each SQL statement
32
7 32 Options: REFERENCING REFERENCING OLD ROW AS OldTuple/ Old Table AS Oldstuff NEW NEW AS NewTuple / New Table AS Newstuff When triggering event is an update, there will be old and new tuples for row-level trigger. There will be new and old set of tuples for statement-level trigger For insert trigger event: new tuple/table For delete trigger event: old tuple/table Refer to these by [NEW|OLD] [ROW|TABLE] AS
33
7 33 Statement Level Trigger CREATE TRIGGER average-price-preserve AFTER UPDATE OF price ON titles REFERENCING OLD TABLE AS OldStuff // old version of changed tuples NEW TABLE AS NewStuff // new version WHEN (50 < (SELECT AVG (price) FROM titles) ) BEGIN DELETE FROM titles WHERE (title_id, title, price, type) NOT IN OldStuff; INSERT INTO titles (SELECT * FROM OldStuff WHERE (title_id, title, price, type) NOT IN NewStuff ); END;
34
7 34 OPTIONS: When condition WHEN (NewTuple.price > 1.1*OldTuple.price) Any Boolean-valued condition Evaluated before or after the triggering event depending on whether BEFORE or AFTER is used in the event Can access the names of old and new tuples/tables declared in REFERENCING
35
7 35 OPTIONS: Actions Can one or more SQL statements. If more than one surround with BEGIN and END Modification statements
36
7 36 INSTEAD OF Create view for CS titles Create view CsTitles As Select (title_id, title, price) From titles Where type = ‘CS’;
37
7 37 INSTEAD OF Insert new CS title through the CsTitles books Create trigger CsTitlesInsert Instead of Insert on CsTitles Referencing New Row as NT For Each Row Insert Into titles(title_id, title, type, price) Values (NT.title_id, NT.title, ‘CS’, NT.price);
38
7 38 Statement Level Trigger CREATE TRIGGER average-price-preserve INSTEAD OF UPDATE OF price ON titles REFERENCING OLD TABLE AS OldStuff // old version of changed tuples NEW TABLE AS NewStuff // new version WHEN (50 < (SELECT AVG (price) FROM ((titles EXCEPT OldStuff) UNION NewStuff)) BEGIN DELETE FROM titles WHERE (title_id, title, price, type) IN OldStuff; INSERT INTO titles (SELECT * FROM NewStuff); END;
39
7 39 Bad Things Can Happen CREATE TRIGGER Bad-trigger AFTER UPDATE OF price ON titles REFERENCING OLD ROW AS OldTuple NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.price > 50) UPDATE titles SET price = NewTuple.price * 2 WHERE title_id = NewTuple.title_id
40
7 40 Exercise Create a trigger that updates the ytd_sales in tiles table whenever a book is sold.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.