View
235
Download
0
Category
Tags:
Preview:
Citation preview
Object-Relational Model
Review: Data Models
• Hierarchical
• Network
• ER
• (Pure) Relational
• (Pure) Object-oriented (ODMG)
• Object-relational (since SQL:1999)
• Semistructured (XML)
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
Review: OO Concepts
• Type sytem– Atomic types– Type constructors: struct, collection, reference
• Classes and Objects
• OID
• Methods
• Class hierarchy
• Inheritance
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
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?
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}))
OO vs OR
• Objects vs. tuples– How is relationship represented?
• Methods
• Type systems
• References and OID’s
• Backwards compatibility
ODL to OR
• More direct than ODL to Relations
• Methods can be translated now.
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.
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.
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
);
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;
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);
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.
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.
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
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
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);
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);
Operations on OR Data
• All SQL applies.
• Some new operations
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’;
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.
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
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.
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 >.
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;
End
Recommended