SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections 7.1-7.3, 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.

Slides:



Advertisements
Similar presentations
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
Advertisements

Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Fall 2001Arthur Keller – CS 18010–1 Schedule Oct. 25 (TH) More Constraints, Triggers. u Read Sections Oct. 30 (T) Embedded SQL. u Read Section.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
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.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 PL/SQL Oracle’s Version of Triggers and PSM. 2 PL/SQL uOracle uses a variant of SQL/PSM which it calls PL/SQL. uPL/SQL not only allows you to create.
Triggers.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
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 IT 244 Database Management System Lecture 11 More SQL Constraints &Triggers, SQL Authorization,Transactions Foreign Keys, Local and Global Constraints,
Onsdag The concepts in a relation data model SQL DDL DML.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Databases 1 9th lecture. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
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.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
© D. Wong Java API for XML Processing (JAXP)  For processing XML data using applications written in Java  JAXP APIs - javax.xml.parsers package.
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.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Introduction to PL/SQL. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
Database Management COP4540, SCS, FIU Database Trigger.
1 Constraints, Views, Indexes Foreign Keys Constraints Triggers Virtual and Materialized Views Speeding Accesses to Data.
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.
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.
Slides are reused by the approval of Jeffrey Ullman’s
CMSC-461 Database Management Systems
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Database Design: DBS CB, 2nd Edition
CPSC-608 Database Systems
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
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
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Presentation transcript:

SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC. u Read Sections 7.5, , 8.5. After that u Transaction concepts. u Read Sections 8.6.

SCUHolliday - coen 1789–2 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages we learned earlier. SQL programming is used to describe constraints. Constraint types 1.Primary key declarations (already covered). 2.Foreign-keys = referential integrity constraints. 3.Attribute- and tuple-based checks = constraints within relations. 4.SQL Assertions = global constraints. u Not found in Oracle. 5.Oracle Triggers. - A substitute for assertions.

SCUHolliday - coen 1789–3 Referential Integrity Referential integrity ensures that a value that appears in one table for a given attribute also appears for the same or compatible attribute in another table. For example: If "Oakland" is a branch name in one of the rows in the Account table, we would like to ensure that there is a row in the Branch table for "Oakland".

SCUHolliday - coen 1789–4 Foreign Keys A foreign key is an attribute (column) in one table whose values must match the primary key of another table. In relation R a clause that “attribute A references S( B )” says that whatever values appear in the A column of R must also appear in the B column of relation S. B must be declared the primary key for S. R S AZ 35… 37… BX … 35…

SCUHolliday - coen 1789–5 Foreign Keys Foreign key constraints arise naturally from relationship sets When this is translated to tables, there will be three tables. One for Entity 1 with primary key K1, and one for Entity 2 with primary key K2, and one for relationship R with primary key K1K2. In the table for R, K1 is a foreign key referencing the table for Entity 1. Also K2 in R is a foreign key referencing Entity 2.

SCUHolliday - coen 1789–6 Example CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) REFERENCES Beers(name), price REAL );

SCUHolliday - coen 1789–7 Alternative: add another element declaring the foreign key, as: CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY beer REFERENCES Beers(name) ); Extra element essential if the foreign key is more than one attribute.

SCUHolliday - coen 1789–8 Violating a Foreign Key Constraint 1.Insert or update a Sells tuple so it refers to a nonexistent beer. u Always rejected. 2.Delete or update a Beers tuple that has a beer value some Sells tuples refer to. a) Default: reject. b) Cascade: Ripple changes to referring Sells tuple. Example  Delete “Bud” in Beers. Cascade deletes all Sells tuples that mention Bud.  Update “Bud” to “Budweiser.” Change all Sells tuples with “Bud” in beer column to be “Budweiser.”

SCUHolliday - coen 1789–9 Continued c) Set Null: Change referring tuples to have NULL in referring components. Example  Delete “Bud” in Beer. Set-null makes all Sells tuples with “Bud” in the beer component have NULL there. u Update “Bud” to “Budweiser.” Same change.

SCUHolliday - coen 1789–10 Selecting a Policy Add ON [DELETE, UPDATE] [CASCADE, SET NULL] to declaration of foreign key. Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY beer REFERENCES Beers(name) ON DELETE SET NULL ON UPDATE CASCADE ); “Correct” policy is a design decision. u E.g., what does it mean if a beer goes away? What if a beer changes its name?

SCUHolliday - coen 1789–11 Example create table Branch (branch-namechar(15) not null, branch-citychar(30), assetsinteger, primary key (branch-name) ) create table Account (branch-namechar(15) account# char(10) not null, balance integer, primary key (account#), foreign key (branch-name) references Branch) What does the system check: when delete a row in Account? Branch?

SCUHolliday - coen 1789–12 Attribute-Based Checks Follow an attribute by a condition that must hold for that attribute in each tuple of its relation. Form: CHECK ( condition ). u Condition may involve the checked attribute. u Other attributes and relations may be involved, but only in subqueries. u Oracle: No subqueries allowed in condition. Condition is checked only when the associated attribute changes (i.e., an insert or update occurs).

SCUHolliday - coen 1789–13 Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) CHECK(beer IN (SELECT name FROM Beers) ), price REAL CHECK( price <= 5.00 ) ); Check on beer is like a foreign-key constraint, except:  The check occurs only when we add a tuple or change the beer in an existing tuple, not when we delete a tuple from Beers.

SCUHolliday - coen 1789–14 Tuple-Based Checks Separate element of table declaration. Form: like attribute-based check. But condition can refer to any attribute of the relation. u Or to other relations/attributes in subqueries. u Again: Oracle forbids the use of subqueries. Checked whenever a tuple is inserted or updated.

SCUHolliday - coen 1789–15 Example Only Joe's Bar can sell beer for more than $5. CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, CHECK(bar = 'Joe''s Bar' OR price <= 5.00) );

SCUHolliday - coen 1789–16 SQL Assertions Database-schema constraint. Not present in Oracle. Checked whenever a mentioned relation changes (can be costly). Syntax: CREATE ASSERTION CHECK( );

SCUHolliday - coen 1789–17 Example No bar may charge an average of more than $5 for beer. Sells(bar, beer, price) CREATE ASSERTION NoRipoffBars CHECK(NOT EXISTS( SELECT bar FROM Sells GROUP BY bar HAVING 5.0 < AVG(price) ) ); Checked whenever Sells changes.

SCUHolliday - coen 1789–18 Example There cannot be more bars than drinkers. Bars(name, addr, license) Drinkers(name, addr, phone) CREATE ASSERTION FewBar CHECK( (SELECT COUNT(*) FROM Bars) <= (SELECT COUNT(*) FROM Drinkers) ); Checked whenever Bars or Drinkers changes.

SCUHolliday - coen 1789–19 Triggers (Oracle Version) Often called event-condition-action rules. Event = a class of changes in the DB, e.g., “insertions into Beers.” Condition = a test as in a where-clause for whether or not the trigger applies. Action = one or more SQL statements. Differ from checks or SQL assertions in that: 1. Triggers invoked by the event; the system doesn’t have to figure out when a trigger could be violated. 2. Can use conditions not available in checks. 3. Can take action – assertions just stop.

SCUHolliday - coen 1789–20 Example Whenever we insert a new tuple into Sells, make sure the beer mentioned is also mentioned in Beers, and insert it (with a null manufacturer) if not. Sells(bar, beer, price) CREATE OR REPLACE TRIGGER BeerTrig AFTER INSERT ON Sells FOR EACH ROW WHEN(new.beer NOT IN (SELECT name FROM Beers)) BEGIN INSERT INTO Beers(name) VALUES(:new.beer); END;

SCUHolliday - coen 1789–21 Trigger Options 1. Can omit OR REPLACE. But if you do, it is an error if a trigger of this name exists. 2. AFTER can be BEFORE. 3. If the relation is a view, AFTER can be INSTEAD OF. u Useful for allowing “modifications” to a view; you modify the underlying relations instead. 4. INSERT can be DELETE or UPDATE OF.  FOR EACH ROW can be omitted, with an important effect: the action is done once for the relation(s) consisting of all changes.

SCUHolliday - coen 1789–22 Triggers - Notes There are two special variables new and old, representing the new and old tuple in the change.  old makes no sense in an insert, and new makes no sense in a delete. Notice: in WHEN we use new and old without a colon, but in actions, a preceding colon is needed. The action is a PL/SQL statement.  Simplest form: surround one or more SQL statements with BEGIN and END. u However, select-from-where has a limited form.

SCUHolliday - coen 1789–23 Triggers – more notes Oracle triggers are part of the database schema, like tables or views. Important Oracle constraint: the action cannot change the relation that triggers the action. u Worse, the action cannot even change a relation connected to the triggering relation by a constraint, e.g., a foreign-key constraint.

SCUHolliday - coen 1789–24 Example Maintain a list of all the bars that raise their price for some beer by more than $1. Sells(bar, beer, price) RipoffBars(bar) CREATE TRIGGER PriceTrig AFTER UPDATE OF price ON Sells FOR EACH ROW WHEN(new.price > old.price ) BEGIN INSERT INTO RipoffBars VALUES(:new.bar); END;.