001.02 Ullman CS145 Object-Relational DBMS Fall 2004

download 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

of 50

Transcript of 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    1/50

    1

    Object-Relational

    Databases

    User-Defined Types

    Object IDsNested Tables

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    2/50

    2

    Merging Relational and

    Object ModelsxObject-oriented models support

    interesting data types --- not just

    flat files. Maps, multimedia, etc.

    xThe relational model supports

    very-high-level queries.xObject-relational databases are an

    attempt to get the best of both.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    3/50

    3

    Evolution of DBMSs

    xObject-oriented DBMSs failedbecause they did not offer the

    efficiencies of well-entrenchedrelational DBMSs.

    xObject-relational extensions to

    relational DBMSs capture much ofthe advantages of OO, yet retainthe relation as the fundamental

    abstraction.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    4/50

    4

    SQL-99 and Oracle

    FeaturesxSQL-99 includes many of the

    object-relational features to be

    described.xHowever, being so new, different

    DBMSs use different approaches.

    Well sometimes use features andsyntax from Oracle.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    5/50

    5

    User Defined Types

    x A user-defined type, or UDT, isessentially a class definition, with

    a structure and methods.x Two uses:

    As a rowtype, that is, the type of a

    relation. As the type of an attribute of a

    relation.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    6/50

    6

    UDT Definition

    CREATE TYPE AS (

    );

    x Oracle syntax:

    1. Add OBJECT as in CREATE ASOBJECT.

    2. Follow with / to have the type stored.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    7/50

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    8/50

    8

    References

    xIfT is a type, then REF T is thetype of a reference to T, that is, a

    pointer to an object of type T.xOften called an object ID in OO

    systems.

    xUnlike object IDs, a REF is visible,although it is usually gibberish.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    9/50

    9

    Example: REF

    CREATE TYPE MenuType AS (

    bar REF BarType,

    beer REF BeerType,price FLOAT

    );

    x MenuType objects look like:

    3.00

    To a BarTypeobject To a BeerType

    object

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    10/50

    10

    UDTs as Rowtypes

    xA table may be defined to have aschema that is a rowtype, rather

    than by listing its elements.xSyntax:

    CREATE TABLE OF

    ;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    11/50

    11

    Example: Creating a

    RelationCREATE TABLE Bars OF BarType;

    CREATE TABLE Beers OF BeerType;

    CREATE TABLE Sells OF MenuType;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    12/50

    12

    Values of Relations with a

    RowtypexTechnically, a relation like Bars,

    declared to have a rowtype

    BarType, is not a set of pairs --- itis a unary relation, whose tuplesare objects with two components:

    name and addr.xEach UDT has a type constructor

    of the same name that wraps

    objects of that type.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    13/50

    13

    Example: Type

    ConstructorxThe query

    SELECT * FROM Bars;

    xProduces tuples such as:

    BarType(Joes Bar, Maple St.)

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    14/50

    14

    Accessing Values From a

    RowtypexIn Oracle, the dot works as

    expected.

    But it is a good idea, in Oracle, to usean alias for every relation, when O-Rfeatures are used.

    xExample:SELECT bb.name, bb.addr

    FROM Bars bb;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    15/50

    15

    Accessing Values: SQL-99

    ApproachxIn SQL-99, each attribute of a UDT

    has generator (get the value) and

    mutator (change the value)methods of the same name as theattribute.

    The generator forA takes noargument, as A().

    The mutator for A takes a new valueas argument, as A(v).

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    16/50

    16

    Example: SQL-99 Value Access

    xThe same query in SQL-99 is

    SELECT bb.name(), bb.addr()

    FROM Bars bb;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    17/50

    17

    Inserting Rowtype Values

    xIn Oracle, we use a standard INSERTstatement. But remember that a relation with a

    rowtype is really unary and needs that typeconstructor.

    xExample:

    INSERT INTO Bars VALUES(

    BarType(Joes Bar, Maple St.)

    );

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    18/50

    18

    Inserting Values: SQL-99

    Stylex Create a variableX of the

    suitable type, using the

    constructor method for that type.x Use the mutator methods for the

    attributes to set the values of the

    fields ofX.x InsertX into the relation.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    19/50

    19

    Example: SQL-99 Insert

    xThe following must be part of aprocedure, e.g., PSM, so we have a

    variable newBar.SET newBar = BarType();

    newBar.name(Joes Bar);

    newBar.addr(Maple St.);

    INSERT INTO BarsVALUES(newBar);

    Mutator methodschange newBarsname and addrcomponents.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    20/50

    20

    UDTs as Column Types

    xA UDT can be the type of anattribute.

    xIn either another UDT declaration,or in a CREATE TABLE statement,use the name of the UDT as the

    type of the attribute.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    21/50

    21

    Example: Column Type

    CREATE TYPE AddrType AS (

    street CHAR(30),

    city CHAR(20),

    zip INT

    );

    CREATE TABLE Drinkers (

    name CHAR(30),addr AddrType,

    favBeer BeerType

    );

    Values of addr andfavBeer components

    are objects with 3 and2 fields, respectively.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    22/50

    22

    Oracle Problem With Field

    AccessxYou can access a field F of an

    object that is the value of an

    attributeA byA.F.xHowever, you must use an alias,

    say rr, for the relation R with

    attributeA, as rr.A.F.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    23/50

    23

    Example: Field Access in Oracle

    xWrong:

    SELECT favBeer.name

    FROM Drinkers;xWrong:

    SELECT Drinkers.favBeer.name

    FROM Drinkers;

    xRight:

    SELECT dd.favBeer.name

    FROM Drinkers dd;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    24/50

    24

    Following REFs: SQL-99

    Stylex A -> B makes sense if:

    A is of type REF T.

    B is an attribute (component) ofobjects of type T.

    x Denotes the value of the B

    component of the object pointed tobyA.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    25/50

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    26/50

    26

    Following REFs: Oracle

    StylexREF-following is implicit in the dot.

    xJust follow a REF by a dot and a field of

    the object referred to.

    xExample:

    SELECT ss.beer.name

    FROM Sells ss

    WHERE ss.bar.name = Joes Bar;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    27/50

    27

    Oracles DEREF Operator --

    MotivationxIf we want the set of beer objects for

    the beers sold by Joe, we might try:

    SELECT ss.beer

    FROM Sells ss

    WHERE ss.bar.name = Joes Bar;

    xLegal, but ss.beer is a REF, hencegibberish.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    28/50

    28

    Using DEREF

    xTo see the BeerType objects, use:

    SELECT DEREF(ss.beer)

    FROM Sells ss

    WHERE ss.bar.name = Joes Bar;

    x

    Produces values like:BeerType(Bud, Anheuser-Busch)

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    29/50

    29

    Methods --- Oracle Syntax

    xClasses are more than structures;they may have methods.

    xWell study the Oracle syntax.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    30/50

    30

    Method Definitions

    (Oracle)xDeclare methods in CREATE TYPE.

    xDefine methods in a CREATE TYPE

    BODY statement. Use PL/SQL syntax for methods.

    Variable SELF refers to the object to

    which the method is applied.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    31/50

    31

    Example: Method

    Declarationx Lets add method priceInYen to

    MenuType.CREATE TYPE MenuType AS OBJECT (

    bar REF BarType,

    beer REF BeerType,

    price FLOAT,

    MEMBER FUNCTION priceInYen(rate IN FLOAT)RETURN FLOAT,

    PRAGMA RESTRICT_REFERENCES(priceInYen,WNDS)

    );

    /

    What Oracle callsmethods.

    Write no database state.That is, whatever priceInYen doesit wont modify the database.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    32/50

    32

    Method Definition Oracle

    StylexForm of create-body statement:

    CREATE TYPE BODY

    AS

    END;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    33/50

    33

    Example: Method

    DefinitionCREATE TYPE BODY MenuType AS

    MEMBER FUNCTION

    priceInYen(rate FLOAT) RETURN FLOATIS

    BEGIN

    RETURN rate * SELF.price;END;

    END;

    /

    No mode (IN)in body, justin declaration

    Use parentheses onlywhen there is atleast one argument

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    34/50

    34

    Method Use

    xFollow a name for an object by a dotand the name of the method, with

    arguments if any.xExample:

    SELECT ss.beer.name,

    ss.priceInYen(114.0)

    FROM Sells ss

    WHERE ss.bar.name = Joes Bar;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    35/50

    35

    Order Methods: SQL-99

    xEach UDT T may define twomethods called EQUAL and

    LESSTHAN. Each takes an argument of type Tand

    is applied to another object of type T.

    Returns TRUE if and only if the target

    object is = (resp.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    36/50

    36

    Order Methods: Oracle

    xWe may declare any one methodfor a UDT to be an order method.

    xThe order method returns a value0, as the value ofobject SELF is the

    argument object.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    37/50

    37

    Example: Order Method

    DeclarationxOrder BarType objects by name:CREATE TYPE BarType AS OBJECT (

    name CHAR(20),

    addr CHAR(20),

    ORDER MEMBER FUNCTION before(

    bar2 IN BarType) RETURN INT,

    PRAGMA RESTRICT_REFERENCES(before,

    WNDS, RNDS, WNPS, RNPS)

    );

    / Read/write no database state/package state. Apackage is a collection of procedures and variables

    that can communicate values among them.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    38/50

    38

    Example: Order Method

    DefinitionCREATE TYPE BODY BarType AS

    ORDER MEMBER FUNCTION

    before(bar2 BarType) RETURN INT IS

    BEGIN

    IF SELF.name < bar2.name THEN RETURN 1;

    ELSIF SELF.name = bar2.name THEN RETURN0;

    ELSE RETURN 1;END IF;

    END;

    END;

    /

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    39/50

    39

    Oracle Nested Tables

    xAllows values of tuple componentsto be whole relations.

    xIfT is a UDT, we can create a typeS whose values are relations withrowtype T, by:

    CREATE TYPE S AS TABLE OF T;

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    40/50

    40

    Example: Nested Table

    TypeCREATE TYPE BeerType AS OBJECT (

    name CHAR(20),

    kind CHAR(10),color CHAR(10)

    );

    /

    CREATE TYPE BeerTableType AS

    TABLE OF BeerType;

    /

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    41/50

    41

    Example --- Continued

    xUse BeerTableType in a Manfs relationthat stores the set of beers by each

    manufacturer in one tuple for thatmanufacturer.

    CREATE TABLE Manfs (

    name CHAR(30),

    addr CHAR(50),

    beers beerTableType

    );

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    42/50

    42

    Storing Nested Relations

    xOracle doesnt really store eachnested table as a separate relation

    --- it just makes it look that way.xRather, there is one relation R in

    which all the tuples of all the

    nested tables for one attributeAare stored.

    xDeclare in CREATE TABLE by:

    NESTED TABLEA STORE AS R

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    43/50

    43

    Example: Storing Nested

    TablesCREATE TABLE Manfs (

    name CHAR(30),

    addr CHAR(50),

    beers beerTableType

    )

    NESTED TABLE beers STORE ASBeerTable;

    Note where the semicolongoes and doesnt go.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    44/50

    44

    Querying a Nested Table

    x We can print the value of anested table like any other value.

    x But these values have two typeconstructors:

    1. For the table.

    2. For the type of tuples in the table.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    45/50

    45

    Example: Query a Nested Table

    xFind the beers by Anheuser-Busch:

    SELECT beers FROM Manfs

    WHERE name = Anheuser-Busch;xProduces one value like:

    BeerTableType(

    BeerType(Bud, lager, yellow),BeerType(Lite, malt, pale),

    )

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    46/50

    46

    Querying Within a Nested Table

    xA nested table can be converted toan ordinary relation by applying

    THE().xThis relation can be used in FROM

    clauses like any other relation.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    47/50

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    48/50

    48

    Turning Relations Into Nested

    TablesxAny relation with the proper

    number and types of attributes can

    become the value of a nestedtable.

    xUse CAST(MULTISET() AS

    ) on the relation to turn it into thevalue with the proper type for anested table.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    49/50

    49

    Example: CAST --- 1

    xSuppose we have a relationBeers(beer, manf), where beer is a

    BeerType object and manf a string--- the manufacturer of the beer.

    xWe want to insert into Manfs a new

    tuple, with Petes Brewing Co. asthe name and a set of beers thatare whatever Beers has for Petes.

  • 8/14/2019 001.02 Ullman CS145 Object-Relational DBMS Fall 2004

    50/50

    50

    Example: CAST --- 2

    INSERT INTO Manfs VALUES (

    Petes, Palo Alto,

    CAST(

    MULTISET(SELECT bb.beer

    FROM Beers bb

    WHERE bb.manf = Petes

    ) AS BeerTableType

    )

    );

    The set of BeerTypeobjects for Petes

    Turn the set of objectsinto a nested relation