Object-Oriented Query Languages: Object Query Language (OQL)



Similar documents
Relational Database: Additional Operations on Relations; SQL

Relational Algebra. Basic Operations Algebra of Bags

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Instant SQL Programming

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Oracle Database: SQL and PL/SQL Fundamentals NEW

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Chapter 5. SQL: Queries, Constraints, Triggers

Oracle Database: SQL and PL/SQL Fundamentals

Financial Data Access with SQL, Excel & VBA

Comp 5311 Database Management Systems. 3. Structured Query Language 1

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

3.GETTING STARTED WITH ORACLE8i

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Introduction to Microsoft Jet SQL

SQL Basics. Introduction to Standard Query Language

Microsoft Access 3: Understanding and Creating Queries

SQL. Short introduction

Information Systems (Informationssysteme)

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

Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

A Brief Introduction to MySQL

Database Query 1: SQL Basics

1. Write the query of Exercise 6.19 using TRC and DRC: Find the names of all brokers who have made money in all accounts assigned to them.

Complex Data and Object-Oriented. Databases

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

Databases 2011 The Relational Model and SQL

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

IT2304: Database Systems 1 (DBS 1)

MICROSOFT ACCESS STEP BY STEP GUIDE

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Relational Databases

Real SQL Programming. Embedded SQL Call-Level Interface Java Database Connectivity

IT2305 Database Systems I (Compulsory)

Oracle Database 11g SQL

Using AND in a Query: Step 1: Open Query Design

Query-by-Example (QBE)

Chapter 6 The database Language SQL as a tutorial

Relational Algebra and SQL

Data Stream Management and Complex Event Processing in Esper. INF5100, Autumn 2010 Jarle Søberg

Database Applications Microsoft Access

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Database Design and Programming

Introduction to Python

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Introduction to SQL for Data Scientists

SQL Server 2008 Core Skills. Gary Young 2011

PSU SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams

Inquiry Formulas. student guide

Programming with SQL

Moving from CS 61A Scheme to CS 61B Java

Paper An Introduction to SAS PROC SQL Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Exercise 4 Learning Python language fundamentals

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database 10g: Introduction to SQL

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

T-SQL STANDARD ELEMENTS

Introduction to database design

Using SQL Queries in Crystal Reports

Oracle Database: SQL and PL/SQL Fundamentals NEW

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Information Systems SQL. Nikolaj Popov

Database Administration with MySQL

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Query 4. Lesson Objectives 4. Review 5. Smart Query 5. Create a Smart Query 6. Create a Smart Query Definition from an Ad-hoc Query 9

SQL: Queries, Programming, Triggers

Performing Queries Using PROC SQL (1)

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

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

SQL Nested & Complex Queries. CS 377: Database Systems

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Oracle Database: Introduction to SQL

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

This is a training module for Maximo Asset Management V7.1. It demonstrates how to use the E-Audit function.

IS 312, Information Systems for Business Database Project in Access

SQL Tables, Keys, Views, Indexes

Oracle Database: Introduction to SQL

Exercise 1: Python Language Basics

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

To increase performaces SQL has been extended in order to have some new operations avaiable. They are:

How To Create A Table In Sql (Ahem)

Ad Hoc Advanced Table of Contents

Visual Basic Programming. An Introduction

Creating QBE Queries in Microsoft SQL Server

Microsoft' Excel & Access Integration

Oracle 10g PL/SQL Training

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Unit 10: Microsoft Access Queries

5.1 Database Schema Schema Generation in SQL

Transcription:

INF212 Database Theory Object-Oriented Query Languages: Object Query Language (OQL) Edited by N. Akkøk spring 2003 and spring 2005 from Pål Halvorsen s slides (spring 2002) Also contains slides made by Arthur M. Keller, Vera Goebel, Ragnar Normann

OQL Queries/sub-queries Return types Quantifiers Object creation Aggregation Using host languages Overview Operators on set or bag objects Grouping with properties Page 2

Motivation: Object Query Language (OQL) Relational languages suffer from impedance mismatch when we try to connect them to conventional languages like C or C++ The data models of C and SQL are radically different, e.g., C does not have relations, sets, or bags as primitive types OQL is the query language in the ODMG standard OQL is an attempt by the OO community to extend languages like C++ with SQL-like, relation-at-a-time dictions. Like SQL, OQL is a declarative (not procedural) language Page 3

OQL uses ODL OQL is designed to operate on data described in ODL: For every class we can declare an extent = name for the current set of objects of the class. Remember to refer to the extent, not the class name, in queries. Page 4

OQL: Object- and Value-Equality Two objects of the same type (instances of the same class) cannot be equal, but they may have the same values Example: Object O 1 and O 2 are instance of the of the same class The OQL expression O 1 = O 2 will always be FALSE The OQL expression *O 1 = *O 2 can be TRUE if the two objects have the same state, i.e., same value of all attributes Page 5

OQL: Computations Mutable objects are manipulated by executing defined methods for this class Select in OQL may have side effects, i.e., it can change the state in the database (OQL does not have an own update function in contrast to SQL) Methods are called by navigating along paths; there is no difference for addressing of attributes, relationships, or methods. Page 6

OQL: Types Basic types: string, integer, float, boolean, character, enumerations, etc. Type constructors: Struct for structures. Collection types: set, bag, list, array. (NOTE: dictionary is not suported) Set(Struct()) and Bag(Struct()) play special roles akin to relations. Page 7

OQL: Path Expressions We access components using dot-notations Let x be an object of class C: If a is an attribute of C, then x.a is the value of a in the x object. If r is a relationship of C, then x.r is the value to which x is connected by r, i.e., could be an object or a collection of objects, depending on the type of r If m is a method of C, then x.m( ) is the result of applying m to x. We can form expressions with several dots (only last element may be a collection) OQL allows arrows as a synonym for the dot, i.e, x a is equal to x.a, opposed to for example in C Page 8

OQL: The Bar-Beer-Sell (BBS) Example ODL class Bar (extent Bars) { attribute string name; attribute string addr; relationship Set<Sell> beerssold inverse Sell::bar; } class Beer (extent Beers) { attribute string name; attribute string manf; relationship Set<Sell> soldby inverse Sell::beer; } class Sell (extent Sells) { attribute float price; relationship Bar bar inverse Bar::beersSold; relationship Beer beer inverse Beer::soldBy; void raise_price(float price); } Page 9

OQL: Path Expressions for BBS Example Let s be a variable whose type is Sell s.price is the price in the object s (the beer sold in this bar) s.raise_price(x) raises the price of s.beer in s.bar with x s.bar is a pointer to the bar mentioned in s s.bar.addr is the address of the bar mentioned in s Note: cascade of dots OK because s.bar is an object, not a collection Let b be a variable whose type is Bar b.name is the name of the bar b.beerssold is a set of beers that this bar sells (set of pointers to Sell) Illegal use of path expressions: b.beerssold.price Note: illegal because b.beerssold is a set of objects, not a single object Typical Usage: If x is an object, you can extend the path expression, like s is extended with s.beer and s.beer.name above If x is a collection, like b.beerssold above, it can be used anywhere a collection is appropriate (e.g., FROM), if you want to access attributes of x. Page 10

OQL: Select-From-Where Similar to SQL syntax: SELECT <list of values> FROM <list of collections and typical members> WHERE <condition> Collections in FROM can be: 1. Extents 2. Expressions that evaluate to a collection Following a collection is a name for a typical member, optionally preceded by the keyword AS Note: there may be several different queries giving the same answer Page 11

OQL BBS Example: Select-From-Where Get menu at Joe s focusing on Sells objects: SELECT s.beer.name, s.price FROM Sells s WHERE s.bar.name = "Joe's" Notice double-quoted strings in OQL (SQL has single-quoted) Get Joe s menu, this time focusing on the Bar objects: SELECT s.beer.name, s.price FROM Bars b, b.beerssold s WHERE b.name = "Joe's" Notice that the typical object b in the first collection of FROM is used to help define the second collection. Page 12

OQL: Comparison Operators Values can generally be compared using operators: =!= < > <= >= : equality : different form : less than : greater than : less or equal : greater or equal Additional text comparison operators IN checks if a character is in a text string: <c> IN <text> LIKE checks if two texts are equal: <text 1 > LIKE <text 2 > <text 2 > may contain special characters: _ or? : one arbitrary character * or % : any arbitrary text string Page 13

OQL,BBS Example: Comparison Operators Example: find name and price of all bees at Joe s starting with B and consisting of the text string ud SELECT s.beer.name, s.price FROM Bars b, b.beerssold s WHERE b.name = "Joe's" AND s.beer.name LIKE "B*" AND s.beer.name LIKE "*ud*" AND NOTE 1: The name of the bar is equal to Joe s NOTE 2: The beer name starts with B followed by arbitrary characters NOTE 3: NOTE 3: The beer name contains ud starting with and followed by arbitrary characters Page 14

OQL: Quantifiers We can test whether all members, at least one member, some members, etc. satisfy some condition Boolean-valued expressions for use in WHERE-clauses. All: FOR ALL x IN <collection> : <condition> At least one: EXISTS x IN <collection> : <condition> EXISTS x Only one: UNIQUE x Some/any: <collection> <comparison> SOME/ANY <condition> where <comparison > = <, >, <=, >=, or = The expression has value TRUE if the condition is true NOT reverses the boolean value Page 15

OQL BBS Example: Quantifiers - I Example: Find all bars that sell some beer for more than $5 SELECT b.name FROM Bars b WHERE EXISTS s IN b.beerssold : s.price > 5.00 Example: How would you find the bars that only sold beers for more than $5? SELECT b.name FROM Bars b WHERE FOR ALL s IN b.beerssold : s.price > 5.00 Page 16

OQL BBS Example: Quantifiers - II Example: Find the bars such that the only beers they sell for more than $5 are manufactured by Pete s SELECT b.name FROM Bars b WHERE FOR ALL be IN ( SELECT s.beer be.manf = "Pete's" FROM b.beerssold s WHERE s.price > 5.00 ) : NOTE 2: NOTE 2: all these expensive beers must be manufactured by Pete s NOTE 1: find all beers in a bar where the price is more than $5 Page 17

OQL: Type of the Result Default: bag of structs, field names taken from the ends of path names in SELECT clause. Example: menu at Joe s : SELECT s.beer.name, s.price FROM Sells s WHERE s.bar.name = "Joe's" has result type: Bag(Struct(name: string, price: real)) Page 18

OQL: Rename Fields The result type Bag(Struct(name: string, price: real)) may not have appropriate names for the results attributes Rename by prefixing the path with the desired name and a colon Example: rename attributes of the menu at Joe s : SELECT beername: s.beer.name, s.price FROM Bars b, b.beerssold s WHERE b.name = "Joe's has type: Bag(Struct(beername: string, price: real)) Page 19

OQL: Change the Collection Type - I A bag of structs (default) returned by the SFW-statement is not always appropriate Use SELECT DISTINCT to get a set of structs Example: SELECT DISTINCT s.beer.name, s.price FROM Bars b, b.beerssold s WHERE b.name = "Joe's" Page 20

OQL: Change the Collection Type - II Use ORDER BY clause to get a list of structs Example: joemenu = SELECT s.beer.name, s.price FROM Bars b, b.beerssold s WHERE b.name = "Joe's" ORDER BY s.price ASC ASC = ascending (default); DESC = descending We can extract from a list as if it were an array, e.g., cheapest_beer = joemenu[0].name; Page 21

OQL: Subqueries Used where the result can be a collection type is appropriate, i.e., mainly in FROM clauses and with quantifiers like EXISTS, FOR ALL, etc. Example: subquery in FROM: Find the manufacturers of the beers served at Joe's SELECT DISTINCT b.manf FROM ( SELECT s.beer FROM Sells s WHERE s.bar.name = "Joe's" ) b Page 22

OQL: Assigning Values to Host Language Variables Unlike SQL, which needs to move data between tuples and variables, OQL fits naturally into a host language Select-From-Where produces collections of objects It is possible to assign any variable of proper type a value that is a result from OQL expressions Example (C++ like): Name of bars that only sold beers for more than $5 Set<string> expensive_bars; expensive_bars = SELECT DISTINCT b.name FROM Bars b WHERE FOR ALL s IN b.beerssold : s.price > 5.00 Page 23

OQL: Extraction of Collection Elements I A collection with a single member: Extract the member with ELEMENT. Example: Find the price Joe s charges for Bud and put the result in a variable p: p = ELEMENT( SELECT s.price FROM Sells s WHERE s.bar.name = "Joe's" AND s.beer.name = "Bud") Page 24

OQL: Extraction of Collection Elements II Extracting all elements of a collection, one at a time: 1. Turn the collection into a list. 2. Extract elements of a list with <list_name>[i] Example (C-like): Print Joe's menu, in order of price, with beers of the same price listed alphabetically L = SELECT s.beer.name, s.price FROM Sells s WHERE s.bar.name = "Joe's" ORDER BY s.price, s.beer.name; NOTE 1: make a list printf("beer\tprice\n\n"); NOTE 2: The i th element in L is obtained from L[i-1]. The index i starts at 0 for( i=0; i<=count(l); i++) printf("%s\t%f\n", L[i].name, L[i].price); Page 25

OQL: Creating New Objects A Select-From-Where statement allows us to create new objects whose type is defined in by the types returned in the SELECT statement Example SELECT beername: s.beer.name, s.price FROM Bars b, b.beerssold s NOTE: Defines a new object WHERE b.name = "Joe's Bar" Bag<Struct( beername: string, price: integer)> Constructor functions: create new instances of a class or other defined type (details depend on host language) Example: insert a new beer newbeer = Beer(name: "XXX", manufacturer: "YYY ) Effects: Create a new Beer object, which becomes part of the extent Beers The value of the host language variable newbeer is this object Page 26

OQL: Aggregation The five operators avg, sum, min, max, and count apply to any collection, as long as the operators make sense for the element type. Example: Find the average price of beer at Joe s. x = AVG( SELECT s.price FROM Sells s WHERE s.bar.name = "Joe's"); Note: result of SELECT is technically a bag of 1-field structs, which is identified with the bag of the values of that field. Page 27

OQL: Union, Intersection, and Difference We may apply union, intersection, and difference operators on any objects of Set or Bag type Use keywords UNION, INTERSECT, and EXCEPT, respectively Result type is a Bag if one object is of type Bag; Set otherwise Example: Find the name of all beers served at Joe s that are not served at Steve s ( SELECT s.beer.name FROM Sells s WHERE s.bar.name = "Joe's ) EXCEPT ( SELECT s.beer.name FROM Sells s WHERE s.bar.name = "Steve's ) NOTE 1: find all beers served at Joe s NOTE 2: find all beers served at Steve s NOTE 3: remove beers served at Steve s for beers served at Joe s Page 28

OQL: Grouping I OQL supports grouping similar to SQL - some differences Example in SQL: find average price of beers in all bars SELECT bar.name, AVG(price) FROM Sells GROUP BY bar; Is the bar value the name of the group, or the common value for the bar component of all tuples in the group? In SQL it doesn't matter, but in OQL, you can create groups from the values of any function(s), not just attributes. Thus, groups are identified by common values, not name. Example: group by first letter of bar names (method needed). Page 29

OQL: Grouping II General form: GROUP BY f 1 : e 1, f 2 : e 2,..., f n : e n Thus, made by the OQL clause: Keywords GROUP BY Comma separated list of partition attributes: name colon, and expression Example: SELECT... FROM... GROUP BY barname: s.bar.name Page 30

OQL: Grouping Outline Initial collection: defined by FROM, WHERE Intermediate collection: with function values and partition Output collection Group by values of function(s) Terms from SELECT clause NOTE 1: the selected objects (WHERE) from the collection of objects in FROM, but technically it is a Bag of structs NOTE 2: actual values returned from initial collection when applying GROUP BY expressions: Struct(f 1 :v 1,..., partition:p). First fields indicate the group, P is a bag of values belonging to this group NOTE 3: NOTE 3: The SELECT clause may select from intermediate collection, i.e., f 1,f 2,..,f n and partition values may only be referred through aggregate functions on the members of bag P. Page 31

OQL BBS Example: Grouping I Example: Find the average price of beer at each bar SELECT FROM GROUP BY barname, avgprice: AVG(SELECT p.s.price FROM partition p) Sells s barname: s.bar.name Page 32

OQL BBS Example: Grouping II SELECT barname, avgprice: AVG( SELECT p.s.price FROM partition p) FROM Sells s GROUP BY barname: s.bar.name 1. Initial collection: Sells But technically, it is a bag of structs of the form Struct(s: s1) where s1 is a Sell object. Note, the lone field is named s. In general, there are fields for all of the typical objects in the FROM clause. Page 33

OQL BBS Example: Grouping III SELECT barname, avgprice: AVG( FROM Sells s GROUP BY barname: s.bar.name SELECT p.s.price FROM partition p) 2. Intermediate collection One function: s.bar.name maps Sell objects s to the value of the name of the bar referred to by s Collection is a set of structs of type: Struct{barName:string, partition:set<struct{s:sell}>} For example: Struct(barName = "Joe's",partition = {s 1,,s n }) where s 1,,s n are all the structs with one field, named s, whose value is one of the Sell objects that represent Joe's Bar selling some beer. Page 34

OQL BBS Example: Grouping IV SELECT barname, avgprice: AVG( SELECT p.s.price FROM partition p) FROM Sells s GROUP BY barname: s.bar.name 3. Output collection: consists of beer-average price pairs, one for each struct in the intermediate collection Type of structures in the output: Struct{barName: string, avgprice: real} Note that the subquery in the SELECT clause variables in the partition is referred through the AVG aggregate function We let p range over all structs in partition. Each of these structs contains a single field named s and has a Sell object as its value. Thus, p.s.price extracts the price from one of the Sell objects belonging to this particular bar. Typical output struct - example: Struct(barName = "Joe's", avgprice = 2.83) Page 35

Another OQL BBS Example: Grouping I Example: Find, for each beer, the number of bars that charge a low price ( 2.00) and a high price ( 4.00) for that beer Strategy: group by three things: The beer name, a boolean function that is true if the price is low, and a boolean function that is true if the price is high. SELECT beername, low, high, count: COUNT(partition) FROM Beers b, b.soldby s GROUP BY beername: b.name, low: s.price <= 2.00, high: s.price >= 4.00 Page 36

SELECT FROM GROUP BY OQL: Another BBS Example: Grouping II bname, low, high, count: COUNT(partition) Beers b, b.soldby s bname: b.name, low: s.price <= 2.00, high: s.price >= 4.00 1. Initial collection: Pairs (b, s), where b is a Beer object, and s is a Sell (b.soldby) object representing the sale of that beer at some bar Type of collection members: Struct{b: Beer, s: Sell} Page 37

OQL: Another BBS Example: Grouping III SELECT bname, low, high, count: COUNT(partition) FROM Beers b, b.soldby s GROUP BY bname: b.name, low: s.price <= 2.00, high: s.price >= 4.00 2. Intermediate collection: Quadruples consisting of a beer name, booleans telling whether this group is for high prices, low prices, and the partition for that group The partition is a set of structs of the type: Struct{b: Beer, s: Sell} A typical partition value: Struct(b:"Bud" object,s:a Sell object involving Bud) Page 38

Another OQL BBS Example: Grouping IV 2. Intermediate collection (continued): Type of quadruples in the intermediate collection: Struct{ bname: string, low: boolean, high: boolean, partition: Set<Struct{b: Beer, s:sell}>} Typical structs in intermediate collection: bname low high partition Bud TRUE FALSE S low Bud FALSE TRUE S high Bud FALSE FALSE S mid NOTE 5: NOTE 5: the partition with low = high = TRUE must be empty and will not appear NOTE 1: S X are the sets of beer-sells pairs (b, s) NOTE 2: S low : price is low ( 2) NOTE 3: S high : price is high ( 4) NOTE 4: S mid : medium price (between 2 and 4) Page 39

Another OQL BBS Example: Grouping V SELECT bname, low, high, count: COUNT(partition) FROM Beers b, b.soldby s GROUP BY bname: b.name, low: s.price <= 2.00, high: s.price >= 4.00 3. Output collection: The first three components of each group's struct are copied to the output The last (partition) is counted An example of the result: bname low high count Bud TRUE FALSE 27 Bud FALSE TRUE 14 Bud FALSE FALSE 36 Page 40

OQL: Having GROUP BY may be followed by HAVING to eliminate some of the groups created by GROUP BY The condition applies to the partition field in each structure in the intermediate collection If condition in HAVING clause is FALSE, the group does not contribute to the output collection Page 41

OQL BBS Example: Having Example: Find the average price of beers at each bar, but only in those bars where the most expensive beer cost more than 10$ SELECT FROM barname,avgprice: AVG(SELECT p.s.price FROM partition p) Sells s GROUP BY barname: s.bar.name HAVING MAX( SELECT p.s.price FROM partition p) > 10 NOTE 1: Same as above, finds average price of beers in a bar NOTE 2: Select only those groups where the maximum price is larger than 10 Page 42

OQL Summary Queries/subqueries Select-From-Where Return types bags, sets, or lists Quantifiers for all, exists, etc. Object creation both new elements and returned form queries Aggregation count, max, min, avg, sum Using host languages OQL fits naturally Operators on set or bag objects union, intersect, except Grouping with properties group by with having Page 43