CMSC-461 Database Management Systems

Slides:



Advertisements
Similar presentations
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
Advertisements

Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
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 18010–1 Schedule Today: Feb. 5 (T) u Triggers, PL/SQL. u Read Sections 7.4, 8.2. Assignment 4 due. Feb. 7 (TH) u PL/SQL,
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.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
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.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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, Cushing10–1 Modification to Views Via Triggers Oracle allows us to “intercept” a modification to a view through an instead-of.
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 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
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.
1 Database Systems Defining Database Schema Views.
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.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
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.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Database Management COP4540, SCS, FIU Database Trigger.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 Introduction to Database Systems, CS420 SQL Constraints.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Lecture 11 PL/SQL Slides from
CPSC-310 Database Systems
Introduction to Database Systems, CS420
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CS 440 Database Management Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
CPSC-310 Database Systems
CPSC-310 Database Systems
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
CMSC-461 Database Management Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
Modification to Views Via Triggers
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

CMSC-461 Database Management Systems SQL3 Triggers CMSC-461 Database Management Systems

SQL3 Triggers Some differences, including: Position of FOR EACH ROW. You can execute the action INSTEAD OF as well as before or after the event that triggered the action. The Oracle restriction about not modifying the relation of the trigger or other relations linked to it by constraints is not present in SQL3 (but Oracle is real; SQL3 is paper). 4. The action in SQL3 is a list of SQL3 statements, not a PL/SQL statement.

SQL2 Assertions Database-schema constraint. Not present in Oracle 7.3.2. Checked whenever a mentioned relation changes. Syntax: CREATE ASSERTION <name> CHECK(<condition>);

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.

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.

Challenge Suppose we have our usual relations Beers(name, manf) Sells(bar, beer, price) and we want to maintain the foreign-key constraint that if you sell a beer, its name must appear in Beers.

Challenge (II) Question: if we don't have foreign-key declarations available, how could we arrange for this constraint to be maintained: Using attribute-based constraints? Using SQL2 assertions? using Oracle triggers?

Challenge (III) Question: What if we also want to make sure that each beer mentioned in Beers is sold at at least one bar?

Another Constraints Problem Recall our design of a DB for cities, counties and states, especially how we decided that we needed a relationship cities-states to support the "weakness" of cities. A DB schema might have: States(name, pop, capital) Cities(name, pop, state) Counties(name, pop, state) CinC(cityName, cityState, countyName, countyState)

Another Constraints Problem (II) There is a funny form of redundancy, because we expect to be able to find the state of a city by going to any county for that city and finding the state of that county.

Another Constraints Problem (III) Question: as written, is it possible for a city to be in counties that are in two different states? How would you enforce the requirement that that not happen? Question: if we are willing to enforce the constraint, can you simplify the database schema? Should you?

PL/SQL Found only in the Oracle SQL processor (sqlplus). A compromise between completely procedural programming and SQL's very high-level, but limited statements.

PL/SQL (II) Allows local variables, loops, procedures, examination of relations one tuple at a time.

PL/SQL (III) Rough form: DECLARE declarations BEGIN executable statements END; . run;

PL/SQL (IV) DECLARE portion is optional. Dot and run are needed to end the statement and execute it.

Simplest Form: Sequence of Modifications Likes(drinker, beer) BEGIN INSERT INTO Likes VALUES('Sally', 'Bud'); DELETE FROM Likes WHERE drinker = 'Fred' AND beer = 'Miller'; END; . run;

Procedures Stored database objects that use a PL/SQL statement in their body.

Procedures (II) Form of declaration: CREATE OR REPLACE PROCEDURE <name>(<arglist>) AS declarations BEGIN <PL/SQL statements> END; . run;

Procedures (III) Argument list has name-mode-type triples. Mode: IN, OUT, or INOUT, as in ODL. Types: standard SQL + generic types like NUMBER = any integer or real type.

Procedures (IV) Since types in procedures must match their types in the DB schema, you should generally use an expression of the form relation.attribute%TYPE to capture the type correctly.

Example A procedure to take a beer and price and add it to Joe's menu. Sells(bar, beer, price)

Example (II) CREATE PROCEDURE joeMenu( b IN Sells.beer%TYPE, p IN Sells.price%TYPE ) AS BEGIN INSERT INTO Sells VALUES('Joe''s Bar', b, p); END; . run;

Example (III) Note "run" only stores the procedure; it doesn't execute the procedure.

Invoking Procedures A procedure call may appear in the body of a PL/SQL statement. Example: BEGIN joeMenu('Bud', 2.50); joeMenu('MooseDrool', 5.00); END; . run;

Assignment Assign expressions to declared variables with :=.

Branches IF <condition> THEN <statement(s)> ELSE <statement(s)> END IF; But in nests, use ELSIF in place of ELSE IF.

Loops LOOP . . . EXIT WHEN <condition> . . . END LOOP;

Cursors Declare by: CURSOR <name> IS select-from-where statement

Cursors (II) Cursor gets each tuple from the relation produced by the select-from-where, in turn, using a fetch statement in a loop. Fetch statement: FETCH <cursor name> INTO variable list;

Cursors (III) Break the loop by a statement of the form: EXIT WHEN <cursor name>%NOTFOUND; True when there are no more tuples to get. Open and close the cursor with OPEN and CLOSE.

Example A procedure that examines the menu for Joe's Bar and raises by $1.00 all prices that are less than $3.00. Sells(bar, beer, price)

Example (II) This simple price-change algorithm can be implemented by a single UPDATE statement, but more complicated price changes could not.

Example (III) CREATE PROCEDURE joeGouge() AS theBeer Sells.beer%TYPE; thePrice Sells.price%TYPE; CURSOR c IS SELECT beer, price FROM Sells WHERE bar = 'Joe''s bar';

Example (III) BEGIN OPEN c; LOOP FETCH c INTO theBeer, thePrice; EXIT WHEN c%NOTFOUND; IF thePrice < 3.00 THEN UDPATE Sells SET price = thePrice + 1.00 WHERE bar = 'Joe''s Bar' AND beer = theBeer; END IF; END LOOP;

Example (IV) CLOSE c; END; . run

Single-Row Select Select-from-where in PL/SQL must have an INTO clause listing variables into which a tuple can be placed. It is an error if the select-from-where returns more than one tuple; you should have used a cursor.

Example Find the price Joe charges for Bud (and drop it on the floor). Sells(bar, beer, price) DECLARE p Sells.price%TYPE;

Example (II) BEGIN SELECT price INTO p FROM Sells WHERE bar = 'Joe''s Bar' AND beer = 'Bud'; END; . run