0% found this document useful (0 votes)
124 views14 pages

Object-Relational Features in Oracle Database!: View of The Database World!

The document discusses object-relational features in Oracle Database 11g. It describes key object-oriented concepts like objects, methods, classes, and inheritance. It then explains how these concepts are implemented in Oracle using object types, object tables, and member methods. The advantages of using an object-relational database management system are also summarized.

Uploaded by

جيم جيم
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views14 pages

Object-Relational Features in Oracle Database!: View of The Database World!

The document discusses object-relational features in Oracle Database 11g. It describes key object-oriented concepts like objects, methods, classes, and inheritance. It then explains how these concepts are implemented in Oracle using object types, object tables, and member methods. The advantages of using an object-relational database management system are also summarized.

Uploaded by

جيم جيم
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

8/11/13

View of the Database World!

Object-Relational Features in
Oracle Database!

1
2

8/11/13

Object-Relational Elements in Oracle 11g! Object-Oriented Concepts!

D Object-Oriented Concepts! D Abstraction and Encapsulation (Provided by Abstract Data Types


(ADT))!
D Objects! Abstraction is the process of identifying the essential aspects of an entity
and ignoring the unimportant properties. Focus on what an object is and
D Methods! what it does, rather than how it should be implemented.!
Encapsulation (or information hiding) provides data independence by
D Object Tables! separating the external aspects of an object from its internal details, which
D Type Inheritance! is hidden from the outside world.!
D Classes!
D Collections! Classes: A class is a blueprint or prototype from which objects are created.
D Object Types and References! A group of objects with the same attributes and methods. Hence, the
attributes and the associated methods are defined once for the class rather
than separately for each object.!
Attributes (or instance variables) describe the current state of an object
(the notation for attribute: object-name.attribute-name).!

3
4

8/11/13

Object-Oriented Concepts! Advantages of ORDBMS!


Methods: define the behavior of the object. !They can be used to change D Enables reuse and sharing.!
the objects state by modifying its attribute values, or to query the value of u !Abilityto extend the DBMS server to perform standard functionality
the selected attributes. A method consists of a name and a body that centrally, rather than have it coded in each application. Example:
performs the behavior associated with the method name (notation: object- Embedded Functions, it saves having to define it in each application
name.method-name).! that needs it.!


Attributes

D Ability and support for complex objects and rich data types,
termed abstract data types (ADTs)!
u !Complex applications such as Oracle Spatial!
D The instances of a class are those objects belonging to a class.!
D Support for Inheritance!
u !Inherent attributes and behavior of the pre-existing classes, hence ease of
definition and programming!

5
6

8/11/13

Oracle Object Types! Oracle Objects!

D User-Defined data types (classes)! D Definition!


D Consist of 2 parts: attributes + methods! u !Actual instance of the defined object type,!
CREATE TYPE!person_type AS OBJECT (! u !Storages are allocated to an object and values are assigned to the
name! VARCHAR2(30),! attributes of an object!
phone! VARCHAR2(20), !-- attributes!declared.!


CREATE TABLE contacts (!
MEMBER FUNCTION get_areacode RETURN VARCHAR2 ); -- method! contact person_type,
/ -- This slash needed to get Oracle process this statement.! c_date! DATE );!
-- object type! can be used like any other built-in data types.!
--Defining an object type does not allocate any storage.!

INSERT INTO contacts VALUES (!
--The body of method is defined in a separate CREATE!
person_type(Tommy Trojan, '213-740-1114), -- instance
--TYPE BODY statement, written in PL/SQL or any other languages.!
24 Jan 2004 );!
-- person_type is instantiated and values are assigned to!
DROP TYPE person_type;!
-- the attributes of the object instance.!
--First drop all tables and other types using person_type.!

7
8

8/11/13

Oracle Methods! Member Method!


D Member methods are used to access an object instances values.!
D Definition!
CREATE OR REPLACE TYPE BODY person_type AS!
u !Functions/procedures declared in the object type definition to MEMBER FUNCTION get_areacode RETURN VARCHAR2 IS
implement behavior of the object of that type.! BEGIN!
RETURN SUBSTR(phone, 1, 3);!
u !Written in PL/SQL or virtually any other languages (Java, C)! END get_areacode;!
END;!
/!
D Method types! -- Define the body of the method using CREATE OR REPLACE! TYPE!BODY.!

u !Member method! SELECT c.contact.get_areacode()
FROM contacts c;!
D Defined on object instances data.!
-- Invoke a member method!
u !Static method!
D Invoked on the object type, not its instances.! C.CONTACT.GET_AREACODE()!
D Can be used to the operations that are global to the type (e.g. initialization)! ----------------------------------------------------------------------
213!
u !Constructor method!
D Built-in constructor function, like in C++.!

9
10

8/11/13

Constructor Method! Oracle Object Tables!


D Every object type has a constructor method implicitly defined by D Object Table: special type of table, each row represents an object!
system.! CREATE!TYPE person_type AS OBJECT
D Returns a new instance of the user-defined object type and sets up the name ( VARCHAR2(30),!
values of its attributes.! phone! VARCHAR2(20) );!
D The name of constructor method is the same as the name of the object /!
type.! CREATE TABLE person_table OF person_type;!


INSERT INTO person_table!
VALUES (person_type (Scott Tiger, 321-123-1234));
p = person_type(Scott Tiger, 321-123-1234);! SELECT VALUE(p) FROM person_table p WHERE p.name = Scott! Tiger;!
--Built-in constructor method, person_type(att1, att2) is invoked! -- Single-column table: each row is a person_type object!
--to create a new object instance of person_type, specify values! -- Perform object-oriented operations!
--for its attributes(name, phone), and set the object into a!
--variable p.!
D Comparing to a relational table!

CREATE TABLE person_table (!
INSERT INTO contacts! name VARCHAR2(30),
VALUES! (person_type(Scott Tiger, 321-123-1234), phone! VARCHAR2(20) );!
10 Feb 2004));! INSERT INTO person_table!
--Same! thing occurs here.! VALUES (Tommy Trojan, 213-740-1212);!
SELECT name, phone FROM person_table;!
-- Multi-column table: treat person_table as a relational! table!
11
12

8/11/13

Methods to Compare Objects (1)! Methods to Compare Objects (2)!


D Define a special kind of member methods to compare objects.! u !Order Method!
D Provides direct object-to-object comparison, telling that the current object is less than, equal
D Define either a map method or an order method in an object !type.! to, or greater than the other object.!
u !Map Method!
CREATE or REPLACE TYPE!circle_type AS OBJECT
D Map object instances into one of the scalar types DATE, CHAR, NUMBER,!
x ( NUMBER,!
y NUMBER,
CREATE!TYPE!circle_type AS OBJECT (! r! NUMBER,!
x! NUMBER,! ORDER MEMBER FUNCTION match(c circle_type)! RETURN!INTEGER ); !/!
y! NUMBER,!
r! NUMBER,! CREATE OR REPLACE TYPE BODY circle_type AS
MAP MEMBER FUNCTION get_area RETURN NUMBER ); /! ORDER MEMBER FUNCTION match (c circle_type) RETURN!INTEGER IS!
BEGIN!

IF r < c.r THEN -- 3.14*r2 < 3.14*c.r2!
CREATE TYPE BODY circle_type AS!
RETURN 1;! -- any negative number
MAP MEMBER FUNCTION get_area RETURN NUMBER IS
ELSIF r > c.r THEN!
BEGIN!
RETURN 1;! -- any positive number!
RETURN 3.14 * r * r;! ELSE!
END get_area; RETURN 0;
END; /! END IF;!

END;!
SELECT * FROM circles c! END; -- returns only one integer value among positive, 0, and negative.!
ORDER BY VALUE(c);! --Result should be ordered!by circles area!
13
14

8/11/13

Methods to Compare Objects (3)! OO Concepts - Inheritance!


CREATE!TABLE!circles!OF!circle_type;!
D Subclasses: A class of objects that is defined as a special
INSERT!INTO!circles!VALUES!(circle_type(10,!10,!3));!
case of a more general class (the process of forming
subclasses is called specialization).!
INSERT!INTO!circles!VALUES!(circle_type(40,!20,!8));!
D Superclass: A class of objects that is defined as a general
INSERT!INTO!circles!VALUES!(circle_type(10,!50,!4));! case of a number of special classes (the process of forming
a superclass is called generalization). All instances of a
SELECT!c.x,!c.y! subclass are also instances of its superclass.!
FROM circles!c D Inheritance: By default, a subclass inherits all the
WHERE VALUE(c)!< (circle_type(40, 25,!5)) ;!
properties of its superclass (or it can redefine some (or all)
CIRCLES.X! CIRCLES.Y! of the inherited methods). Additionally, it may define its own
------------- ---------------- unique properties.!
10! 10!
10! 50!

15
16

8/11/13

OO Concepts - Inheritance! OO Concepts - Inheritance!

D Single inheritance: When a subclass inherits D Overriding: To redefine an inherited property by defining
from no more than one superclass (note: forming the same property differently at the subclass level.!
class hierarchies is permissible here).! D Overloading: A general case of overriding where the same
D Multiple inheritance: When a subclass inherits method name is reused within a class definition (overriding)
from more than one superclass (note: a or across class definitions. !Hence, a single message can
mechanism is required to resolve conflicts when perform different functions depending on which object
the Superclasses have the same attributes and/or receiving it and, if appropriate what parameters are passed
methods). !Due to its complexity, not all OO to the method (e.g., print method for different objects).!
languages and database systems support this D Polymorphism: Having many forms in Greek, is a
concept.! The image general case of overloading.!
cannot be

D Repeated inheritance: A special case of multiple displayed.


Your u !Inclusion polymorphism: Same as overriding.!
computer may
inheritance where the multiple Superclasses not have
enough u !Operation (or ad hoc) polymorphism: Same as overloading.!
inherit from a common superclass (note: must memory to
open the
image, or the u !Parametric polymorphism (or Genericity): It uses types as
ensure that subclasses do not inherit properties image may
parameters in generic type (or class) definition.!
multiple times).!
17
18

8/11/13

Oracle Type Inheritance (1)! Oracle Type Inheritance (2)!

D Supertype/Subtype! CREATE OR!REPLACE TYPE person_type AS OBJECT


u !Subtype is derived from a parent object type, Supertype.! ssn ( NUMBER,!
D Subtype inherits all attributes and methods from its supertype.! name VARCHAR2(30),
address! VARCHAR2(20)) NOT FINAL; /!

D Example! --To permit subtype, object type should be! defined!as NOT FINAL.!
Person! --By default, an object type is FINAL!

CREATE TYPE!student_type UNDER person_type!(!


deptid! NUMBER,!
Student! Employee! major! VARCHAR2(30)) NOT FINAL; /!

CREATE TYPE!employee_type UNDER person_type


empid ( NUMBER,!
mgr! VARCHAR2(30)); /!

CREATE TYPE!part_time_student_type UNDER student_type!(!


Part-time Student!
numhours! NUMBER ); /!

19
20

8/11/13

Oracle Type Inheritance! Oracle Type Inheritance (3)!


D Overloading/Overriding methods!
CREATE TABLE persons OF person_type;! CREATE TYPE Shape_typ AS OBJECT (...,!

MEMBER PROCEDURE Enlarge(x! NUMBER),!
...) NOT FINAL; /!
INSERT INTO persons VALUES (student_type(123, 'Tommy', CREATE TYPE Circle_typ UNDER Shape_typ (...,!
'PHE-306', 1, 'cs'));! MEMBER PROCEDURE Enlarge(x! CHAR(1))); /!
INSERT INTO persons VALUES (employee_type(789,'Trojan', --Define the inherited method Enlarge() to deal! with!different!types of!
--input parameters.!
'PHE-314', 888, 'Cyrus'));!
INSERT INTO persons VALUES (456, Mike,'PHE-314);! CREATE TYPE Shape_typ AS OBJECT (...,
MEMBER PROCEDURE Area(),!
FINAL MEMBER FUNCTION id(x NUMBER)...!
SELECT * FROM persons;!
) NOT FINAL; /!
SELECT!VALUE(p) FROM persons p WHERE VALUE(p) IS OF! CREATE TYPE Circle_typ UNDER Shape_typ (...,
(employee_type);! OVERRIDING MEMBER PROCEDURE Area(),!
...); /!
SELECT TREAT(VALUE(p) AS student_type).major FROM --Redefine an inherited method Area() to make it do something different!
persons p WHERE VALUE(p) IS OF (student_type);! --in the subtype.!

21
22

8/11/13

Oracle Collections! Inserting/Querying Collections!


INSERT!INTO contacts!
D Set of data elements!
VALUES!(people_type(person_type(Tommy!Trojan, 213-740-1234),!
u !VArray - ordered set of data elements.! person_type(Scott!Tiger, 321-123-1234)),!
SELECT! 12 Feb 2004);!

CREATE TYPE phones AS VARRAY(3) of VARCHAR2(20); /!
* FROM contacts;!
--Each element has an index, corresponding to its position in! CONTACT(NAME, PHONE) !C_DATE!
--the array! ---------------------------------------------------------------------------
PEOPLE_TYPE(PERSON_TYPE('Tommy Trojan', '213-740-1234'), PERSON_TYPE('Scott!

Tiger', '321-123-1234')) !12-FEB-04!
u !Nested Table - unordered set of data elements!

SELECT p.phone, c.c_date FROM contacts c, TABLE(c.contact) p;!
CREATE TYPE people_type AS TABLE OF person_type; /!
PHONE! C_DATE!
--Declare the table type used for a nested table.! -------------------- ---------
213-740-1234! 12-FEB-04!
CREATE TABLE! contacts (! 321-123-1234! 12-FEB-04!
contact people_type,!
SELECT p.phone! FROM TABLE(SELECT c.contact FROM contacts c) p;!
c_date! DATE )!
-- result(?)!
NESTED TABLE!contact STORE AS people_table;!
--Declare a nested table!

23
24

8/11/13

Oracle Object Types and References! Oracle Object Types and Reference!
D REF Datatype! D Querying to a REF!
u !REF is a logical "pointer" to a row object.!
D For an object type t, REF t is the reference to the values of type t.!
SELECT *!
CREATE TABLE! contacts (! FROM contacts;!

contact REF person_type,
c_date! DATE );! CONTACT !C_DATE!
--contact attribute is a reference to the values of person_type.! --------------------------------------------------------------
0000220208D28EEDE1C5736BD7E034080020B68B64 !12-JAN-04!
CREATE TABLE person_table OF person_type;!
--create object table consisting of person_type objects!
SELECT c.contact.name, c.c_date
INSERT INTO person_table VALUES (Tommy Trojan, 213-740-1212); FROM contacts c;!

INSERT INTO contacts! --using dot notation to follow the reference.!


SELECT REF(p), 12 Jan 2004!
FROM person_table p! CONTACT.NAME! C_DATE!
where p.name like %Tommy%;! --------------------! ---------
Tommy Trojan! 12-JAN-04!

25
26

8/11/13

References!

D For more information,!


u !Online Oracle 11g Documentations
http://www.oracle.com/technology/documentation/database11g.html!


u !A.R. 4: Application Developers Guide Object-Relational Features!

27

You might also like