Docsity

Object-Oriented Query Languages: Understanding OQL and Its Functionality, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Object-Oriented Query Languages: Understanding OQL and Its Functionality and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Object-Orientation in Query

Languages

OQL

  • OQL, the Object Query Language, gives us an SQL like notation for expressing queries.
  • It is intended that OQL will be used as an extension to some object-oriented host languages, such as C++, Smalltalk, or Java.

Select-From-Where Expressions in OQL

  • OQL permits us to write expressions using a select- from-where syntax similar to SQL’s 1) The keyword SELECT followed by a list of expressions. 2) The keyword FROM followed by a list of one or more variable declarations. 3) The keyword WHERE and Boolean-value expressions

Modifying the Result

  • To make the result a set, use the keyword DISTINCT after SELECT, as in SQL.
  • To make the result a list, add ORDER BY clause at the end of the query again as in SQL.

Complex Output Types

  • The elements in the SELECT clause need not be simple variables. They can be any expression, including expressions built using type constructors. For example, we can apply the Struct type constructor to several expressions and get a select-from-where query that produces a set or bag of structures.

Example

SELECT DISTINCT Struct(start1: s1, start2: s2)

FROM Star s1, Star s

WHERE s1.address = s2.address AND s1.name<s2.name

We want the set of pairs of stars living at the same address.

Aggregation Expressions

  • OQL uses the same five aggregation operators that SQL does: AVG, COUNT, SUM, MIN, and MAX.

Example: AVG(SELECT m.length FROM Movies m)

Union, Intersection, and Difference

We may apply the union, intersection, and difference operators to two objects of set or bag type. These three operators are represented, as in SQL, by the keywords UNION, INTERSECT, and EXCEPT, respectively.

  • (SELECT DISTINCT m
  • FROM Movies m, m.stars s
  • WHERE s.name = “Harrison Ford”)
  • EXCEPT
  • (SELECT DISTINCT m
  • FROM Movies m
  • WHERE m.ownedBy.name = “Disney”

Obtaining Each Member of a Collection

To obtaining each member of a set or bag we need to turn our set or bag into a list. We do so with a select-from-where expressions that uses ORDER BY.

Example

  1. movieList = SELECT m FROM Movies m ORDER BY m.title, m.year
  2. numberOfMovies = COUNT(Movies);
  3. for(i=0; i<numberOfMovies; i++){
  4. movie = movieList[i];
  5. count<<movie.title<<“ “<<movie.year<<“ “
  6. <<movie.length<<“\n”;

Line 1 sort the movie class. Line 2 computes the number of movies, using the OQL operator COUNT. Line 3 and 4 use for loop inwhich integer variable i ranges over each position of the list. After that the i element of the list assigned to variable movie. Line 5 and 6 are for print.

Defining Types in SQL

  • A simple form of UDT definition is:
  1. The keywords CREATE TYPE
  2. The name for the type
  3. The keyword AS
  4. A parenthesized, comma-separated list of attributes and their types.
  5. A comma-separated list of methods, including their argument type(s), and return type

Example

CREATE TYPE AddressType AS {

street CHAR(50), city CHAR(20)

};

CREATE TYPE StarType AS {

name CHAR (30), address AddressType

};