Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5, 5.7-5.8.

Slides:



Advertisements
Similar presentations
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Database Management System Module 3:. Complex Constraints In this we specify complex integrity constraints included in SQL. It relates to integrity constraints.
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
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL Constraints and Triggers
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
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 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
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.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
The Relational Model These slides are based on the slides of your text book.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
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 SQL: Constraints and Triggers Chapter 5,
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
SQL Integrity Constraints. 421B: Database Systems - Integrity Constraints 2 Integrity Constraints (Review) q An IC describes conditions that every legal.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
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
Database Management COP4540, SCS, FIU Database Trigger.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Advanced SQL: Aggregation and Null Values.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
SQL – Part 2.
SQL: Structured Query Language (‘Sequel’)
SQL: Queries, Programming, Triggers
The Relational Model Relational Data Model
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
SQL: Structured Query Language
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
Presentation transcript:

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5,

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke2 Integrity Constraints (Review)  An IC describes conditions that every legal instance of a relation must satisfy.  Inserts/deletes/updates that violate IC’s are disallowed.  Can be used to ensure application semantics (e.g., sid is a key), or prevent inconsistencies (e.g., sname has to be a string, age must be < 200)  Types of IC’s : Domain constraints, primary key constraints, foreign key constraints, general constraints.  Domain constraints : Field values must be of right type. Always enforced.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke3 General Constraints: CHECK  Syntax: CHECK conditional-expression.  The conditional expression captures more general ICs than keys.  The conditional expressions can use queries.  The conditional expressions required to hold only if the associated table is nonempty.  A CHECK constraint may be expressed over several tables; however, it is often expressed over one single table.  Constraints can be named:  CONSTRAINT MyConstraint CHECK conditional-expression

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke4 CHECK Constraints: Examples Constraint: Rating must be in the range 1 to 10 CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 ))

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke5 CHECK Constraints: Examples CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT noInterlakeRes CHECK (`Interlake’ <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid))) Constraint: Interlake boats cannot be reserved

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke6 General Constraints: ASSERTION CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 )  This solution is awkward and wrong:  It is wrongfully associated only with Sailors, though it involves both Sailors and Boats.  If Sailors is empty, the number of Boats tuples can be anything, since the conditional expression is not required to hold in such case! Constraint: Number of boats plus number of sailors is < 100

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke7 General Constraints: ASSERTION  The assertion is not associated with any one of the tables involved.  ASSERTION is the right solution; not associated with either table. CREATE ASSERTION smallClub CHECK ( ( SELECT COUNT (S.sid) FROM Sailors S) + ( SELECT COUNT (B.bid) FROM Boats B) < 100 ) Constraint: Number of boats plus number of sailors is < 100

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke8 General Constraints: Further Examples  Write SQL integrity constraints (domain, key, foreign key, or CHECK constraints; or assertions) for given requirements:  Students must have a minimum cgpa of 5.  Every TA must also be a student.  The total percentage of all assignments to projects for a given student must be at most 100%.  A TA must have a cgpa higher than any student that she coaches. Schema: Students(sid: int, sname: string, age: int, cgpa: real) Works(sid: int, pid: int, pct_time: int) Projects(did: int, budget: real, ta: int)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke9 General Constraints: Further Examples (1) Constraint: Students must have a minimum cgpa of 5. CREATE TABLE Students ( sid INTEGER, sname CHAR(10), age REAL, cgpa REAL, PRIMARY KEY (sid), CHECK ( cgpa >= 5)) Constraint: Every TA must also be a student. CREATE ASSERTION TAisStudent CHECK ( ( SELECT COUNT (*) FROM Projects P WHERE P.ta_id NOT IN (SELECT sid FROM Students)) =0 )

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke10 General Constraints: Further Examples (2) Constraint: The total percentage of all assignments to projects for a student must be at most 100%. CREATE TABLE Works ( sid INTEGER, pid INTEGER, pct_time INTEGER, PRIMARY KEY (sid,pid), FOREIGN KEY (sid) REFERENCES Students, FOREIGN KEY (pid) REFERENCES Projects, CHECK ( (SELECT COUNT (W.stid) FROM Works W GROUP BY W.stid HAVING SUM(pct_time) > 100) = 0)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke11 General Constraints: Further Examples (3) CREATE ASSERTION TAHigherCGPA CHECK (( SELECT COUNT(S.stid) FROM Students S, Students TA, Works W, Projects P WHERE S.sid=W.sid AND W.pid=P.pid AND P.ta=TA.sid AND S.cgpa > TA.cgpa) =0) Constraint: A TA must have a cgpa higher than any student that she coaches.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke12 Triggers  Trigger: procedure that starts automatically if specified changes occur to the DBMS  Three parts:  Event (activates the trigger)  Condition (tests whether the triggers should run)  Action (what happens if the trigger runs)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke13 Triggers  BEFORE trigger: action executed before the activating event occurs.  AFTER trigger: action executed after the activating event occurs.  INSTEAD trigger: action executed instead of the activating event.  Row-level trigger: executed once per modified row (that satisfies the trigger condition).  Statement-level trigger: executed once per modifying statement.  Transition variables: NEW, OLD, NEW TABLE, OLD TABLE.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke14 Triggers: Example (SQL:92) CREATE TRIGGER incr_count AFTER INSERT ON Sailors WHEN (new.age < 18) FOR EACH ROW BEGIN count:=count+1; END  Illustrates use of NEW to refer to newly inserted tuples  Exists since SQL:92 Increment a count for each newly inserted sailor whose age < 18.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke15 Triggers: Example (SQL:1999) CREATE TRIGGER youngSailorsUpdate AFTER INSERT ON Sailors REFERENCING NEW TABLE AS NewSailors FOR EACH STATEMENT /* This is the default */ INSERT INTO YoungSailors(sid, name, age, rating) SELECT sid, name, age, rating FROM NewSailors N WHERE N.age <= 18 Save newly inserted sailors aged < 18 in a special table.  Illustrates use of NEW TABLE to refer to a set of newly inserted tuples  Exists since SQL:1999

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke16 Triggers: More Elaborated Example CREATE TRIGGER bumpTAcgpa AFTER UPDATE ON Students WHEN OLD.cgpa < NEW.cgpa FOR EACH ROW BEGIN UPDATE Students S SET S.cgpa = NEW.cgpa WHERE S.cgpa < NEW.cgpa AND S.sid IN (SELECT P.ta FROM STudents S1, Works W, Projects P WHERE S1.sid = NEW.sid AND S1.sid = W.sid AND W.sid = P.sid); END Whenever a student is given a (bonus) raise on his cgpa, the TA’s cgpa must be increased to be at least as high.  Illustrates use of Oracle PL/SQL syntax in the action part.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke17 Summary  SQL allows specification of rich integrity constraints  Triggers respond to changes in the database