IC and Triggers in SQL. Find age of the youngest sailor with age <=18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) AS minage.

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

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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
Database Management System Module 3:. Complex Constraints In this we specify complex integrity constraints included in SQL. It relates to integrity constraints.
Active DatabasesCIS 6711 Triggers: The Problem - Examples from COMPANY Database 1.Limit all salary increases to 50%. 2.Enforce policy that salaries may.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
Jennifer Widom Constraints & Triggers Triggers – Introduction.
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 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.
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 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
Database Systems More SQL Database Design -- More SQL1.
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 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
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.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
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,
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.
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.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
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.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
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 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Elaborated By : REPUBLIQUE TUNISIENNE MINISTERE DE L’ENSEIGNEMENT SUPERIEUR University Year :
Copyright © 2004 Pearson Education, Inc.. Chapter 24 Enhanced Data Models for Advanced Applications.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
SQL – Part 2.
Creating Database Triggers
Constraints and Triggers
SQL: Structured Query Language (‘Sequel’)
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
Introduction to Triggers
SQL: Structured Query Language
Presentation transcript:

IC and Triggers in SQL

Find age of the youngest sailor with age <=18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation: Sailors instance: HAVING Clause

Integrity Constraints (Review) An IC describes conditions that every legal instance of a relation must satisfy To disallow inserts/deletes/updates that violate IC’s Types of IC’s: Domain constraints, primary key constraints, foreign key constraints, non-null, general constraints

An Example via CHECK Clause CREATE TABLE Stu ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK (rating >= 1 AND rating <= 10 ) )

CHECK Example CREATE TABLE Stu ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CONSTRAINT checkRating CHECK (rating >= 1 AND rating <= 10 ) )

ASSERTION Example Constraints Over Multiple Relations Consider a very small school: the count of students and professors should be less than 500 The following is a poor integrity test as it is associated with one relation (the Stu table could be empty and thus the integrity rule is never checked! Disassociate from the Stu table CREATE TABLE Stu ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Stu S) + (SELECT COUNT (P.pid) FROM Prof P) < 500 ) )

ASSERTION Example Constraints Over Multiple Relations CREATE ASSERTION smallSchool CHECK ( (SELECT COUNT (S.sid) FROM Stu S) + (SELECT COUNT (P.pid) FROM Prof P) < 500 )

ASSERTION Example The KEY Constraint CREATE ASSERTION Key CHECK ( (SELECT COUNT (DISTINCT sid) FROM Stu) = (SELECT COUNT (*) FROM Stu) ); Note: ASSERTION is in standard SQL but not implemented

Triggers Event-Condition-Action rules: procedures that start automatically if a change occurs to the DBMS When event occurs, check condition; if true do action Why: to move monitoring logic from applications into the DBMS (more modular, consistency among applications, no duplications) Allow automatic repair Database stores triggers just as regular data so they are persistent and accessible to all database operations Useful for security purposes Triggers make a passive database “active”

Advantages To move application logic and business rules into database Allows more functionality for DBAs to establish vital constraints/rules of applications Rules managed in some central “place” Rules automatically enforced by DBMS, no matter which applications later come on line

The Event-Condition-Action Model Actions may apply before or after the triggering event is executed An SQL statement may change several rows – Apply action once per SQL statement – Apply action for each row changed by SQL statement

The Company Database 1.Limit all salary increases to 50%. 2.Enforce policy that salaries may never decrease. 3.Maintain TotalSalary in DEPARTMENT relation as employees and their salaries change. (EN, Fig (a)) 4.Inform a supervisor whenever a supervisee’s salary becomes larger than the supervisor’s. (EN, Fig (b)) 5.All new hires for a given job code get the same starting salary, which is available in the STARTING_PAY table. EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) DEPARTMENT(DNO, TotalSalary, ManagerSSN) STARTING_PAY(JobCode, StartPay)

Limit all salary increases to 50% create trigger emp_salary_limit before update of EMPLOYEE for each row when (new.Salary > 1.5 * old.Salary) set new.Salary = 1.5 * old.Salary; “new” refers to the new tuple. “old” refers to the old tuple.

Enforce policy that salaries may never decrease create trigger emp_salary_no_decrease before update of EMPLOYEE for each row when (new.Salary < old.Salary) begin log the event; signal error condition; end Method depends on DBMS.

All new hires for a given job code get the same starting salary create trigger emp_start_pay before insert on EMPLOYEE for each row set Salary = (select StartPay fromSTARTING_PAY whereJobCode = new.JobCode) EMPLOYEE(Name, SSN, Salary, DNO, SupervisorSSN, JobCode) STARTING_PAY(JobCode, StartPay)

Trigger Syntax CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) BEGIN --- sql statements END;