SLIDE 1 ODL Sub classes F
w name
sub class b y colon and its sup erclass. Example: Ales are Beers with a Color class Ales:Beers { attribute string color; }
jects
the Ales class acquire all the attributes and relationships
the Beers class.
E/R en tities can ha v e manifestations in a class and sub class, in ODL w e assume eac h
ject is a mem b er
exactly
class. 1
SLIDE 2 Keys in ODL Indicate with key(s) follo wing the class name, and a list
attributes forming the k ey .
eral lists ma y b e used to indicate sev eral alternativ e k eys.
aren theses group mem b ers
a k ey , and also group key to the declared k eys.
us, (key(a 1 ; a 2 ; : : : ; a n )) = \one k ey consisting
all n attributes." (key a 1 ; a 2 ; : : : ; a n ) = \eac h a i is a k ey b y itself." Example class Beers (key name) { attribute string name ...
ememb er : Keys are
in ODL. The \ob ject ID" suces to distinguish
jects that ha v e the same v alues in their elemen ts. 2
SLIDE 3 Example: Multiple Multiattribut e Keys class Courses (key (dept, number), (room, hours)) { ... 3
SLIDE 4 T ranslating ODL to Relations 1. Classes without relationships: lik e en tit y set, but sev eral new problems arise. 2. Classes with relationships: a) T reat the relationship separately , as in E/R. b) A ttac h a man y-one relationship to the relation for the \man y ." 4
SLIDE 5 ODL Class Without Relationships
ODL allo ws attribute t yp es built from structures and collecti
t yp es.
Mak e
attribute for eac h eld.
mak e
tuple for eac h mem b er
the set.
✦
More than
set attribute? Mak e tuples for all com binations.
ODL class ma y ha v e no k ey , but w e should ha v e
in the relation to represen t \OID." 5
SLIDE 6 Example class Drinkers (key name) { attribute string name; attribute Struct Addr {string street, string city, int zip} address; attribute Set<string> phone; } name street cit y zip phone n 1 s 1 c 1 z 1 p 1 n 1 s 1 c 1 z 1 p 2
the k ey for the class (name) is not the k ey for the relation (name, phone).
✦
name in the class determines a unique
ject, including a set
phones.
✦
name in the relation do es not determine a unique tuple.
✦
Since tuples are not iden tical to
jects, there is no inconsistency!
violati
separate
name-phone. 6
SLIDE 7 ODL Relationships
the relationship is man y-one from A to B , put k ey
B attributes in the relation for class A.
relationship is man y-man y , w e'll ha v e to duplicate A-tuples as in ODL with set-v alued attributes.
✦
W
y
really rather create a separate relation for a man y-man y- relationship?
✦
Y
wind up separating it an yw a y , during BCNF decomp
7
SLIDE 8 Example class Drinkers (key name) { attribute string name; attribute string addr; relationship Set<Beers> likes inverse Beers::fans; relationship Beers favorite inverse Beers::realFans; relationship Drinkers husband inverse wife; relationship Drinkers wife inverse husband; relationship Set<Drinkers> buddies inverse buddies; } Drink ers(name, addr, b eerName , fa vBeer, wife, budd y) 8
SLIDE 9 Decomp
in to 4NF
name!addr fa vBeer wife
name! ! b eerName, name! ! buddy
decomp
Drinkers(name , addr, favBeer, wife) DrBeer(name , beer ) DrBuddy(name , buddy) 9
SLIDE 10 OQL Motiv atio n:
languages suer from imp e danc e mismatch when w e try to connect them to con v en tional languages lik e C
C++.
✦
The data mo dels
C and SQL are radically dieren t, e.g. C do es not ha v e relations, sets,
bags as primitiv e t yp es; C is tuple-at-a-time, SQL is relation-at-a- time.
is an attempt b y the OO comm unit y to extend languages lik e C++ with SQL-lik e, relation-at-a-time dictions. 10
SLIDE 11 OQL T yp es
t yp es: strings, in ts, reals, etc., plus class names.
yp e constructors:
✦
Struct for structures.
✦
Collecti
t yp es: set, bag, list, arra y .
e ODL, but no limit
the n um b er
times w e can apply a t yp e constructor.
and Bag(Struct()) pla y sp ecial roles akin to relations. 11
SLIDE 12 OQL Uses ODL as its Sc hema-Denition P
ev ery class w e can declare an extent = name for the curren t set
jects
the class.
✦
Remem b er to refer to the exten t, not the class name, in queries. 12
SLIDE 13 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; } 13
SLIDE 14 P ath Expressions Let x b e an
ject
class C .
a is an attribute
C , then x:a = the v alue
a in the x
ject.
r is a relationship
C , then x:r = the v alue to whic h x is connected b y r .
✦
Could b e an
ject
a collect i
jects, dep ending
the t yp e
r .
m is a metho d
C , then x:m(
is the result
applying m to x. 14
SLIDE 15 Examples Let s b e a v ariable whose t yp e is Sell.
= the price in the
ject s.
= the address
the bar men tioned in s.
✦
Note: cascade
dots OK b ecause s.bar is an
ct, not a collecti
Example
Illegal Use
Dot b.beersSold.price, where b is a Bar
ject.
y illegal ? Because b.beersSold is a set
jects, not a single
ject. 15
SLIDE 16 OQL Select-F rom-Where SELECT <list
v alues> FROM <list
collecti
and t ypical mem b ers> WHERE <condition>
in FROM can b e: 1. Exten ts. 2. Expressions that ev aluate to a collecti
wing a collecti
is a name for a t ypical mem b er,
preceded b y AS. Example Get the men u at Jo e's. SELECT s.beer.name, s.price FROM Sells s WHERE s.bar.name = "Joe's Bar"
double-quoted strings in OQL. 16
SLIDE 17 Example Another w a y to get Jo e's men u, this time fo cusing
the Bar
jects. SELECT s.beer.name, s.price FROM Bars b, b.beersSold s WHERE b.name = "Joe's Bar"
that the t ypical
ject b in the rst collecti
FROM is used to help dene the second collecti
T ypical Usage
x is an
ject, y
can extend the path expression, lik e s
s.beer in s.beer.name.
x is a collect i
y
use it in the FROM list, lik e b.beersSold ab
e, if y
w an t to access attributes
x. 17
SLIDE 18 T ailoring the T yp e
the Result
bag
structs, eld names tak en from the ends
path names in SELECT clause. Example SELECT s.beer.name, s.price FROM Bars b, b.beersSold s WHERE b.name = "Joe's Bar" has result t yp e: Bag(Struct( name: string, price: real )) 18
SLIDE 19 Rename Fields Prex 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 t yp e: Bag(Struct( beer: string, price: real )) 19
SLIDE 20 Change the Collectio n T yp e
SELECT DISTINCT to get a set
structs. Example SELECT DISTINCT s.beer.name, s.price FROM Bars b, b.beersSold s WHERE b.name = "Joe's Bar"
ORDER BY clause to get a list
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
= ascending (default); DESC = descending.
e can extract from a list as if it w ere an arra y , e.g. cheapest = joeMenu[1].name; 20