Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

Similar documents
Assign expressions to declared variables with :=. END IF; EXIT WHEN éconditioné END LOOP;

Databases 1. SQL/PSM and Oracle PL/SQL

Real SQL Programming Persistent Stored Modules (PSM)

Beersèname, manfè. Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

Outline. CS 235: Introduction to Databases. DB Application Programming. Interface Solutions. Basic PSM Form. Persistent Stored Modules

PL/SQL, Embedded SQL. Lecture #14 Autumn, Fall, 2001, LRX

Introduction to SQL. Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Real SQL Programming 1

Databases 1. Defining Tables, Constraints

CSCD43: Database Systems Technology. Lecture 4

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

Chapter 6 The database Language SQL as a tutorial

Transactions, Views, Indexes. Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

Views, Indexes, Authorization. Views. Views 8/6/18. Virtual and Materialized Views Speeding Accesses to Data Grant/Revoke Priviledges

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

Grouping Operator. Applying γ L (R) Recall: Outerjoin. Example: Grouping/Aggregation. γ A,B,AVG(C)->X (R) =??

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

Chapter 9 SQL in a server environment

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

A tuple is dangling if it doesn't join with any

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Logical Query Languages. Motivation: 1. Logical rules extend more naturally to. recursive queries than does relational algebra.

CSC 343 Winter SQL: Aggregation, Joins, and Triggers MICHAEL LIUT

DATABASE DESIGN - 1DL400

Keep relation as the fundamental. Compare with ëobject-oriented DBMS," which. and tacks on relations as one of many types.

CS54100: Database Systems

They like one or more beers and frequent one. Bars have unique names and addresses. They. serve one or more beers and are frequented

Database Design and Programming

Chapter 6 The database Language SQL as a tutorial

Keep relation as the fundamental. Compare with ëobject-oriented DBMS," which. and tacks on relations as one of many types.

Schedule. Feb. 12 (T) Advising Day. No class. Reminder: Midterm is Feb. 14 (TH) Today: Feb. 7 (TH) Feb. 21 (TH) Feb. 19 (T)

Subqueries. Must use a tuple-variable to name tuples of the result

Chapter 2 The relational Model of data. Relational model introduction

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

Interpretation is not exactly ëmissing value." There could be many reasons why no value is

SQL DATA DEFINITION LANGUAGE

SQL: Data Manipulation Language

Constraints. Local and Global Constraints Triggers

Chapter 9 SQL in a server environment

Introduction to Transactions: Controlling Concurrent "Behavior" Virtual and Materialized Views" Indexes: Speeding Accesses to Data"

Databases-1 Lecture-01. Introduction, Relational Algebra

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

SQL in Programming Languages Read chapter 5 of Atzeni et al. BD: Modelli e Linguaggi di Interrogazione and section 8.

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

CSCI3030U Database Models

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

Non-interactive SQL. EECS Introduction to Database Management Systems

Most database operations involve On- Line Transaction Processing (OTLP).

Relational Algebra and SQL. Basic Operations Algebra of Bags

Query Languages for XML

Contains slides made by Naci Akkøk, Pål Halvorsen, Arthur M. Keller, Vera Goebel

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

On-Line Application Processing

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

Relational Query Languages

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

Contents I Introduction 1 Introduction to PL/SQL iii

Database Application Development

Oracle PLSQL Training Syllabus

Relational Algebra. Algebra of Bags

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Database-Connection Libraries

Negation wrapped inside a recursion makes no. separated, there can be ambiguity about what. the rules mean, and some one meaning must

Chapter 10 Advanced topics in relational databases

XML. Document Type Definitions. Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

I Relational Database Modeling how to define

Warehousing. Data Mining

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

I Relational Database Modeling how to define

Schedule. Today: Feb. 21 (TH) Feb. 28 (TH) Feb. 26 (T) Mar. 5 (T) Read Sections , Project Part 6 due.

Constraints and Triggers

Oracle Database 12c: Program with PL/SQL Duration: 5 Days Method: Instructor-Led

Lecture 08. Spring 2018 Borough of Manhattan Community College

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Chapter 7: Constraints and Triggers. Foreign Keys Local and Global Constraints Triggers

XQUERY: an XML Query Language

CS54100: Database Systems

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Logical Query Languages. Motivation: 1. Logical rules extend more naturally to. recursive queries than does relational algebra. Used in SQL recursion.

Oracle Database: Program with PL/SQL

Entity-Relationship Model. Purpose of E/R Model

Oracle Database: Program with PL/SQL Ed 2

Oracle - Oracle Database: Program with PL/SQL Ed 2

Chapter 2 The relational Model of data. Relational algebra

Recap and Schedule. Till Now: Today: Ø Query Languages: Relational Algebra; SQL. Ø Datalog A logical query language. Ø SQL Recursion.

Oracle Database 12c R2: Program with PL/SQL Ed 2 Duration: 5 Days

Conditionally control code flow (loops, control structures). Create stored procedures and functions.

IZ0-144Oracle 11g PL/SQL Certification (OCA) training

From E/R Diagrams to Relations

Database Management

SQL STORED ROUTINES. CS121: Relational Databases Fall 2017 Lecture 9

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

based mainly on course notes by Will Hyslop (UNB-F) and course notes by Jeff Ullman and others (Stanford)

Oracle Database 11g: Program with PL/SQL Release 2

1 Relational Data Model

Transcription:

Modication to Views Via Triggers Oracle allows us to ëintercept" a modication to a view through an instead-of trigger Example Likesèdrinker, beerè Sellsèbar, beer, priceè Frequentsèdrinker, barè CREATE VIEW Synergy AS SELECT Likesdrinker, Likesbeer, Sellsbar FROM Likes, Sells, Frequents WHERE Likesdrinker = Frequentsdrinker AND Likesbeer = Sellsbeer AND Sellsbar = Frequentsbar; 1

CREATE TRIGGER ViewTrig INSTEAD OF INSERT ON Synergy FOR EACH ROW INSERT INTO Likes VALUESè :newdrinker, :newbeerè; INSERT INTO Sellsèbar, beerè VALUESè:newbar, :newbeerè; INSERT INTO Frequents VALUESè :newdrinker, :newbarè; run 2

SQL Triggers Read in text Some dierences, including: 1 The Oracle restriction about not modifying the relation of the trigger or other relations linked to it by constraints is not present in SQL èbut Oracle is real; SQL is paperè 2 The action in SQL is a list of èrestrictedè SQL statements, not a PLèSQL statement 3

PLèSQL Oracle's version of PSM èpersistent, Stored Modulesè Use via sqlplus A compromise between completely procedural programming and SQL's very high-level, but limited statements Allows local variables, loops, procedures, examination of relations one tuple at a time Rough form: DECLARE declarations executable statements run; DECLARE portion is optional Dot and run èor a slash in place of run;è are needed to end the statement and execute it 4

Simplest Form: Sequence of Modications Likesèdrinker, beerè INSERT INTO Likes VALUESè'Sally', 'Bud'è; DELETE FROM Likes WHERE drinker = 'Fred' AND beer = 'Miller'; run; 5

Procedures Stored database objects that use a PLèSQL statement in their body Procedure Declarations CREATE OR REPLACE PROCEDURE énameéèéarglistéè AS édeclarationsé éplèsql statementsé run; 6

Argument list has name-mode-type triples Mode: IN, OUT, or IN OUT for readonly, write-only, readèwrite, respectively Types: standard SQL + generic types like NUMBER = any integer or real type Since types in procedures must match their types in the DB schema, you should generally use an expression of the form relationattributeètype to capture the type correctly 7

Example A procedure to take a beer and price and add it to Joe's menu Sellsèbar, beer, priceè CREATE PROCEDURE joemenuè b IN SellsbeerèTYPE, p IN SellspriceèTYPE èas INSERT INTO Sells VALUESè'Joe''s Bar', b, pè; run; Note ërun" only stores the procedure; it doesn't execute the procedure 8

Invoking Procedures A procedure call may appear in the body of a PLèSQL statement Example: joemenuè'bud', 250è; joemenuè'moosedrool', 500è; run; 9

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; 10

Queries in PLèSQL 1 Single-row selects allow retrieval into a variable of the result of a query that is guaranteed to produce one tuple 2 Cursors allow the retrieval of many tuples, with the cursor and a loop used to process each in turn 11

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 oorè Sellsèbar, beer, priceè DECLARE p SellspriceèTYPE; SELECT price INTO p FROM Sells WHERE bar = 'Joe''s Bar' AND beer = 'Bud'; run 12

Cursors Declare by: CURSOR énameé IS select-from-where statement 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; 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 13

Example A procedure that examines the menu for Joe's Bar and raises by $100 all prices that are less than $300 Sellsèbar, beer, priceè This simple price-change algorithm can be implemented by a single UPDATE statement, but more complicated price changes could not 14

CREATE PROCEDURE joegougeèè AS thebeer SellsbeerèTYPE; theprice SellspriceèTYPE; CURSOR c IS SELECT beer, price FROM Sells WHERE bar = 'Joe''s bar'; OPEN c; LOOP FETCH c INTO thebeer, theprice; EXIT WHEN cènotfound; IF theprice é 300 THEN UPDATE Sells SET price = theprice + 100 WHERE bar = 'Joe''s Bar' AND beer = thebeer; END IF; END LOOP; CLOSE c; run 15

Row Types Anything èeg, cursors, table namesè that has a tuple type can have its type captured with èrowtype We can create temporary variables that have tuple types and access their components with dot Handy when we deal with tuples with many attributes 16

Example The same procedure with a tuple variable bp CREATE PROCEDURE joegougeèè AS CURSOR c IS SELECT beer, price FROM Sells WHERE bar = 'Joe''s bar'; bp cèrowtype; OPEN c; LOOP FETCH c INTO bp; EXIT WHEN cènotfound; IF bpprice é 300 THEN UDPATE Sells SET price = bpprice + 100 WHERE bar = 'Joe''s Bar' AND beer = bpbeer; END IF; END LOOP; CLOSE c; run 17