Download CS 180: Winter 2002 Class Schedule and SQL Recursion in Object-Oriented Databases and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!
Winter 2002^
Arthur Keller – CS 180
Schedule
•^ Today: Feb. 28 (TH)^ ◆^ Datalog and SQL Recursion, ODL.^ ◆^ Read Sections 10.3-10.4, 4.1-4.4. Project Part 6 due.•^ Mar. 5 (T)^ ◆^ More ODL, OQL.^ ◆^ Read Sections 9.1. Assignment 7 due.•^ Mar. 7 (TH)^ ◆^ More OQL.^ ◆^ Read Sections 9.2-9.3.•^ Mar. 12 (T)^ ◆^ Semistructured Data, XML, XQuery.^ ◆^ Read Sections 4.6-4.7. Assignment 8 due.
Winter 2002^
Arthur Keller – CS 180
SQL Recursion
WITH^ stuff that looks like Datalog rulesan SQL query about EDB, IDB• Rule = [RECURSIVE]
R ()
AS
SQL query
Winter 2002^
Arthur Keller – CS 180
Plan for Describing Legal SQL Recursion1.^ Define “monotonicity,” a property that generalizes“stratification.”2.^ Generalize stratum graph to apply to SQL queriesinstead of Datalog rules.^ ◆
(Non)monotonicity replaces
NOT^ in subgoals.
3.^ Define semantically correct SQL recursions in termsof stratum graph.MonotonicityIf relation
P^ is a function of relation
Q^ (and perhaps
other things), we say
P^ is^ monotone
in^ Q^ if adding
tuples to
Q^ cannot cause any tuple of
P^ to be deleted.
Winter 2002^
Arthur Keller – CS 180
Monotonicity Example
In addition to certain negations, an aggregation can causenonmonotonicity.^ Sells(bar, beer,
price)
SELECT AVG(price)FROM SellsWHERE bar =
'Joe''s
Bar';
•^ Adding to
Sells
a tuple that gives a new beer Joe sells
will usually change the average price of beer at Joe’s.• Thus, the former result, which might be a single tuplelike (2.78) becomes another single tuple like (2.81), andthe old tuple is lost.
Winter 2002^
Arthur Keller – CS 180
Example
For the Sib/Cousin example, there are threenodes:
Sib,^
Cousin
, and^ SQ
(the second
term of the union in the rule for
Cousin
• No nonmonotonicity, hence legal.
Sib^
Cousin^ SQ
Winter 2002^
Arthur Keller – CS 180
A Nonmonotonic Example
Change the
UNION^
to^ EXCEPT
in the rule for
Cousin
RECURSIVE
Cousin(x,y)
AS
SibEXCEPT(SELECT
p1.child,
p2.child FROM^ Par
p1,^ Par
p2,^ Cousin WHERE^ p1.parent
=^ Cousin.x AND^ p2.parent
=^ Cousin.y
) • Now, adding to the result of the subquery candelete^ Cousin
facts; i.e.,
Cousin
is
nonmonotone in
SQ.
-^ Infinite number of –’s in cycle, so illegal in SQL.
Sib^
Cousin^ SQ
Winter 2002^
Arthur Keller – CS 180
Object-Oriented DBMS’s• ODMG = Object Data Management Group:an OO standard for databases.• ODL = Object Description Language:design in the OO style.• OQL = Object Query Language: queries anOO database with an ODL schema, in amanner similar to SQL.
Winter 2002^
Arthur Keller – CS 180
ODL Overview
Class declarations include:1.^ Name for the interface.2.^ Key declaration(s), which are optional.3.^ Extent
declaration = name for the set of
currently existing objects of a class.4. Element
declarations. An element is an
attribute, a relationship, or a method.
Winter 2002^
Arthur Keller – CS 180
ODL Relationships
•^ Only binary relations supported.^ ◆^ Multiway relationships require a “connecting” class, asdiscussed for E/R model.•^ Relationships come in inverse pairs.^ ◆^ Example: “Sells” between beers and bars is representedby a relationship in bars, giving the beers sold,
and^ a
relationship in beers giving the bars that sell it.• Many-many relationships have a set type (called a collection type
) in each direction.
•^ Many-one relationships have a set type for theone, and a simple class name for the many.•^ One-one relations have classes for both.
Winter 2002^
Arthur Keller – CS 180
Beers-Bars-Drinkers Example^ class Beers {attribute string name;attribute string
manf;
relationship Set
servedAt
inverse Bars::serves;
relationship Set fans
inverse Drinkers::likes;
} • An element from another class is indicated by:: • Form a set type with
Set<type
Winter 2002^
Arthur Keller – CS 180
class Drinkers {attribute string name;attribute
Struct Bars::Addraddress; relationship Set likes
inverse Beers::fans; relationship Set frequents
inverse Bars::customers; } • Note reuse of
Addr
type.
Winter 2002^
Arthur Keller – CS 180
15–
ODL Type System
•^ Basic types: int, real/float, string, enumeratedtypes, and classes.•^ Type constructors:
Struct
for structures and
four^ collection types
:^ Set,^
Bag,^ List
,^ Array
and^ Dictionary
•^ Relationship types may only be classes or acollection of a class.
Winter 2002^
Arthur Keller – CS 180
Example: Multiway RelationshipConsider a 3-way relationship bars-beers-prices. We have to create a connectingclass BBP.^ class Prices {attribute real price;relationship Set
toBBP inverse BBP::thePrice;} class BBP {relationship Bars
theBar^ inverse ... relationship Beers
theBeer^
inverse ...
relationship Prices
thePrice inverse Prices::toBBP;}
-^ Inverses for
theBar,
theBeer
must be added to
Bars,^ Beers
-^ Better in this special case: make no
Prices^
class; make
price^ an attribute
of^ BBP. • Notice that keys are optional.^ ◆^ BBP^ has no key, yet is not “weak.” Object identity suffices to distinguish different^ BBP^ objects.
Winter 2002^
Arthur Keller – CS 180
Roles in ODL
Names of relationships handle “roles.”Example: Spouses and Drinking Buddies^ class Drinkers {attribute string name;attribute
Struct^
Bars::Addr address; relationship Set likes
inverse Beers::fans; relationship Set frequents
inverse Bars::customers; relationship Drinkers husband
inverse wife; relationship Drinkers wife
inverse husband; relationship Set buddies
inverse buddies; } • Notice that
Drinkers::
is optional when the inverse is a relationship of the same class.