7 1 Constraints & Triggers Chapter 7. 7 2 Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

1 Constraints, Triggers and Active Databases Chapter 9.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Database Systems More SQL Database Design -- More SQL1.
Triggers.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
1 Database Systems Defining Database Schema Views.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Deductive Databases General idea: some relations are stored (extensional), others are defined by datalog queries (intensional). Many research projects.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
Database Management COP4540, SCS, FIU Database Trigger.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
1 Introduction to Database Systems, CS420 SQL Constraints.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Introduction to Database Systems, CS420
CPSC-608 Database Systems
SQL: Constraints and Triggers
CPSC-310 Database Systems
CMSC-461 Database Management Systems
SQL – Constraints & Triggers
CPSC-608 Database Systems
Assertions and Triggers
Presentation transcript:

7 1 Constraints & Triggers Chapter 7

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.

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.

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)

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) );

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 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.

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)

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

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)

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)

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)

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);

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

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)

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

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

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 <= ) ); Checked every time a tuple of R is updated or a tuple is inserted into R.

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)) );

7 20 Dropping and Adding constriaints ALTER TABLE authors DROP CONSTRAINT Ageconstraint ALTER TABLE authors ADD CONSTRAINT Ageconstraint CHECK (age >= 0 and age <=150);

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

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) );

7 23 Timing of Assertions Check In principle, DBMS must check every assertion after each modification to any relation to the database. Optimization

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.

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.

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.

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.

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;

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

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

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

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

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;

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

7 35 OPTIONS: Actions Can one or more SQL statements. If more than one surround with BEGIN and END Modification statements

7 36 INSTEAD OF Create view for CS titles Create view CsTitles As Select (title_id, title, price) From titles Where type = ‘CS’;

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);

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;

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

7 40 Exercise Create a trigger that updates the ytd_sales in tiles table whenever a book is sold.