Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object-Relational Model. Review: Data Models Hierarchical Network ER (Pure) Relational (Pure) Object-oriented (ODMG) Object-relational (since SQL:1999)"— Presentation transcript:

1 Object-Relational Model

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

3 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 OORDB ODL/OQL ORDBOODB SQL-99

4 Review: OO Concepts Type sytem –Atomic types –Type constructors: struct, collection, reference Classes and Objects OID Methods Class hierarchy Inheritance

5 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

6 Nested Relations Type of an attribute can be a relation schema. Example Students(sno,name,age,courses(cno,title,credit)) –Redundancy? CTX(course,teachers(name),texts(title)) –MVD?

7 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}))

8 OO vs OR Objects vs. tuples –How is relationship represented? Methods Type systems References and OID’s Backwards compatibility

9 ODL to OR More direct than ODL to Relations Methods can be translated now.

10 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.

11 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.

12 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 );

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

14 Creating a Table using a UDT Syntax CREATE 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. Example CREATE TABLE people OF Person (PRIMARY KEY name);

15 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.

16 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.

17 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 Declaration CREATE TABLE tablename OF typename ( REF IS attribute-name how-generated ); –SYSTEM GENERATED –DERIVED: use the PK

18 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

19 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);

20 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);

21 Operations on OR Data All SQL applies. Some new operations

22 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. Example SELECT DEREF(sref) FROM SC WHERE sref->name = ‘James Bond’;

23 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.

24 Example: Observer Method Example SELECT 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

25 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.

26 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 >.

27 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;

28 End


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

Similar presentations


Ads by Google