CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.

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.
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.
IC and Triggers in SQL. Find age of the youngest sailor with age
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
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.
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.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
The Relational Model These slides are based on the slides of your text book.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
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.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
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.
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.
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.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
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.
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
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
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.
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.
SQL – Part 2.
Constraints and Triggers
Aggregation.
SQL: Structured Query Language (‘Sequel’)
SQL: Queries, Constraints, Triggers
SQL: Structured Query Language
SQL – Constraints & Triggers
Assertions and Triggers
Presentation transcript:

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 158 Integrity Constraints An integrity constraint (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 and NOT NULL constraints, primary key constraints and foreign key constraints, general constraints.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 159 Not-Null Constraints The IC NOT NULL disallows NULL values for a specified attribute. CREATE TABLE Students (sid CHAR(20) PRIMARY KEY, name CHAR(20) NOT NULL, login CHAR(10) NOT NULL, age INTEGER, gpa REAL); What about specifying gpa as NOT NULL? Primary key attributes are implicitly NOT NULL.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 160 General Constraints Attribute-based CHECK defined in the declaration of an attribute, activated on insertion to the corresponding table or update of attribute. Tuple-based CHECK defined in the declaration of a table, activated on insertion to the corresponding table or update of tuple. Assertion defined independently from any table, activated on any modification of any table mentioned in the assertion.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 161 General Constraints Can use general SQL queries to express constraints. Much more powerful than domain and key constraints. Constraints can be named. Test of constraints can be deferred until the end of the corresponding transaction of the activating SQL statement.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 162 Attribute-based CHECK Attribute-based CHECK constraint is part of an attribute definition. Is checked whenever a tuple gets a new value for that attribute (INSERT or UPDATE). Violating modifications are rejected. CHECK constraint can contain an SQL query referencing other attributes (of the same or other tables), if their relations are mentioned in the FROM clause. CHECK constraint is not activated if other attributes mentioned get new values.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 163 Attribute-based CHECK Attribute-based CHECK constraints are most often used to restrict allowable attribute values. CREATE TABLE Sailors ( sid INTEGER PRIMARY KEY, sname CHAR(10), rating INTEGER CHECK ( rating >= 1 AND rating <= 10), age REAL);

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 164 Tuple-based CHECK Tuple-based CHECK constraints can be used to constrain multiple attribute values within a table. Condition can be anything that can appear in a WHERE clause. Same activation and enforcement rules as for attribute- based CHECK. CREATE TABLE Sailors ( sid INTEGER PRIMARY KEY, sname CHAR(10), previousRating INTEGER, currentRating INTEGER, age REAL, CHECK (currentRating >= previousRating) );

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 165 Tuple-based CHECK CHECK constraint that refers to other table: CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CHECK (‘Interlake’ <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid))); But: these constraints are invisible to other tables, i.e. are not checked upon modification of other tables. What happens if the name of a boat is updated?

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 166 Assertions Number of boats plus number of sailors is < 100. Tuple-based CHECK awkward and wrong! If Sailors is empty, the number of Boats tuples can be anything! 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 ) );

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 167 Assertions ASSERTION is the right solution; not associated with either table. Condition can be anything allowed in a WHERE clause. Constraint is tested whenever any (!) of the referenced tables is modified. Violating modifications are rejectced. Different from CHECK constraints, ICs expressed as assertion are always enforced (unless they are deferred until the end of the transaction). CHECK constraints are more efficient to implement than ASSERTIONs.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 168 Assertions Number of boats plus number of sailors is < 100. CREATE ASSERTION smallClub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ); Number of reservations per sailor is < 10. CREATE ASSERTION notTooManyReservations CHECK ( 10 > ALL (SELECT COUNT (*) FROM Reserves GROUP BY sid) );

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 169 Triggers Trigger : procedure that starts automatically if specified changes occur to the DB. Three parts of a trigger: Event (activates the trigger) insert, delete or update of the database. Condition (tests whether the trigger should run) a Boolean statement or a query (nonempty answer set = true, empty answer set = false). Action (what happens if the trigger runs) wide varity of options.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 170 Triggers Synchronization of the Trigger with the activating statement (DB modification) Before After Instead of Deferred (at end of transaction). Number of Activations of the Trigger Once per modified tuple (FOR EACH ROW) Once per activating statement (default).

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 171 Triggers CREATE TRIGGER youngSailorUpdate AFTER INSERT ON SAILORS/* Event */ REFERENCING NEW TABLE NewSailors FOR EACH STATEMENT INSERT/* Action */ INTO YoungSailors(sid, name, age, rating) SELECT sid, name, age, rating FROM NewSailors N WHERE N.age <= 18; This trigger inserts young sailors into a separate table. It has no (i.e., an empty, always true) condition.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 172 Triggers Options for the REFERENCING clause: NEW TABLE: the set (!) of tuples newly inserted (INSERT). OLD TABLE: the set (!) of deleted or old versions of tuples (DELETE / UPDATE). OLD ROW: the old version of the tuple (FOR EACH ROW UPDATE). NEW ROW: the new version of the tuple (FOR EACH ROW UPDATE). The action of a trigger can consist of multiple SQL statements, surrounded by BEGIN... END.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 173 Triggers CREATE TRIGGER notTooManyReservations AFTER INSERT ON Reserves/* Event */ REFERENCING NEW ROW NewReservation FOR EACH ROW WHEN (10 <= (SELECT COUNT(*) FROM Reserves WHERE sid =NewReservation.sid))/* Condition */ DELETE FROM Reserves R WHERE R.sid= NewReservation.sid /* Action */ AND day= (SELECT MIN(day) FROM Reserves R2 WHERE R2.sid=R.sid); This trigger makes sure that a sailor has less than 10 reservations, deleting the oldest reservation of a given sailor, if neccesary. It has a non- empty condition (WHEN).

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 174 Triggers vs. General Constraints Triggers can be harder to understand. Several triggers can be activated by one SQL statement ( arbitrary order !). A trigger may activate other triggers ( chain activation ). Triggers are procedural. Assertions react on any database modification, trigger only only specified event. Trigger execution cannot be optimized by DBMS. Triggers have more applications than constraints. monitor integrity constraints, construct a log, gather database statistics, etc.

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 175 Summary SQL allows specification of rich integrity constraints (ICs): attribute-based, tuple-based CHECK and assertions (table-independent). CHECK constraints are activated only by modifications of the table they are based on, ASSERTIONs are activated by any modification that can possibly violate them. Choice of the most appropriate method for a particular IC is up to the DBA. Triggers respond to changes in the database. Can also be used to represent ICs.