Object Relational Database Systems:: 2. in 3. Comparison of and Approaches
Object Relational Database Systems:: 2. in 3. Comparison of and Approaches
1. Introduction
2. Objects in SQL3
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
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
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
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.
ORDBS 12
Abstract data type (ADT)
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
);
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
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