Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure)...

28
Object-Relational Model

Transcript of Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure)...

Page 1: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Object-Relational Model

Page 2: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Review: Data Models

• Hierarchical

• Network

• ER

• (Pure) Relational

• (Pure) Object-oriented (ODMG)

• Object-relational (since SQL:1999)

• Semistructured (XML)

Page 3: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Two Approaches to OO+DB

• The OO community: pure OO

• The SQL community: bring the best of OO into RDB world.– SQL:1999, SQL:2003

OO RDBODL/OQL

ORDBOODB

SQL-99

Page 4: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Review: OO Concepts

• Type sytem– Atomic types– Type constructors: struct, collection, reference

• Classes and Objects

• OID

• Methods

• Class hierarchy

• Inheritance

Page 5: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

From Relation to Object-Relation

• Complex type system for attributes– Structure and set/bag– Esp.: bag of structs = relation

Nested relation

• Methods

• Tuple ID: tuples play the role of objects, and have OID’s.– Hidden, but can be made visible to users.

• References: pointers to tuples

Page 6: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Nested Relations

• Type of an attribute can be a relation schema.

• ExampleStudents(sno,name,age,courses(cno,title,credit))– Redundancy?

CTX(course,teachers(name),texts(title))– MVD?

Page 7: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

References

• Type of an attribute can be a reference to a tuple with a given schema or a set of references to tuples with a given schema.– A(*R): A’s type is a reference to a tuple with schema R.– A({*R}): A’s type is a set of references to tuples with

schema R.

• Eliminate redundancy in nested-relations.• Example

Course(cno,title,credit)

Student(sno,name,age,courses({*Course}))

Page 8: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

OO vs OR

• Objects vs. tuples– How is relationship represented?

• Methods

• Type systems

• References and OID’s

• Backwards compatibility

Page 9: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

ODL to OR

• More direct than ODL to Relations

• Methods can be translated now.

Page 10: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

User-Defined Types in SQL

• UDT in SQL = Classes in ODL

• UDT is used in two ways– A UDT can be the type of a relation; i.e., the

type of its tuples.Sometimes called a row type.

– A UDT can be the type of an attribute in a relation.

Page 11: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

UDT Declaration

• Renaming of an existing type:CREATE TYPE type-name AS primitive-type;

– To enforce stronger type checking.

• Class-like UDT:CREATE TYPE type-name

AS (list of attribute decalarations)

list of methods;– Don’t treat relationship as property, but represent it by

a separate relation or through reference.

Page 12: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Example

CREATE TYPE HeightType AS INTEGER;

CREATE TYPE WeightType AS INTEGER;

CREATE TYPE AddressType AS (

street CHAR(50),

city CHAR(20)

);

CREATE TYPE Person AS (

name CHAR(30),

address AddressType

);

Page 13: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Methods in UDT

• Need both a declaration and a definition• Example CREATE TYPE AddressType AS ( street CHAR(50), city CHAR(20)) METHOD houseNum( ) RETURNS CHAR(10);

CREATE METHOD houseNum( ) RETURNS CHAR(10) FOR AddressType BEGIN …PSM language … END;

Page 14: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Creating a Table using a UDT

• SyntaxCREATE TABLE table-name OF UDT-name

(list-of-elements);– Tuples must be thought of as objects.– List-of-elements: key, FK, Check constraints.

For the table, not for the UDT.

• ExampleCREATE TABLE people OF Person

(PRIMARY KEY name);

Page 15: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Typed Table

• A table that is declared to be based on some structured type is called a typed table.– its columns correspond in name and declared

type to the attributes of the structured type.

• Typed tables have one additional column, called the “self-referencing column” whose type is a reference type associated with the structured type of the table.

Page 16: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Subtable vs. Supertable

• If a typed table TB1 has an associated structured type TP1 that is a subtype of some other structured type TP2, then TB1 can be defined to be a “subtable” of a typed table TB2 whose associated type is TP2; TB2 is, in this case, a “supertable” of TB1.

Page 17: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Reference Column

• UDT tables may have a reference column that serves as the “identity” for its tuples, like OID. The table is then referenceable.– Primary key of the table– Generated and maintained unique by the DBMS

• DeclarationCREATE TABLE tablename OF typename (

REF IS attribute-name how-generated );– SYSTEM GENERATED– DERIVED: use the PK

Page 18: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

References

• For an attribute A to refer to tuples of a table R of UDT T with a reference column:

A REF(T) SCOPE R– if no scope is specified, the reference can go to

any relation of T

Page 19: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Example

CREATE TYPE Person AS ( ... );

CREATE TABLE people OF Person (

REF IS pID SYSTEM GENERATED,

PRIMARY KEY (name));

CREATE TYPE Department AS (

name CHAR(30),

head REF(Person) SCOPE people);

Page 20: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Many-Many Relationships

• Suppose we have declared:CREATE TYPE StudentType AS …;

CREATE TABLE students OF StudentType;

CREATE TYPE CourseType AS …;

CREATE TABLE courses OF CourseType;

Then we may represent the many-many relationship between students and courses by references:CREATE TABLE SC (

sref REF(StudentType) SCOPE students,

cref REF(CourseType) SCOPE courses);

Page 21: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Operations on OR Data

• All SQL applies.

• Some new operations

Page 22: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Following References

• If x is of type REF(T), then x refers to some tuple t of type T.– x->a is the value of attribute a in t.– DEREF(x) is the tuple t.

• ExampleSELECT DEREF(sref)

FROM SC

WHERE sref->name = ‘James Bond’;

Page 23: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Accessing Components of Tuples

• Every UDT has an implicitly defined observer method x( ) for each attribute x.

• If t is variable of type T, then t.x( ) is the value of x in the tuple (object) denoted by t.

Page 24: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Example: Observer Method

• ExampleSELECT s.name()

FROM students s

WHERE s.sno() = ‘007’;– Tuple (object) variable is needed.– ( ) of observer method may be omitted in

practice.Not for other methods.

Lu Chaojun, SJTU

Page 25: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Generator and Mutator Methods

• When a UDT T is defined, two other methods are defined automatically:– Generator method: T( ) returns an object of

type T, with no values in its various components.

– Mutator method: t.x(v) sets v to attribute x of tuple t.

Page 26: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Ordering Relationships on UDT

• To specify an ordering or comparison on UDT objects.1. CREATE ORDERING FOR T

EQUALS ONLY BY STATE;Equality of corresponding components

2. CREATE ORDERING FOR T

EQUALS ONLY BY RELATIVE WITH F;Equality by test F(x1,x2) = 0.

3. CREATE ORDERING FOR T

ORDERING FULL BY RELATIVE WITH F;Ordering by testing F(x1,x2): <0 for <, =0 for =, >0 for >.

Page 27: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

Example

CREATE ORDERING FOR AddressType

ORDERING FULL BY RELATIVE WITH AddrLEG;

CREATE FUNCTION AddrLEG(

x1 AddressType,

x2 AddressType

) RETURNS INTEGER

IF x1.city() < x2.city() THEN RETURN(-1)

ELSEIF x1.city() > x2.city() THEN RETURN(1)

ELSEIF x1.street() < x2.street() THEN RETURN(-1)

ELSEIF x1.street() = x2.street() THEN RETURN(0)

ELSE RETURN(1)

END IF;

Page 28: Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)

End