30
ORDBS 1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

Embed Size (px)

Citation preview

Page 1: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 1

Object Relational Database Systems:

1. Introduction

2. Objects in SQL3

3. Comparison of ODL/OQL and SQL3 Approaches

CIS 671

Page 2: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 2

Object Query Language (OQL)• “Bring the best of SQL to the object world.”• Syntax similar to SQL• Plus additional features• Works with programming languages where

ODMG has defined bindings– Java, C++, Smalltalk– Returns object matching type system of that language– May implement class operations in these languages

• SQL3 – “Bring the best of the object-oriented world to the relational world.”

Page 3: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 3

Overview: Object Relational DB SystemsSQL3

• Interfacing SQL to a programming language.– Cursors

• Data Types– Built-in– User Defined

• Operations

Page 4: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 4

Data Types – Built-in• smallint – 16-bit integer• integer – 32-bit integer• decimal(p,s) – decimal

number, precision p, scale s• float – 32-bit float• double – 64-bit float

• date – year, month, day• time – hour, minute, second• timestamp - year, month,

day, hour, minute, second, microsecond

• interval – year/month or day/time

• char(n) – fixed length, n ≤ 254• varchar(n) – varying length

– n ≤ 254 – 254 ≤ n ≤ 4000; no group by, etc.

• bit(n)• varbit(n)• boolean [SQL3]• Large Objects [SQL3]

– blob(binary large object)• ≤ 231 –1 bytes

– clob(character large object)• ≤ 231 –1 bytes

Domaine.g., create domain

SSN_TYPE as char(9)

not used much.

Page 5: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 5

Objects in SQL3

• Row objects, tuples or structs, and

• Abstract data types (ADT), general objects, used as components of tuples.

Page 6: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 6

Moviestitleyearlength /* in minutes */filmType:{color,

blackAndWhite}lengthInHoursstarNamesotherMovies

Movies Example: Movies, Stars, StudiosODL Version

Starsnameaddress

streetcity

Studiosname

stars starredIn

ownedBy owns

Set of stars of this movie.

Other movies by star of this movie.

Page 7: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 7

Movies Example: Movies, Stars, StudiosER Version

Movie MovieStarStarsInN M

Page 8: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 8

Row Types [SQL3 or SQL:1999]• Row-type Definition:

create row type T (<component-declaration-list>)

• Examples:create row type AddressType(

street char(50),city char(20));

create row type StarType(name char(30),address AddressType

);

create table MovieStar of type StarType;

select MovieStar..name, MovieStar..address..street

from MovieStarWhere MovieStar..address..city = ‘Columbus’;

Cannot directly represent set of

movies starred-in.

Double dotPath Exp.

Page 9: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 9

References • SQL3’s way to represent “objects”.

create row type StarType(name char(30),address AddressType );

create row type MovieType(title char(30),year integer );

create row type StarsInType(star ref(StarType),movie ref(MovieType) );

create table Movie of type MovieType;create table MovieStar of type StarType;create table StarsIn of type StarsInType;

select movie -> titlefrom StarsInwhere star->name = ‘Mel Gibson’

Dereferencing operator (->)

Movie MovieStarStarsInN M

Page 10: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 10

Object Identifiers• May be explicitly declared and accessed.• Can serve as both object ID and primary key.

create row type MovieType(movie_id ref(MovieType),title char(30),year integer );

create row type StarType(star_id ref(StarType),name char(30),address AddressType );

create table Movie of type MovieTypevalues for movie_id are system generated;

create table MovieStar of type StarTypevalues for star_id are system generated;

Page 11: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 11

Specifying Scope to Aid Performancecreate row type StarsInType(

star ref(StarType),movie ref(MovieType) );

create table StarsIn of type StarsInTypescope for star is MovieStar,scope for movie is Movie;

select movie -> titlefrom StarsInwhere star->name = ‘Mel Gibson’

How to find movies?Examine every StarsIn tuple.

Index on name attribute.

Could have another relation with StarType.

Page 12: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 12

Objects in SQL3

• Row objects, tuples or structs (REVIEW), and

• Abstract data types (ADT), general objects, used as components of tuples.

Page 13: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 13

Abstract data type (ADT)

• Used as components of tuples, – Not as tuples themselves.

• Have tuple structure.

Page 14: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 14

ADT - “Built-in” Functions• Constructor functions returning new object of type.

– All attributes initially null.– If T is name of ADT, then T() is constructor.

• Observer functions for each attribute.– If A is attribute name & X is variable whose value is an

object of the ADT, then A(X) (or X.A) is the value of attribute A of object X.

• Mutator functions for each attribute.– Sets the value of that attribute to a new value.– Normally used on left side of assignment

Page 15: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 15

Example: Address ADT

create type AddressADT (street char(50),city char(20),equals addrEq,less than addrLT

other functions could be declared here);

Addresses will be encapsulated• Access to street & city allowed only if observer

and mutator functions made public.• Functions addrEQ and addrLT defined later.

Page 16: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 16

More interesting example: ADT for Mpegs

create type Mpeg (video blob,length integer,copyright varchar(255),equals default, /* i.e., identity */less than none

definitions of MPEG functions go here);

Page 17: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 17

Defining Methods for ADT’s

• Function types– Internal

• Written in SQL.

– External• Written in C++, Java, etc.

• Only signature appears in definition of the ADT.

Function <name> ( <arguments> ) returns <type> ;

Page 18: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 18

Extended SQL • := used as assignment operator.• Variable local to the function can be declared by

giving its name, preceded by a colon and followed by its type.

:schar(50) /* s will be a street */

• Dot operator used to access components of a structure:a.street := :s /* a, an address */

• Boolean values can be expressed as in where clauses.

• begin and end are used to collect several statements into the body of a function.

Page 19: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 19

Some Functions for the ADT AddressADT:1. Constructor Function

function AddressADT (:s char(50), :c char(20))returns AddressADT;

:a AddressADT; /* declare local variable */

begin:a := AddressADT(); /* use built-in constructor */

:a.street := :s;:a.city := :c;return :a;

end;

Note: We can use the same name, AddressADT, as the default constructor.

Page 20: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 20

More Functions for the ADT AddressADT:

2. equals (addrEq )and less than (addrLT) Functions

function addrEq (:a1 AddressADT, :a2 AddressADT)returns boolean;

return ( :a1.street = :a2.street and:a1.city = :a2.city);

function addrLT (:a1 AddressADT, :a2 AddressADT)returns boolean;

return ( (:a1.city < :a2.city) or(:a1.city = :a2.city and :a1.street < :a2.street));

Page 21: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 21

Another Function for the ADT AddressADT:

function fullAddr (:a AddressADT)returns char(82);

:z char(10); /* for the zip code nnnnn-nnnn*/

begin:z := findZip(:a.street, :a.city );return (:a.street || ‘ ‘ || :a.city || ‘ ‘ || :z);

end;

3. fullAddr, function returning the full address, including zip code, as a single character string

findZip, an externally defined function which looks up the zip code for a given city and street.

Page 22: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 22

External Functions• ADT Definition must include:

– Signature.– Specification of programming language in

which function is written.

declare external findZipchar(50), char(20) returns char(10)language Java;

Arguments passed according to Java conventions.

Page 23: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 23

Comparison of ODL/OQL and SQL3 Approaches

• Similarities outweigh differences, even though origins different:– ODL/OQL: object-oriented programming languages.– SQL3: relational database languages.

• Have effectively adopted ideas from each other.

Page 24: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 24

Comparing ODL/OQL vs. SQL3actually

ODL/OQL vs. SQL3 row types vs. SQL3 ADT types

1. Programming environment.

2. Role of relations.

3. Encapsulation.

4. Extents of classes.

5. Mutability of objects.

6. Object identity.

Page 25: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 25

ODL/OQL vs. SQL3 row types vs. SQL3 ADT types

1. Programming environment

• OQL– Assumes statements embedded in OO programming

language, C++, Java, …

• SQL3– Objects not objects of surrounding programming

language.– External functions in SQL3 ADT’s provide

additional flexibility.

Page 26: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 26

ODL/OQL vs. SQL3 row types vs. SQL3 ADT types

2. Role of relations

• OQL– Sets and bags of objects or structures are central.

• SQL3– Relations are central.– Row types describe relations.– ADT’s describe new types for attributes.

• Collections of structures in ODL/OQL similar to Relations in SQL3.

Page 27: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 27

• SQL3 Row Types– Not encapsulated.

– Querying & modifying relations, tuples, and components allowed.

• SQL3 ADT’s– Encapsulated in the usual sense.

• ODL Classes– Similar to SQL3 ADT’s in encapsulation

ODL/OQL vs. SQL3 row types vs. SQL3 ADT types

3. Encapsulation

Page 28: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 28

ODL/OQL vs. SQL3 row types vs. SQL3 ADT types

4. Extents for Classes

• OQL– Single extent maintained for each class.

– Thus references (relationships in OQL) always refer to some member or members of this extent.

• SQL3– An extent for a row type allowed, but not required.

• If no extent for a row type, then may be problem finding relation containing referenced tuple.

Page 29: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 29

ODL/OQL vs. SQL3 row types vs. SQL3 ADT types

5. Mutability of Objects

• Immutable Object: once created, no part of its values can change.– Objects of elementary type, e.g., integers or strings, are immutable.

• Mutable Object: components may change, while object retains its identity.

• ODL Classes & SQL3 Row Types– Define classes of mutable objects.– ODL/OQL: modification occurs through surrounding programming

language, not through OQL.

• SQL3 ADT’s– Mutator functions applied to their values result in new value, which may

replace old one.• Similar to SQL update statement on integer-valued attribute produces a new

integer that might replace the old integer in the tuple.

Page 30: ORDBS1 Object Relational Database Systems: 1. Introduction 2. Objects in SQL3 3. Comparison of ODL/OQL and SQL3 Approaches CIS 671

ORDBS 30

ODL/OQL vs. SQL3 row types vs. SQL3 ADT types

6. Object Identity

• OQL & SQL3 ADT’s– “Standard” OID: system generated, which

cannot be stored or manipulated by user.

• SQL3 Row Type– User can create “primary key”.– Without this relations would usually have two

“keys”:• OID• Surrogate value, e.g., Employee_ID.