Sei sulla pagina 1di 13

Object Relational Model

The object-relational modelcombines features of the relational and object-oriented models.


This model provides the rich type system of object-oriented databases, combined with relations
as the basis for storage of data.It applies inheritance to relations, not just to types. The object-
relational data model provides a smooth migration path from relational databases, which is
attractive to relational database vendors.

An object-relational database (ORD), or object-relational database management system


(ORDBMS), is a database management system (DBMS) similar to a relational database, but with
an object-oriented database model: objects, classes and inheritance are directly supported in
database schemas and in the query language. In addition, just as with pure relational systems, it
supports extension of the data model with custom data-types and methods.

An object-relational database can be said to provide a middle ground between relational


databases and object-oriented databases (OODBMS). In object-relational databases, the approach
is essentially that of relational databases: the data resides in the database and is manipulated
collectively with queries in a query language; at the other extreme are OODBMSes in which the
database is essentially a persistent object store for software written in an object-oriented
programming language, with a programming API for storing and retrieving objects, and little or
no specific support for querying.

Query in SQL vs. Query in Object Relational Database

Object Definition Language

In Relational Database, DDL is used to specify the database schemas, similarly, the ODL
allows you to specify a logical schema foran object-oriented database. ODL is a programming-
language-independent specificationlanguage for defining OODB schemas. Just as an SQL DDL
schema is portableacross SQL-compliant relational DBMSs, an ODL schema is portable across
ODMGcompliantODBMSs.

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


In ODL, a class is specified using the class keyword, and an attribute is specified using
the attribute keyword. The Student and Course classes are defined as follows:

UML:CD for University Course registration

Sample ODL Schema for above UML: CD

class Student {
attribute string name;
attributeDate dateOfBirth;
attribute string address;
attribute string phone;
short age( );
floatgpa( );
// relationship between Student and CourseOffering
relationship set<CourseOffering> takes inverse CourseOffering::taken_by;
// operations
booleanregister_for(string crse, short sec, string term);.
};

class Course {
attribute string crse_code;
attribute string crse_title;
attribute short credit_hrs;
// plus relationships and operation . . .
};

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


classCourseOffering {
attribute string term;
attributeenum section {1, 2, 3, 4, 5, 6, 7, 8};
// many-to-many relationship between CourseOffering and Student
relationship set<Student>taken_by inverse Student::takes;
// one-to-many relationship between CourseOffering and Course
relationship Course belongs_to inverse Course::offers;
//operation
short enrollment( )
};
Creating an Object Instances

When a new instance of a class is created, a unique object identifier is assigned.


You may specify an object identifier with one or more unique tag names. For example,
we can create a new course object called IT8001 as follows:

IT8001 course ( );

This creates a new instance of Course. The object tag name, IT8001, can be used
to reference this object. We have not specified the attribute values for this object at this
point. Suppose you want to create a new student object and initialize some of its
attributes.

Raihana student (name: “Raihana”, dateOfBirth: 4/5/93);

You can also specify the values for the attributes within a structure, as in the following
example:

Koushikastudent (
name: “Koushika”, dateOfBirth: 2/12/93,
address: {street_address “310 College Rd”, city “Chennai”, state “TN”, zip 600043},
phone: {personal_number123456789});

OQL

We will now describe the Object Query Language (OQL), which is similar to
SQL-92 and has been set forth as an ODMG standard for querying OODBs. OQL allows
you a lot of flexibility in formulating queries. You can write a simple query such as

Kumutha.dateOfBirth;
which returns Kumutha’s birth date, a literal value, or
PadmaPriya.address;
This returns a structure with values for street address, city, state, and zip. If instead we
want to simply find in which city Roshini resides, we can write
Roshini.address.city;
Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU
OQL-Basic Select Command

We have specified the attributes crse_title and credit_hrs for the extent (i.e., set of all
Course instances in the database) in the select clause, and stated the condition
c.crse_code = “IT8001” in the where clause.
selectc.crse_title, c.credit_hrs
from courses c
wherec.crse_code = “IT8001”

Because we are dealing with only one extent, we could have left out the variable c
without any loss in clarity.

selects.age
from students s
where s.name = “Muthulakshmi”

The below query returns a bag of object instances,

select s
from students s
wheres.gpa> = 8.0 and s.address.city ! = “Chennai”

Creating Object Types

You create an object type using the CREATE TYPE statement. The following example
uses the CREATE TYPE statement to create an object type named t_address. This object type is
used to represent an address and contains four attributes named street, city, state, and zip:

CREATE TYPE t_address AS OBJECT (


street VARCHAR2(15),
city VARCHAR2(15),
state CHAR(2),
zip VARCHAR2(5)
);

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


The next example creates an object type named t_person; notice that t_person has an
attribute named address, which is of type t_address:

CREATE TYPE t_person AS OBJECT (


id INTEGER,
first_name VARCHAR2(10),
last_name VARCHAR2(10),
dob DATE,
phone VARCHAR2(12),
addresst_address
);
Describe command to get object information

You can use the DESCRIBE command to get information on an object type. The
following examples show the t_address, t_person types:

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


Using Object types in DB tables
You can use an object type to define an individual column in a table, and the objects
subsequently stored in that column are known as column objects. You can also use an object type
to define an entire row in a table; the table is then known as an object table. Finally, you can use
an object reference to access an individual row in an object table;The following example creates
a table named products that contains a column named product of type t_product; the table also
contains a column named quantity_in_stock, which is used to store the number of those products
currently in stock:

A constructor is a built-in method for the object type, and it has the same name as the
object type; the constructor accepts parameters that are used to set the attributes of the new
object. The constructor for the t_product type is named t_product and accepts five parameters,
one to set each of the attributes; for example, t_product(1, pasta, 20 oz bag of pasta, 3.95, 10)
creates a new t_product object and sets its id to 1, name to pasta, description to 20 oz bag of
pasta, price to 3.95, and days_valid to 10.

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


The following INSERT statements add two rows to the products table; notice the use of
the t_product constructor to supply the attribute values for the product column objects:

The following query retrieves these rows from the products table; notice that the product
column objects’ attributes are displayed within a constructor for t_product:

Select * from product;

You can also retrieve an individual column object from a table; to do this, you must
supply a table alias through which you select the object. The following query retrieves product
#1 from the products table; notice the use of the table alias p for the products table, through
which the product object’s id attribute is specified in the WHERE clause:

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


The t_product object type contains a function named get_sell_by_date(), which calculates
and returns the date by which the product must be sold. The function does this by adding the
days_valid attribute to the current date, which is obtained from the database using the
SYSDATE() function. You can call the get_sell_by_date() function using a table alias, as shown
in the following query that uses the table alias p for the products table:

The following UPDATE statement modifies the description of product #1; notice that the table
alias p is used again:

The following DELETE statement removes product #2:

Tables as Objects
You can use an object type to define an entire table, and such a table is known as an
object table. The following example creates an object table named object_products, which stores
objects of type t_product; notice the use of the OF keyword to identify the table as an object
table of type t_product:

CREATE TABLE object_products OF t_product;

When inserting a row into an object table, you can choose whether to use a constructor to
supply attribute values or to supply the values in the same way that you would supply column
values in a relational table.

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


// Inserting into table without using t_product constructor

INSERT INTO object_products (


id, name, description, price, days_valid
) VALUES (
2, 'sardines', '12 oz box of sardines', 2.99, 5
);
// Inserting into table using t_product constructor

INSERT INTO object_products VALUES (


t_product(1, 'pasta', '20 oz bag of pasta', 3.95, 10)
);

The following query retrieves these rows from the object_products table:

SELECT * FROM object_products;

Type Inheritance in OODB

Oracle Database 9i introduced object type inheritance, which allows you to define
hierarchies of object types. For example, you might want to define a business person object type
and have that type inherit the existing attributes from t_person. The business person type could
extend t_person with attributes to store the person’s job title and the name of the company they
work for. For t_person to be inherited from, the t_person definition must include the NOT
FINAL clause:

The NOT FINAL clause indicates that t_person can be inherited from when defining another
type.

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


To have a new type inherit attributes and methods from an existing type, you use the
UNDER keyword when defining your new type. Our business person type, which I’ll name
t_business_person, uses the UNDER keyword to inherit the attributes from t_person:

Table Inheritance

In this example, t_person is known as the supertype, and t_business_person is known as


the subtype. You can then use t_business_person when defining column objects or object tables.
For example, the following statement creates an object table named object_business_customers:

CREATE TABLE object_business_customers OF t_business_person;

The following INSERT statement adds an object to object_business_customers; notice


that the two additional title and company attributes are supplied at the end of the
t_business_person constructor:

More Examples
Definition of People object

We may want to store extra information in the database about people who are students,
and about people who are teachers.

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


Object Database Management Group (ODMG)

The ODMG is an informal consortium of ODBMS vendors working on standards to allow


portability of customer software across their products. By supporting the same interface, they expect
to significantly accelerate progress towards effective adoption of the standard.

The primary goal of the ODMG was to put forward a set of specifications that allowed a
developer to write portable applications for object database and object-relational mapping products.
In order to do that, the data schema, programming language bindings, and data manipulation and
query languages needed to be portable.

Major components of the ODMG

 Object Model. This was based on the Object Management Group's Object Model. The OMG core
model was designed to be a common denominator for object request brokers, object database
systems, object programming languages, etc. The ODMG designed a profile by adding
components to the OMG core object model.
 Object Specification Languages. The ODMG Object Definition Language (ODL) was used to
define the object types that conform to the ODMG Object Model. The ODMG Object
Interchange Format (OIF) was used to dump and load the current state to or from a file or set of
files.
 Object Query Language (OQL). The ODMG OQL was a declarative (nonprocedural) language
for query and updating. It used SQL as a basis, where possible, though OQL supports more
powerful object-oriented capabilities.
 C++ Language Binding. This defined a C++ binding of the ODMG ODL and a C++ Object
Manipulation Language (OML). The C++ ODL was expressed as a library that provides classes
and functions to implement the concepts defined in the ODMG Object Model. The C++ OML
syntax and semantics are those of standard C++ in the context of the standard class library. The
C++ binding also provided a mechanism to invoke OQL.
 Smalltalk Language Binding. This defined the mapping between the ODMG ODL and Smalltalk,
which was based on the OMG Smalltalk binding for the OMG Interface Definition Language
(IDL). The Smalltalk binding also provided a mechanism to invoke OQL.
 Java Language Binding. This defined the binding between the ODMG ODL and the Java
programming language as defined by the Java 2 Platform. The Java binding also provided a
mechanism to invoke OQL.

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


ODMG hierarchy of languages and steps in the generation of an ODBMS application

Extended Relational Database Management Systems (ERDBMS)


ERDBMSs have characteristics of both an RDBMS and an ODBMS (thus, the loose
application of the ORDBMS label). ERDBMS products provide a relational data model and
query language that have been extended to include many of the features that are typical of
ODBMSs.

Typically, programming capabilities are embedded in the query language. This


capability is not to be confused with stored procedures that are provided by a number of
relational vendors. With the ERDBMSs, programmers are able to write functions in
conventional languages as well as in SQL. These functions can then be embedded in standard
SQL statements in exactly the same manner as a DBMS vendor function (e.g., the Sybase
getdate() function).

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU


Most significantly, these DBMSs have been extended to handle:
· complex data types, which include user defined abstract data types,
· non-tabular structures,
· automatically generated, logical object identifiers,
· tables within tables,
· a type hierarchy,
· multiple inheritance,
· compound objects,
· schema evolution,
· transitive closure operations.

ODL-Defining classes and objects-Covered

OQL-Querying on classes created by ODL-Covered

-End of unit 2
Handouts to follow.

Advances in Database | Semester 5 | M.Tech-IT| Vivekanandan D, Dept of IT, MIT, AU

Potrebbero piacerti anche