Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.

Similar presentations


Presentation on theme: "1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i."— Presentation transcript:

1 1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i Oracle9i Application Developer's Guide - Object-Relational Features, Release 2 (9.2) http://cgweb1.unn.ac.uk/SubjectAreaResources/database/oracle/doc/ appdev.920/a96594/toc.htm By Dr. Akhtar Ali Extensions to Relational Databases

2 2 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Oracle Object-Relational Features User Defined Object Types (UDOT) –A type having attributes and methods Objects are instances of UDOTs. Type Inheritance –You can specialize an object type by creating subtypes that have some added, differentiating feature, e.g. attributes or/and methods Object Tables –An object table is a special kind of table in which each row represents an object. Object Views –An object view is a way to access relational data using object-relational features. –It lets you develop object-oriented applications without changing the underlying relational schema.

3 3 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Oracle Object-Relational Features … REF Data type –A REF is a logical "pointer" to a row object. REF s and collections of REF s model associations among objects, particularly many-to-one/many-to-many relationships. –REF s provide an easy mechanism for navigating between objects. Collections –Two collection data types: varrays and nested tables –A varray is an ordered collection of elements: the position of each element has an index number, and you use this number to access particular elements. –A nested table can have any number of elements: no maximum is specified in the definition of the table; also, the order of the elements is not preserved. –Elements of a nested table are actually stored in a separate storage table.

4 4 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) User Defined Object Types (UDOT) An Object type declaration is like a record or tuple construct. Methods can be associated with a UDOT. Object types can be used as Domain for attributes. CREATE TYPE PointType AS OBJECT ( x NUMBER, y NUMBER ); / CREATE TYPE LineType AS OBJECT ( end1 PointType, end2 PointType, MEMBER FUNCTION length(scale IN NUMBER) RETURN NUMBER, PRAGMA RESTRICT_REFERENCES(length, WNDS)); / Methods are defined separately. Pragma is a directive to the compiler. WNDS means that this method does not modify database tables.

5 5 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Methods on a UDOT Methods –Methods body is written in PL/SQL. CREATE TYPE BODY LineType AS MEMBER FUNCTION length(scale NUMBER) RETURN NUMBER IS BEGIN RETURN scale * SQRT((SELF.end1.x - SELF.end2.x) * (SELF.end1.x - SELF.end2.x) + (SELF.end1.y - SELF.end2.y) * (SELF.end1.y - SELF.end2.y) ); END; / –Methods can be invoked from PL/SQL or SQL queries (if free from side-effect defined by Pragma clause).

6 6 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Object Tables Tables can be created that store collections of objects. CREATE TYPE Movie AS OBJECT ( Title varchar(15), Year number(4,0), Lenght number(3, 0)); / CREATE TABLE Movie_Tab OF Movie (PRIMARY KEY (Title)); –Such a table can be viewed as: A single column table in which each entry is a Movie object. A multi-column table in which each attribute of Movie is a column. –Objects that appear in object tables are called row objects. –Objects that appear as attributes of other objects or in table columns are called column objects.

7 7 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Nested Tables A nested table is an unordered set of data elements that are all of the same data type. It has a single column of built-in type or UDOT. First define a type to be table of a some built-in type or UDOT. CREATE TYPE MovieTabTyp AS TABLE OF Movie; –Then define a column or attribute in another table or type of this new nested table type. CREATE TABLE Star ( Name varchar(15), …, Movies MovieTabTyp DEFAULT MovieTabTyp() ) NESTED TABLE Movies STORE AS Star_Movie; –Tuples in the Movies column are stored in a separate storage table called Star_Movie.

8 8 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Nested Tables … Querying Nested Tables –This query returns values that may not be very useful for users. SELECT S.Name, S.Movies FROM Star S WHERE S.Name = 'Fisher' –We can Unnest the nested table to get some useful values. SELECT S.Name, M.Title, M.Year FROM Star S, TABLE (S.Movies) M WHERE S.Name = 'Fisher' –The above query returns the following values.

9 9 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) References To Objects Nested tables do not support sharing of objects and Relationships are value-based. References provide OID based 1:1 and 1:many relationships with object-relational features. CREATE TYPE PointTP AS OBJECT ( id integer, x NUMBER, y NUMBER ); / CREATE TABLE Points OF PointTP ( Primary Key (id) ); / CREATE TABLE Lines2 ( end1 REF PointTP, end2 REF PointTP) / –end1 and end2 attributes store references (pointers) to PointTP objects.

10 10 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Collections of References Many:Many relationships can be represented by nested tables of references. References provide OID based 1:1, many:1, 1:many, many:many relationships with object-relational features. Create Type Movie_ref_typ as Object ( MovieRef REF Movie); / Create Type Movie_set_typ as Table of Movie_ref_typ; / CREATE TABLE Star ( Name varchar(15), …, Movies Movie_set_typ) NESTED TABLE Movies STORE AS Star_Movie; –Type Movie_ref_typ is an object with a single attribute that is a reference to an object of type Movie. –Now Movies is a nested table that contain references to Movie objects.

11 11 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Populating Collections INSERT INTO TABLE ( SELECT S.Movies FROM Star S WHERE S.Name = 'Hamill') SELECT REF(m) FROM Movie_Tab m WHERE m.Title in ('Star Wars', 'Empire', 'War on Terror') / –The first select finds the nested table –The second select returns Reference to the movie objects that are then stored in the Movies nested table.

12 12 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Querying Collections using DEREF

13 13 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Querying Collections using Cursor

14 14 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Querying Collections using Unnesting

15 15 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Type Inheritance You can specialize the attributes or methods of a subtype in these ways: –Add new attributes that its parent supertype does not have. –Add entirely new methods that the parent does not have. –Change the implementation of some of the methods a subtype inherits from its parent so that the subtype's version executes different code from the parent's. FINAL and NOT FINAL Types and Methods –A type defined as NOT FINAL can be specialized. –By default all UDOTs are FINAL –By default all Methods are NOT FINAL –If you want to create a subtype of an UDOT defined as FINAL, you can do so using ALTER TYPE.

16 16 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Type Inheritance: Examples A FINAL Type Definition CREATE TYPE Person_typ AS OBJECT ( NINUM CHAR(9), NAME VARCHAR2(30), ADDRESS VARCHAR2(100)) FINAL; Changing a type from FINAL to NOT FINAL ALTER TYPE Person_typ NOT FINAL; NOT FINAL type with a FINAL Method CREATE TYPE T AS OBJECT (..., MEMBER PROCEDURE Print(), FINAL MEMBER FUNCTION foo(x NUMBER)... ) NOT FINAL; Subtypes CREATE TYPE Student_typ UNDER Person_typ ( DEPTID NUMBER, MAJOR VARCHAR2(30)) NOT FINAL; CREATE TYPE PartTimeStudent_typ UNDER Student_typ ( NUMOFHOURS NUMBER(2));


Download ppt "1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i."

Similar presentations


Ads by Google