Review Three things managed by a DBMS 1.Data organization  E/R Model  Relational Model 2.Data Retrieval  Relational Algebra  SQL 3.Data Integrity.

Slides:



Advertisements
Similar presentations
1 Constraints, Triggers and Active Databases Chapter 9.
Advertisements

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.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
IC and Triggers in SQL. Find age of the youngest sailor with age
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
CMSC424: Database Design Instructor: Amol Deshpande
Integrity Constraints
CMSC424: Database Design Instructor: Amol Deshpande
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
1 Schema Refinement and Normal Forms Chapter 19 Raghu Ramakrishnan and J. Gehrke (second text book) In Course Pick-up box tomorrow.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 8.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
CMSC424: Database Design Instructor: Amol Deshpande
CMSC424: Database Design Instructor: Amol Deshpande
Cs3431 Triggers vs Constraints Section 7.5. cs3431 Triggers (Make DB Active) Trigger: A procedure that starts automatically if specified changes occur.
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.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
The Relational Model These slides are based on the slides of your text book.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Functional Dependencies An example: loan-info= Observe: tuples with the same value for lno will always have the same value for amt We write: lno  amt.
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.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
1 SQL: Constraints and Triggers Chapter 5,
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5,
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
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.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
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 
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Assertions and Triggers in SQL
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity Constraints Domain Constraints Referential Integrity Assertions Triggers.
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.
Chapter 6: Integrity (and Security)
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Integrity Constraints
SQL: Structured Query Language (‘Sequel’)
The Relational Model Relational Data Model
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
SQL – Constraints & Triggers
Presentation transcript:

Review Three things managed by a DBMS 1.Data organization  E/R Model  Relational Model 2.Data Retrieval  Relational Algebra  SQL 3.Data Integrity  Integrity Constraints

Integrity Constraints Purpose: prevent semantic inconsistencies in data e.g.: 4 kinds of IC’s: 1. Key Constraints 2. Attribute Constraints 3. Referential Integrity Constraints 4. Global Constraints e.g.: No entry for Kenmore... ???

IC’s What are they?  predicates on the database  must always be true (:, checked whenever db gets updated) There are the following 4 types of IC’s: Key constraints (1 table) e.g., 2 accts can’t share the same acct_no Attribute constraints (1 table) e.g., 2 accts must have nonnegative balance Referential Integrity constraints ( 2 tables) E.g. bnames associated w/ loans must be names of real branches Global Constraints (n tables) E.g., a loan must be carried by at least 1 customer with a svngs acct

Key Constraints Idea: specifies that a relation is a set, not a bag SQL examples: 1. Primary Key: CREATE TABLE branch( bname CHAR(15) PRIMARY KEY, bcity CHAR(20), assets INT); or CREATE TABLE depositor( cname CHAR(15), acct_no CHAR(5), PRIMARY KEY(cname, acct_no)); 2. Candidate Keys: CREATE TABLE customer ( ssn CHAR(9) PRIMARY KEY, cname CHAR(15), address CHAR(30), city CHAR(10), UNIQUE (cname, address, city);

Key Constraints Effect of SQL Key declarations PRIMARY (A1, A2,.., An) or UNIQUE (A1, A2,..., An) Insertions: check if any tuple has same values for A1, A2,.., An as any inserted tuple. If found, reject insertion Updates to any of A1, A2,..., An: treat as insertion of entire tuple Primary vs Unique (candidate) 1.1 primary key per table, several unique keys allowed. 2.Only primary key can be referenced by “foreign key” (ref integrity) 3.DBMS may treat primary key differently (e.g.: create an index on PK)

Attribute Constraints Idea:  Attach constraints to values of attributes  Enhances types system (e.g.: >= 0 rather than integer) In SQL: 1. NOT NULL e.g.: CREATE TABLE branch( bname CHAR(15) NOT NULL,.... ) Note: declaring bname as primary key also prevents null values 2. CHECK e.g.: CREATE TABLE depositor(.... balance int NOT NULL, CHECK( balance >= 0),.... ) affect insertions, update in affected columns

Attribute Constraints Domains: can associate constraints with DOMAINS rather than attributes e.g: instead of: CREATE TABLE depositor(.... balance INT NOT NULL, CHECK (balance >= 0) ) One can write: CREATE DOMAIN bank-balance INT ( CONSTRAINT not-overdrawn CHECK (value >= 0), CONSTRAINT not-null-value CHECK( value NOT NULL)); CREATE TABLE depositor (..... balance bank-balance, ) Advantages?

Attribute Constraints Advantage of associating constraints with domains: 1. can avoid repeating specification of same constraint for multiple columns 2. can name constraints e.g.: CREATE DOMAIN bank-balance INT ( CONSTRAINT not-overdrawn CHECK (value >= 0), CONSTRAINT not-null-value CHECK( value NOT NULL)); allows one to: 1. add or remove: ALTER DOMAIN bank-balance ADD CONSTRAINT capped CHECK( value <= 10000) 2. report better errors (know which constraint violated)

Referential Integrity Constraints Idea: prevent “dangling tuples” (e.g.: a loan with a bname, Kenmore, when no Kenmore tuple in branch) Referencing Relation (e.g. loan) Referenced Relation (e.g. branch) “foreign key” bname primary key bname Ref Integrity: ensure that: foreign key value  primary key value (note: need not to ensure , i.e., not all branches have to have loans)

Referential Integrity Constraints Referencing Relation (e.g. loan) Referenced Relation (e.g. branch) bname x x x In SQL: CREATE TABLE branch( bname CHAR(15) PRIMARY KEY....) CREATE TABLE loan ( FOREIGN KEY bname REFERENCES branch); Affects: 1) Insertions, updates of referencing relation 2) Deletions, updates of referenced relation

Referential Integrity Constraints c c x x x A B what happens when we try to delete this tuple? titi tjtj Ans: 3 possibilities 1) reject deletion/ update 2) set t i [c], t j [c] = NULL 3) propagate deletion/update DELETE: delete ti, tj UPDATE: set ti[c], tj[c] to updated values

Referential Integrity Constraints c c x x x A B what happens when we try to delete this tuple? titi tjtj CREATE TABLE A (..... FOREIGN KEY c REFERENCES B action ) Action: 1) left blank (deletion/update rejected) (or NO ACTION) 2) ON DELETE SET NULL/ ON UPDATE SET NULL sets ti[c] = NULL, tj[c] = NULL 3) ON DELETE CASCADE deletes ti, tj ON UPDATE CASCADE sets ti[c], tj[c] to new key values

Global Constraints Idea: two kinds 1) single relation (constraints spans multiple columns) E.g.: CHECK (total = svngs + check) declared in the CREATE TABLE 2) multiple relations: CREATE ASSERTION SQL examples: 1) single relation: All Bkln branches must have assets > 5M CREATE TABLE branch ( bcity CHAR(15), assets INT, CHECK (NOT(bcity = ‘Bkln’) OR assets > 5M)) Affects: insertions into branch updates of bcity or assets in branch

Global Constraints SQL example: 2) Multiple relations: every loan has a borrower with a savings account CHECK (NOT EXISTS ( SELECT * FROM loan AS L WHERE NOT EXISTS( SELECT * FROM borrower B, depositor D, account A WHERE B.cname = D.cname AND D.acct_no = A.acct_no AND L.lno = B.lno))) Problem: Where to put this constraint? At depositor? Loan?.... Ans: None of the above: CREATE ASSERTION loan-constraint CHECK(..... ) Checked with EVERY DB update! very expensive.....

Global Constraints Issues: 1) How does one decide what global constraint to impose? 2) How does one minimize the cost of checking the global constraints? Ans: Functional dependencies. but before we go there

Summary: Integrity Constraints Constraint TypeWhere declaredAffects...Expense Key Constraints CREATE TABLE (PRIMARY KEY, UNIQUE) Insertions, UpdatesModerate Attribute Constraints CREATE TABLE CREATE DOMAIN (Not NULL, CHECK) Insertions, UpdatesCheap Referential Integrity Table Tag (FOREIGN KEY.... REFERENCES....) 1.Insertions into referencing rel’n 2. Updates of referencing rel’n of relevant attrs 3. Deletions from referenced rel’n 4. Update of referenced rel’n 1,2: like key constraints. Another reason to index/sort on the primary keys 3,4: depends on a. update/delete policy chosen b. existence of indexes on foreign key Global Constraints Table Tag (CHECK) or outside table (CREATE ASSERTION) 1. For single rel’n constraint, with insertion, deletion of relevant attrs 2. For assesrtions w/ every db modification 1. cheap 2. very expensive

Triggers (Active database)  Trigger: A procedure that starts automatically if specified changes occur to the DBMS  Analog to a "daemon" that monitors a database for certain events to occur  Three parts:  Event (activates the trigger)  Condition (tests whether the triggers should run) [Optional]  Action (what happens if the trigger runs)  Semantics:  When event occurs, and condition is satisfied, the action is performed. 18

Triggers – Event,Condition,Action  Events could be : BEFORE|AFTER INSERT|UPDATE|DELETE ON e.g.: BEFORE INSERT ON Professor  Condition is SQL expression or even an SQL query (query with non-empty result means TRUE)  Action can be many different choices :  SQL statements, and even DDL and transaction-oriented statements like “commit”. 19

Example Trigger Assume our DB has a relation schema : Professor (pNum, pName, salary) We want to write a trigger that : Ensures that any new professor inserted has salary >=

Example Trigger CREATE TRIGGER minSalary BEFORE INSERT ON Professor for what context ? BEGIN check for violation here ? END; 21

Example Trigger CREATE TRIGGER minSalary BEFORE INSERT ON Professor FOR EACH ROW BEGIN Violation of Minimum Professor Salary? END; 22

Example Trigger CREATE TRIGGER minSalary BEFORE INSERT ON Professor FOR EACH ROW BEGIN IF (:new.salary < 60000) THEN RAISE_APPLICATION_ERROR (-20004, ‘Violation of Minimum Professor Salary’); END IF; END; 23

Details of Trigger Example  BEFORE INSERT ON Professor  This trigger is checked before the tuple is inserted  FOR EACH ROW  specifies that trigger is performed for each row inserted  :new  refers to the new tuple inserted  If (:new.salary < 60000)  then an application error is raised and hence the row is not inserted; otherwise the row is inserted.  Use error code: ;  this is in the valid range 24

Example Trigger Using Condition CREATE TRIGGER minSalary BEFORE INSERT ON Professor FOR EACH ROW WHEN (new.salary < 60000) BEGIN RAISE_APPLICATION_ERROR (-20004, ‘Violation of Minimum Professor Salary’); END;  Conditions can refer to old/new values of tuples modified by the statement activating the trigger. 25

Triggers: REFERENCING CREATE TRIGGER minSalary BEFORE INSERT ON Professor REFERENCING NEW as newTuple FOR EACH ROW WHEN (newTuple.salary < 60000) BEGIN RAISE_APPLICATION_ERROR (-20004, ‘Violation of Minimum Professor Salary’); END; 26

Example Trigger CREATE TRIGGER updSalary BEFORE UPDATE ON Professor REFERENCING OLD AS oldTuple NEW as newTuple FOR EACH ROW WHEN (newTuple.salary < oldTuple.salary) BEGIN RAISE_APPLICATION_ERROR (-20004, ‘Salary Decreasing !!’); END;  Ensure that salary does not decrease 27

Another Trigger Example (SQL:99) CREATE TRIGGER youngSailorUpdate AFTER INSERT ON SAILORS REFERENCING NEW TABLE AS NewSailors FOR EACH STATEMENT INSERT INTO YoungSailors(sid, name, age, rating) SELECT sid, name, age, rating FROM NewSailors N WHERE N.age <= 18 28

Row vs Statement Level Trigger  Row level: activated once per modified tuple  Statement level: activate once per SQL statement  Row level triggers can access new data, statement level triggers cannot always do that (depends on DBMS).  Statement level triggers will be more efficient if we do not need to make row-specific decisions 29

Row vs Statement Level Trigger  Example: Consider a relation schema Account (num, amount) where we will allow creation of new accounts only during normal business hours. 30

Example: Statement level trigger CREATE TRIGGER MYTRIG1 BEFORE INSERT ON Account FOR EACH STATEMENT --- is default BEGIN IF (TO_CHAR(SYSDATE,’dy’) IN (‘sat’,’sun’)) OR (TO_CHAR(SYSDATE,’hh24:mi’) NOT BETWEEN ’08:00’ AND ’17:00’) THEN RAISE_APPLICATION_ERROR(-20500,’Cannot create new account now !!’); END IF; END; 31

When to use BEFORE/AFTER  Based on efficiency considerations or semantics.  Suppose we perform statement-level after insert, then all the rows are inserted first, then if the condition fails, and all the inserted rows must be “rolled back”  Not very efficient !! 32

Combining multiple events into one trigger CREATE TRIGGER salaryRestrictions AFTER INSERT OR UPDATE ON Professor FOR EACH ROW BEGIN IF (INSERTING AND :new.salary < 60000) THEN RAISE_APPLICATION_ERROR (-20004, 'below min salary'); END IF; IF (UPDATING AND :new.salary < :old.salary) THEN RAISE_APPLICATION_ERROR (-20004, ‘Salary Decreasing !!'); END IF; END; 33

Summary : Trigger Syntax CREATE TRIGGER BEFORE|AFTER INSERT|DELETE|UPDATE [OF ] ON | [REFERENCING [OLD AS ] [NEW AS ]] [FOR EACH ROW] (default is “FOR EACH STATEMENT”) [WHEN ( )] ; 34

Constraints versus Triggers  Constraints are useful for database consistency  Use IC when sufficient  More opportunity for optimization  Not restricted into insert/delete/update  Triggers are flexible and powerful  Alerters  Event logging for auditing  Security enforcement  Analysis of table accesses (statistics)  Workflow and business intelligence …  But can be hard to understand ……  Several triggers (Arbitrary order  unpredictable !?)  Chain triggers (When to stop ?)  Recursive triggers (Termination?) 35

Functional Dependencies An example: loan-info= Observe: tuples with the same value for lno will always have the same value for amt We write: lno  amt (lno “determines” amt, or amt is functional determined by lno) True or false? amt  lno? lno  cname? lno  bname? bname  lno? can’t always decide by looking at populated db’s

Functional Dependencies In general: A1 A2.... An  B set of attributes single attribute Formally: if 2 tuples “agree” on their values for A1, A2,...,An they will also agree on their values for B Formally:  t, u: (t[A1] = u[A1]  t[A2] = u[A2] ....  t[An] = u[An] ) => t[B] = u[B]

Back to Global IC’s How do we decide what constraints to impose? Consider loan-info(bname, lno, cname, amt) with FDs: lno  bname How do we ensure that lno  bname? CREATE ASSERTION lno-bname CHECK ( NOT EXIST (SELECT * FROM loan-info l1, loan-info l2 WHERE ?)) ? == l1.lno = l2.lno AND l1.bname <> l2.bname FD’s tell us what global constraints to impose....

Back to FDs How to derive them? (1) Key constraints ( e.g.: bname a key for branch bname  bname bname  bcity bname  assets) we can write: bname  bname bcity assets Q: Define “superkeys” in terms of FD’s: A: Any set of attributes in a relation that functionally determines all attributes in the relation Q: Define “candidate key” in terms of FD’s: A: Any superkey such that the removal of any attribute leaves a set that does not functionally determine all attributes.

Functional Dependencies How to derive them? (1) Key constraints (2) Laws of physics.... e.g.: time room  course (3) Trial-and-error... Given R=(A, B,C) try each of the following to see if they make sense: A  B AB  C A  C AC  B B  A BC  A B  C C  A C  B What about: AB  A ? B  B ? Just say:...plus all of the trivial dependencies

Back to Global IC’s (2) Avoiding the expense Recall: lno  bname preserved by: CREATE ASSERTION lno-bname CHECK ( NOT EXIST (SELECT * FROM loan-info l1, loan-info l2 WHERE l1.lno = l2.lno AND l1.bname <> l2.bname)) Is it necessary to have an assertion for every FD’s? Ans: Luckily, no. Can preprocess FD set some FD’s can be eliminated some FD’s can be combined

Functional Dependencies Combining FD’s: a. cname  ccity CREATE ASSERTION name-city CHECK ( NOT EXIST (SELECT * FROM customer c1, customer c2 WHERE c1.cname = c2.cname AND c1.ccity <> c2.ccity)) b. cname  cstreet CREATE ASSERTION name-street CHECK ( NOT EXIST (SELECT * FROM customer c1, customer c2 WHERE c1.cname = c2.cname AND c1.cstreet <> c2.cstreet)) combine into: cname  ccity cstreet CREATE ASSERTION name-city-street CHECK ( NOT EXIST (SELECT * FROM customer c1, customer c2 WHERE c1.cname = c2.cname AND ??)) ?? = ((c1.ccity <> c2.ccity) OR (c1.cstreet <> c2.cstreet))

Functional Dependencies Determining unnecessary FD’s: Consider cname  cname CREATE ASSERTION name-name CHECK ( NOT EXIST (SELECT * FROM customer c1, customer c2 WHERE c1.cname = c2.cname AND c1.cname <> c2.cname)) cannot possibly be violated! Note: X  Y s.t. Y X a “trivial dependency” (true, regradless of attributes involved) So: Don’t create assertions for trivial dependencies

Functional Dependencies Determining unnecessary FD’s: even non-trivial FD’s can be unnecessary e.g. a. lno  bname CREATE ASSERTION lno-bname CHECK ( NOT EXIST (SELECT * FROM loan-info l1, loan-info l2 WHERE l1.lno = l2.lno AND l1.bname <> l2.bname)) b. bname  assets CREATE ASSERTION lno-bname CHECK ( NOT EXIST (SELECT * FROM loan-info l1, loan-info l2 WHERE l1.bname = l2.bname AND l1.assets <> l2.assets))

Cont. c. lno  assets CREATE ASSERTION lno-bname CHECK ( NOT EXIST (SELECT * FROM loan-info l1, loan-info l2 WHERE l1.lno = l2.lno AND l1.assets <> l2.assets)) But if (a) and (b) succeed, then c must also