Docsity

ODL Subclasses-Database systems-Lecture 16 Slides-Computer Science, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download ODL Subclasses-Database systems-Lecture 16 Slides-Computer Science and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

ODL Sub classes

Follow name of sub class by colon and its sup erclass.

Example: Ales are Beers with a Color

class Ales:Beers { attribute string color; }

 Ob jects of the Ales class acquire all the attributes and relationships of the Beers class.

 While E/R entities can have manifestations in a class and sub class, in ODL we assume each ob ject is a memb er of exactly one class.

Keys in ODL

Indicate with key(s) following the class name, and a list of attributes forming the key.

 Several lists may b e used to indicate several alternative keys.

 Parentheses group memb ers of a key, and also group key to the declared keys.

 Thus, (key(a 1 ; a 2 ; : : : ; an )) = \one key consisting of all n attributes." (key a 1 ; a 2 ; : : : ; an ) = \each ai is a key by itself."

Example

class Beers (key name) { attribute string name ...

 Remember : Keys are optional in ODL. The \ob ject ID" suces to distinguish ob jects that have the same values in their elements.

Translating ODL to Relations

  1. Classes without relationships: like entity set, but several new problems arise.
  2. Classes with relationships:

a) Treat the relationship separately, as in E/R. b) Attach a many-one relationship to the relation for the \many."

ODL Class Without Relationships

 Problem: ODL allows attribute typ es built from structures and collecti on typ es.

 Structure: Make one attribute for each eld.

 Set: make one tuple for each memb er of the set. F More than one set attribute? Make tuples for all combinations.

 Problem: ODL class may have no key, but we should have one in the relation to represent \OID."

ODL Relationships

 If the relationship is many-one from A to B , put key of B attributes in the relation for class A.

 If relationship is many-many, we'll have to duplicate A-tuples as in ODL with set-valued attributes. F Wouldn't you really rather create a separate relation for a many-many- relationship?

F You'll wind up separating it anyway, during BCNF decomp osition.

Example

class Drinkers (key name) { attribute string name; attribute string addr; relationship Set likes inverse Beers::fans; relationship Beers favorite inverse Beers::realFans; relationship Drinkers husband inverse wife; relationship Drinkers wife inverse husband; relationship Set buddies inverse buddies; }

Drinkers(name, addr, b eerName, favBeer, wife, buddy)

OQL

Motivatio n:

 Relational languages su er from impedance mismatch when we try to connect them to conventional languages like C or C++. F The data mo dels of C and SQL are radically di erent, e.g. C do es not have relations, sets, or bags as primitive typ es; C is tuple-at-a-time, SQL is relation-at-a- time.

 OQL is an attempt by the OO community to extend languages like C++ with SQL-like, relation-at-a-time dictions.

OQL Typ es

 Basic typ es: strings, ints, reals, etc., plus class names.

 Typ e constructors:

F Struct for structures.

F Collecti on typ es: set, bag, list, array.

 Like ODL, but no limit on the numb er of times we can apply a typ e constructor.

 Set(Struct()) and Bag(Struct()) play sp ecial roles akin to relations.

class Bar (extent Bars) { attribute string name; attribute string addr; relationship Set beersSold inverse Sell::bar; }

class Beer (extent Beers) { attribute string name; attribute string manf; relationship Set soldBy inverse Sell::beer; }

class Sell (extent Sells) { attribute float price; relationship Bar bar inverse Bar::beersSold; relationship Beer beer inverse Beer::soldBy; }

Path Expressions

Let x b e an ob ject of class C.

 If a is an attribute of C , then x:a = the value of a in the x ob ject.

 If r is a relationship of C , then x:r = the value to which x is connected by r. F Could b e an ob ject or a collect i on of ob jects, dep ending on the typ e of r.

 If m is a metho d of C , then x:m(  ) is the result of applying m to x.

OQL Select-From-Where

SELECT FROM WHERE

 Collecti ons in FROM can b e:

  1. Extents.
  2. Expressions that evaluate to a collecti on.

 Following a collecti on is a name for a typical memb er, optionally preceded by AS.

Example

Get the menu at Jo e's.

SELECT s.beer.name, s.price FROM Sells s WHERE s.bar.name = "Joe's Bar"

 Notice double-quoted strings in OQL.

Example

Another way to get Jo e's menu, this time fo cusing on the Bar ob jects.

SELECT s.beer.name, s.price FROM Bars b, b.beersSold s WHERE b.name = "Joe's Bar"

 Notice that the typical ob ject b in the rst collecti on of FROM is used to help de ne the second collecti on.

Typical Usage

 If x is an ob ject, you can extend the path expression, like s or s.beer in s.beer.name.

 If x is a collect i on, you use it in the FROM list, like b.beersSold ab ove, if you want to access attributes of x.

Rename Fields

Pre x the path with the desired name and a colon.

Example

SELECT beer: s.beer.name, s.price FROM Bars b, b.beersSold s WHERE b.name = "Joe's Bar"

has typ e:

Bag(Struct( beer: string, price: real ))

Change the Collectio n Typ e

 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 Bar"

 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 Bar" 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 = joeMenu[1].name;