0% found this document useful (0 votes)
26 views

Object Relational Database Systems:: 2. in 3. Comparison of and Approaches

Uploaded by

abcdeb
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Object Relational Database Systems:: 2. in 3. Comparison of and Approaches

Uploaded by

abcdeb
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Object Relational Database Systems:

1. Introduction

2. Objects in SQL3

3. Comparison of ODL/OQL and SQL3 Approaches

CIS 671
ORDBS 1
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.”
ORDBS 2
Overview: Object Relational DB Systems
SQL3
• Interfacing SQL to a programming language.
– Cursors
• Data Types
– Built-in
– User Defined
• Operations

ORDBS 3
Data Types – Built-in
• smallint – 16-bit integer • char(n) – fixed length, n ≤ 254
• integer – 32-bit integer • varchar(n) – varying length
– n ≤ 254
• decimal(p,s) – decimal – 254 ≤ n ≤ 4000; no group by, etc.
number, precision p, scale s • bit(n)
• float – 32-bit float • varbit(n)
• double – 64-bit float • boolean [SQL3]
• Large Objects [SQL3]
– blob(binary large object)
• date – year, month, day • ≤ 231 –1 bytes
• time – hour, minute, second – clob(character large object)
• ≤ 231 –1 bytes
• timestamp - year, month,
day, hour, minute, second, Domain
microsecond e.g., create domain
• interval – year/month or SSN_TYPE as char(9)
day/time not used much.
ORDBS 4
Objects in SQL3
• Row objects, tuples or structs, and
• Abstract data types (ADT), general objects,
used as components of tuples.

ORDBS 5
Movies Example: Movies, Stars, Studios
ODL Version
Stars
Movies name
stars starredIn
title address
year street
length /* in minutes */ city
filmType:{color,
blackAndWhite}
lengthInHours ownedBy owns Studios
starNames name
otherMovies

Set of stars of this movie.

Other movies by star of this movie.


ORDBS 6
Movies Example: Movies, Stars, Studios
ER Version

N M
Movie StarsIn MovieStar

ORDBS 7
Row Types [SQL3 or SQL:1999]
• Row-type Definition:
create row type Double dot
T (<component-declaration-list>) Path Exp.
• Examples:
create row type AddressType(
street char(50),
city char(20));
create row type StarType(
name char(30), Cannot directly
address AddressType represent set of
movies starred-in.
);
create table MovieStar of type StarType;
select MovieStar..name, MovieStar..address..street
from MovieStar
ORDBS Where MovieStar..address..city = ‘Columbus’; 8
References • SQL3’s way to represent “objects”.
N M
Movie StarsIn MovieStar

create row type StarType( create row type MovieType(


name char(30), title char(30),
address AddressType ); year integer );
create row type StarsInType(
star ref(StarType),
movie ref(MovieType) );
create table Movie of type MovieType; Dereferencing
create table MovieStar of type StarType; operator (->)
create table StarsIn of type StarsInType;
select movie -> title
from StarsIn
ORDBS where star->name = ‘Mel Gibson’ 9
Object Identifiers
• May be explicitly declared and accessed.
• Can serve as both object ID and primary key.

create row type StarType( create row type MovieType(


star_id ref(StarType), movie_id ref(MovieType),
name char(30), title char(30),
address AddressType ); year integer );
create table Movie of type MovieType
values for movie_id are system generated;
create table MovieStar of type StarType
values for star_id are system generated;

ORDBS 10
Specifying Scope to Aid Performance
create row type StarsInType(
How to find movies?
star ref(StarType),
movie ref(MovieType) ); Examine every StarsIn tuple.

select movie -> title


from StarsIn
Index on name attribute. where star->name = ‘Mel Gibson’
Could have another
relation with StarType.

create table StarsIn of type StarsInType


scope for star is MovieStar,
scope for movie is Movie;
ORDBS 11
Objects in SQL3
• Row objects, tuples or structs (REVIEW),
and
• Abstract data types (ADT), general objects,
used as components of tuples.

ORDBS 12
Abstract data type (ADT)

• Used as components of tuples,


– Not as tuples themselves.
• Have tuple structure.

ORDBS 13
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

ORDBS 14
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.
ORDBS 15
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
);

ORDBS 16
Defining Methods for ADT’s
Function <name> ( <arguments> ) returns <type> ;

• Function types
– Internal
• Written in SQL.
– External
• Written in C++, Java, etc.
• Only signature appears in definition of the ADT.

ORDBS 17
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.
:s char(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.

ORDBS 18
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.
ORDBS 19
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));
ORDBS 20
Another Function for the ADT AddressADT:
3. fullAddr, function returning the full address,
including zip code, as a single character string

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;
findZip, an externally defined
function which looks up the zip code
for a given city and street.
ORDBS 21
External Functions
• ADT Definition must include:
– Signature.
– Specification of programming language in
which function is written.

declare external findZip


char(50), char(20) returns char(10)
language Java;

Arguments passed according to


Java conventions.

ORDBS 22
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.

ORDBS 23
Comparing ODL/OQL vs. SQL3
actually
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.

ORDBS 24
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.

ORDBS 25
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.
ORDBS 26
ODL/OQL vs. SQL3 row types vs. SQL3 ADT types
3. Encapsulation
• 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

ORDBS 27
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.

ORDBS 28
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.

ORDBS 29
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.
ORDBS 30

You might also like