IST 210 Constraints and Triggers. IST 210 2 Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.

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.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
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.
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.
1 Constraints Foreign Keys Local and Global Constraints Triggers Source: slides by Jeffrey Ullman.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
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,
1 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Database Systems More SQL Database Design -- More SQL1.
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.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
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.
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)
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
1 Database Systems Defining Database Schema Views.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
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.
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 
Assertions and Triggers in SQL
Database Management COP4540, SCS, FIU Database Trigger.
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.
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.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CPSC-310 Database Systems
SQL: Constraints and Triggers
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
CMSC-461 Database Management Systems
CPSC-608 Database Systems
SQL – Constraints & Triggers
CPSC-608 Database Systems
Assertions and Triggers
Presentation transcript:

IST 210 Constraints and Triggers

IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints Example: key constraints Triggers: Actions that are executed when a specified condition occurs Easier to implement than many constraint Example: insert a tuple

IST Constraints Keys Foreign-key (referential integrity) Value-based constraints Tuple-based constraints Assertions (SQL boolean expression)

IST Foreign Keys Requires that values for certain attributes must appear in other relations. Example: Dog(name, breed, Owner) the the value for the owner of the dog must appear in the Owner relation

IST Foreign keys Keyword REFERENCES either Within the declaration of an attribute when only one attribute involved OR As an element of the schema Example: FOREIGN KEY ( ) REFERENCES ( ); Referenced attributes must be declared as PRIMARY KEY

IST Example Dog(name,breed,owner) CREATE TABLE Owner ( name CHAR (20) PRIMARY KEY, SSN INT, phone CHAR (20)); CREATE TABLE Dog ( name CHAR(20) PRIMARY KEY, breed CHAR(10), owner CHAR(20) REFERENCES Owner(name) );

IST Example Dog(name,breed,owner) CREATE TABLE Owner ( name CHAR (20) PRIMARY KEY, SSN INT, phone CHAR (20)); CREATE TABLE Dog ( name CHAR(20) PRIMARY KEY, breed CHAR(10), owner CHAR(20), FOREIGN KEY (owner) REFERENCES Owner(name) );

IST Enforcing Foreign-Key Constraints If there is a foreign-key constraints form attributes A 1, …, A n of a relation R to the primary-key B 1, …, B m of an other relation S, then possible violations: Insert or update on R may introduce values not in S Deletion or update on S may remove values needed for tuples in R Tuples of R without a matching primary-key in S are called dangling tuples

IST Actions to prevent Foreign-Key Violations Insertions or updates that would create a dangling tuple must be REJECTED Example: If a new dog is inserted into the Dog relation before the owner’s data is inserted into the Owner relation.

IST Actions for Deleting/Modifying Tuples Needed for Foreign-Key Three possible ways to handle: 1.Default: REJECT the modification 2.Cascade: make the same changes in the referencing relation 3.Set NULL: change the referencing attribute to NULL

IST Example: Cascade Suppose owner ‘Alexandra Smith’ is deleted from Owner relation Delete all tuples from Dog where the owner attribute value is ‘Alexandra Smith’ Suppose Alexandra Smith wants to update her name to ‘Alexandra Ray’ Change the owner attribute values of all tuples in Dog from ‘Alexandra Smith’ to ‘Alexandra Ray’

IST Example: Set NULL Suppose owner ‘Alexandra Smith’ is deleted from Owner relation Change all tuples from Dog where the owner attribute value is ‘Alexandra Smith’ to owner=NULL Suppose Alexandra Smith wants to update her name to ‘Alexandra Ray’ (same as before) Change the owner attribute values of all tuples in Dog from ‘Alexandra Smith’ to ‘Alexandra Ray’

IST Choosing a Policy When declaring a foreign key, policy can be set independently for deletions and updates If not declared then default is used Example: CREATE TABLE Dog ( name CHAR(20) PRIMARY KEY, breed CHAR(10), owner CHAR(20), FOREIGN KEY (owner) REFERECES Owner(name) ON DELETE SET NULL);

IST Attribute-Based Constraints Constraint the value of a particular attribute CHECK( ) is added to the declaration of the attribute Condition may use the name of the attribute or any other relation or attribute name may be in a sub-query

IST Example CREATE TABLE Dog ( name CHAR(20) PRIMARY KEY, breed CHAR(10), owner CHAR(20) CHECK (owner IN (SELECT name FROM Owner)), weight REAL CHECK (0 <weight AND weight < 120) );

IST Timing of Checks Attribute value check is checked only when the value of the attribute is inserted or updated Example: CHECK (0 < weight AND weight < 120) is verified every time a new weight value is inserted/updated the Dog database CHECK (owner IN (SELECT name FROM Owner)) is not checked when an owner is deleted from Owner – NOT LIKE FOREIGN KEY

IST Tuple-Based Checks Check ( ) may be added during schema definition Condition may refer to any attribute of the relation but other relations and their attributes require sub-queries Checked during insert or update

IST Example CREATE TABLE Dog ( name CHAR(20) PRIMARY KEY, breed CHAR(10), owner CHAR(20), weight REAL, CHECK (owner = ‘Alexandra Smith’ OR breed = ‘G.S.’));

IST Assertions Holds on database-schema elements like relations and views Must always be true Condition may refer to any relation or attribute in the database schema CREATE ASSERTION CHECK ( );

IST Example In Dog relation(name, breed, weight, owner) tiny dogs selected CREATE ASSERTION TinyDogs CHECK NOT EXISTS ( SELECT name FROM Dog WHERE weight < 12 );

IST Example In Owners and Dogs cannot be more owners than dogs. CREATE ASSERTION Few-owners CHECK ( (SELECT COUNT (*) FROM Owner) <= (SELECT COUNT (*) FROM Dog) );

IST Timing Assertion In general, check every assertion after every modification to any relation of the database Clever system: only certain changes can cause a given assertion to be violated  check only after these changes

IST Triggers Attribute and tuple-based checks  limited in capabilities Assertions: general and powerful but difficult to implement efficiently Triggers: Allows the user to specify when the check occurs. General purpose conditions and sequence of SQL database modifications

IST Triggers Also called event-condition-action (ECA) rules Event: typically a type of database modification Condition: and SQL boolean-valued expression Action: any SQL statement

IST Example Instead of using foreign-key constraints to reject an insertion of a dog into Dog if the owner is not present in Owner, use trigger to insert the same owner into Owner with NULL for phone and SSN. CREATE TRIGGER OWNR AFTER INSERT ON Dog REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.owner NOT IN (SELECT name FROM Owner)) INSERT INTO Owner(name) VALUES(NewTuple.owner);

IST Options: Create Trigger CREATE TRIGGER Option: CREATE OR REPLACE TRIGGER useful to modify existing trigger CREATE TRIGGER OWNR AFTER INSERT ON Dog REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.owner NOT IN (SELECT name FROM Owner)) INSERT INTO Owner(name) VALUES(NewTuple.owner);

IST Options: Condition AFTER can be BEFORE or INSTEAD OF for views (can be used to execute view modifications and translate them to modifications on the base relations INSERT and be DELETE or UPDATE or UPDATE … ON a particular attribute CREATE TRIGGER OWNR AFTER INSERT ON Dog REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.owner NOT IN (SELECT name FROM Owner)) INSERT INTO Owner(name) VALUES(NewTuple.owner);

IST Options: For Each Row Triggers: Row-level Statement-level FOR EACH ROW indicates row-level, its absence indicates statement-level Row-level triggers executed once for each modified tuple Statement-level triggers executed once for an SQL statement, regardless of the number of modified tuples CREATE TRIGGER OWNR AFTER INSERT ON Dog REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.owner NOT IN (SELECT name FROM Owner)) INSERT INTO Owner(name) VALUES(NewTuple.owner);

IST Options: Referencing INSERT statement implies a new tuple (row-level) or a new set of tuples (statement-level) DELETE implies and old tuple or table UPDATE implies both Format of reference: [NEW OLD] [TUPLE TABLE] AS CREATE TRIGGER OWNR AFTER INSERT ON Dog REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.owner NOT IN (SELECT name FROM Owner)) INSERT INTO Owner(name) VALUES(NewTuple.owner);

IST Options: Condition Any boolean-valued condition is appropriate Evaluated before or after the triggering event, depending on whether BEFORE or AFTER was used Access the new/old tuples or set of tuples through names declared in the REFERENCING clause CREATE TRIGGER OWNR AFTER INSERT ON Dog REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.owner NOT IN (SELECT name FROM Owner)) INSERT INTO Owner(name) VALUES(NewTuple.owner);

IST Options: Action More than one SQL statements are allowed in action Surround be BEGIN … END if there is more than one Action: modification CREATE TRIGGER OWNR AFTER INSERT ON Dog REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.owner NOT IN (SELECT name FROM Owner)) INSERT INTO Owner(name) VALUES(NewTuple.owner);

IST Triggers on Views Generally, it is impossible to modify a view because it does not exist INSTEAD OF trigger lets us interpret view modifications

IST Example Owner(name,phone,address) Owns(O.name,D.name,D.breed) Dog(name,age,weight,breed) Create a view with the owner’s name and phone, and the dog’s name and weight CREATE VIEW dog-and-owner AS (SELECT o.name, o.phone,d.name, weight FROM Owner o, Owns, Dog d WHERE o.name = O.name and d.name = D.name )

IST Example CREATE TRIGGER View-Update INSTEAD OF INSERT ON dog-and-owner REFERENCING NEW ROW AS n FOR EACH ROW BEGIN INSERT INTO Dog(name,weight) VALUES(n.d.name, weight); INSERT INTO OWNER(name,phone) VALUES(n.o.name, n.o.phone); INSERT INTO OWNS(O.name,D.name) VALUES(n.o.name, n.o.phone); END;