Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture 21 - 4/6/2006.

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.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #13 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
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.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
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.
CMSC424: Database Design Instructor: Amol Deshpande
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,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
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.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
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.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Constraints, Triggers and Index James Wang.
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)
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
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.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Assertions and triggers1. 2 Constraints Attribute-based CHECK constraints create table … ( postcode number(4) check (postcode > 0) ); Checked at update.
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
Assertions and Triggers in SQL
SQL Exercises – Part I April
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.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
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.
Copyright © 2004 Pearson Education, Inc.. Chapter 24 Enhanced Data Models for Advanced Applications.
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.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Introduction to Structured Query Language (SQL)
SQL: Constraints and Triggers
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
CMSC-461 Database Management Systems
SQL – Constraints & Triggers
CPSC-608 Database Systems
-Transactions in SQL -Constraints and Triggers
Assertions and Triggers
Presentation transcript:

Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006

Lecture 21© CIS Spring Check Constraints Limits values that may appear in components for some attributes Expressed as either Constraint on attribute in definition of its relation’s schema Constraint on a tuple as a whole; part of relation schema, but not associated with individual attributes

Lecture 21© CIS Spring Attribute Check Constraint Simple Case: Assume in table Studio we have declaration: presC# INT REFERENCES MovieExec(cert#) NOT NULL Cannot use set-null policy to fix referential int. violation General Case: Attached to attribute declaration Keyword CHECK followed by any condition that could follow WHERE clause in SQL query Checked whenever any tuple gets a new value for this attribute (incl. on inserts of new tuples) Not checked when modification does not change the value of the attribute to which CHECK belongs

Lecture 21© CIS Spring Example CREATE TABLE MovieStar ( … gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)), … ); Condition being checked can be anything that could follow WHERE in SFW query

Lecture 21© CIS Spring Example Can the following attribute-based CHECK constraint simulate a referential integrity constraint? CREATE TABLE Studio ( Name CHAR(30) PRIMARY KEY, Address VARCHAR(255), presC# INT CHECK (presC# IN (SELECT cert# FROM MovieExec)); No, updates to MovieExec are invisible to the above CHECK constraint

Lecture 21© CIS Spring Tuple-Based Check Constraint Add a tuple-based CHECK constraint to MovieStar schema that prevents the insertion of male stars whose name begin with “Ms.” Checked after insertions and updates to tuples of the relation on which it is defined CREATE TABLE MovieStar ( Name CHAR(30) PRIMARY KEY, Address VARCHAR(255), Gender CHAR(1), Birthdate DATE, CHECK (gender <> ‘M’ OR name NOT LIKE ‘Ms.%’) //forbid the insertion of tuples that satisfy multiple conditions, namely “male and name starts with ‘Ms.’ ” //equivalent to the OR of the negation of the same terms

Lecture 21© CIS Spring Assertions More powerful mechanism of constraining values in database are part of database schema First-class database citizens like views or relations Assertion is a boolean-valued SQL expression that must be true at all times Easy to state for DB implementer, simply state what must be true Harder to implement efficiently since DBMS must deduce whether or not a given database modification could affect truth of assertion

Lecture 21© CIS Spring Example Express that no one can become president of a studio unless net worth greater than $10M MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#) CREATE ASSERTION RichPres CHECK (NOT EXISTS (SELECT * FROM Studio, MovieExec WHERE presC# = cert# AND netWorth < ) ) ); Can this be simulated with tuple-based CHECK constraints?

Lecture 21© CIS Spring Using a Tuple-Based CHECK CREATE TABLE Studio ( name CHAR(30) PRIMARY KEY, address VARCHAR(255), presC# INT REFERENCES MovieExec(cert#), CHECK (presc# NOT IN (SELECT cert# FROM MovieExec WHERE netWorth < ) ) );

Lecture 21© CIS Spring Example Assert that the total length of all movies by a given studio shall not exceed 10,000 minutes Movie(title,year,length,inColor,studioName,producerC#) CREATE ASSERTION SumLength CHECK (10000 >= ALL (SELECT SUM(length)FROM Movie GROUP BY StudioName)); Is the effect the same as that of the following tuple- based CHECK: CHECK (10000 >= ALL (SELECT SUM(length) FROM Movie GROUP BY studioName));

Lecture 21© CIS Spring Summary of CHECK Constraints and Assertions Type of Constraint When Declared When Activated Guaranteed to Hold Attribute-based CHECK With attribute On insertion to relation or attribute update Not if subqueries Tuple-based CHECK Element of relational schema On insertion to relation or tuple update Not if subqueries Assertion Element of Database schema On any change to any mentioned relation Yes

Lecture 21© CIS Spring Triggers Aka “event-condition-action” (ECA) rules Three important facts about triggers Only awakened when certain events, specified by db programmer, occur Executing triggers involves testing a condition first If condition satisfied, action of trigger is executed

Lecture 21© CIS Spring Example Write trigger to prevent any attempt to lower networth of movie exec MovieExec(name,address,cert#,netWorth) CREATE TRIGGER NetWorthTrigger AFTER UPDATE OF netWorth ON MovieExec REFERENCING OLD ROW AS OldTuple, NEW ROW AS NewTuple, FOR EACH ROW WHEN (OldTuple.netWorth > NewTuple.netWorth) UPDATE MovieExec SET netWorth = OldTuple.netWorth WHERE cert# = NewTuple.cert#; Event Condition Action

Lecture 21© CIS Spring Comments The action rule may be executed BEFORE or AFTER the event If before, when clause is tested before triggering event Besides update, other triggering events are insert and delete When clause is optional The action may contain any number of SQL statements, separated by BEGIN … END If triggering event is insert, may use a NEW ROW AS clause to give name to inserted row Conversely, may use OLD ROW AS in case of a deletion

Lecture 21© CIS Spring More Comments If we omit FOR EACH ROW clause, trigger becomes statement-level trigger (as opposed to row-level trigger) Statement-level trigger is executed ONCE no matter how many rows it actually effects Cannot refer to old and new tuples However, both types of triggers can access old and new set of tuples OLD TABLE AS … (i.e., deleted tuples or old versions of updated tuples) NEW TABLE AS … (i.e., inserted tuples or new versions of updated tuples)

Lecture 21© CIS Spring Example Prevent average net worth of movie executives to drop below $500K Violation on insert, update, delete => need three triggers! MovieExec(name,address,cert#,netWorth) CREATE TRIGGER AvgNetWorthTrigger AFTER UPDATE OF netWorth ON MovieExec REFERENCING OLD TABLE AS OldStuff, NEW TABLE AS NewStuff FOR EACH STATEMENT WHEN ( > (SELECT AVG(netWorth) FROM MovieExec)) BEGIN DELETE FROM MovieExec WHERE (name,address,cert#,netWorth) IN NewStuff; INSERT INTO MovieExec (SELECT * FROM OldStuff); END;