Sei sulla pagina 1di 64

Object –Oriented Database

New Database Application

The Object-Oriented Data Model

Object-Oriented Language

Persistent Programming Languages


Persistent C++ System

Nested Relation

Inheritance
Reference Type

Querying With Complex Type


Function and Procedure

Comparison of Object-Oriented and Object Relational Database


Object Oriented Database
New Database Application
• Data models designed for data-processing-style applications are not
adequate for new technologies.
• Computer-aided design, Multimedia and image data base.

• New Application requirement the database system to handle feature

Data Encapsulation

Novel methods for indexing and querying


The Object Oriented model
Object-Oriented Data Model
• Objects corresponds to an entity in the E-R model

• Object-Oriented paradigm is based on encapsulating code

• Object-Oriented data model is a logical model

• Adaptation of the Object-oriented Programming paradigm

eg: C++
Object Structure
• An object has associated with it

 A set of variables that contain the data for the object.

 A set of message to which the object responds

 A set of methods, each of which is a body of code to implement a


message
• The physical representation of data is visible

• Message and responses provide the only external interface


Message and Methods
• The team message does not necessarily imply physical message
passing.
• Methods are program written in a general-purpose language
 Only variable in the object

 Data in other object are referenced only

• Every Attribute of an entity must be represented by a variable

E.g., The attribute address is represented by a variable address


and two message get-address and self-address
Object Classes
• Similar object are grouped into a class. It is called an instance of its
class
• Object in a class have the same
 Variable type
 Message interface

 Method
E.g:
Group object for people into a person class
Classes are analogous to entity sets in the E-R
model
Class Definition Example
Class employee{
/*variables*/
String name;
String address;
date start-date;
int salary;
/*Message */
int annual-salary();
String get-name():
String get-address();
int set-address(String new-address);
int employment-length();
};
• Strict encapsulation,methods to read and set other variables are also needed
• Employment-length is an example of a derived attribute
Object Oriented languages
Object-Oriented Languages
• Object-oriented concepts can be used as a design tool, and be encoded

e.g relational database


• The object orientation can be incorporated into a programming
language
• It used to manipulate the database

Object-relational systems-add complex type

 Persistent programming language-extend object-oriented


programming language
Persistent Programming languages
Persistent Programming Languages
• It allow object to be created and stored in a database without any
explicit format changes
• Object to be manipulated in memory

• The program language without having to go through a data


manipulation language like SQL
• It is most power of programming languages

• Complexity of languages makes automatic high –level optimization

• Do not support declarative querying very well


Persistence of objects
• Class-declare all objects of class is simple but inflexible

• Creation-extend the syntax for creating transient objects

• Marking-beyond program execution, before program termination

• Reference-declare(root) object
Object Identity and Pointers
• Object is assigned a object identifier.

• Intraprocedure - only during the execution

• Intraprogram- only during the execution single process or query

• Interprogram- one program execution to another

• Persistent- it throughout program executions and structural


reorganization of data
• In O-O languages such as c++,it is actually an in memory pointer
• Persistent pointer-program execution; can be thought of as a pointer
into the database
Storage and Access of Persistent Objects
How to find object in the database:
 Name objects-cannot scale to large number of objects

-only to class extends and other collection of objects,but not to


objects
 Expose object identifiers or persistent pointers

-All object have object identifiers


 Store collection of object allows program to iterate over the
collection of required object
-Collection type

-Class extent- the collection of all objects


Persistent C++ System
Persistent C++ System
• C++ language support to be added without changing the language

Declare a class called Persistent_Object

Overloading

• Without extending the c++ language

-Relatively easy to implement

-Difficult to use
ODMC C++ Object Definition Language
• C++ to support persistence

• Provided most functionality via template classes and class libraries

• class Ref<class>-Specify references


• Set<class>-sets of objects.

• Eg: use notation inverse to specify referential integrity constraints


ODMG C++ ODL:Example
class Person : public Persistent_Object{
public:

String name;
String address;

};
class Customer:public Person{

public:
Date member_from;
int customer_id;

Ref<Branch>home_branch;
Set<Ref<Account>> accounts inverse Account::owners;

};
ODMG C++ Object Manipulation
Language
• C++ operator new(db)

Ref<Account> account=new(bank_db) Account;


• new –specified database

• Dereference operator -> Ref<Customer>

• Constructor for a class-special method to initialize objects

• Destructor for a class-special method that is called when objects in


the class are deleted
ODMG C++ OML:Example
int create_account_owner(String name,String address){

Database * bank_db;

Bank_db=Database::open(“Bank-DB”);

Transaction Trans;

Trans.begin();

Ref<Account> account=new(bank_db) Account;

Ref<Customer>cust=new(bank_db) Customer;

Cust->name=name;

Cust->address=address;

Cust->accounts.insert_element(account);

account-> owner.insert_element(cust);

}
Object Relational Database
Object Relational databases
• The object-relational data model

• It extends the relational model

• Add constructs to relational query languages

• Declarative access to data

• Extending the modeling power


Nested Relations
Nested Relations
• Motivation
-Permit non-atomic domains
-E.g non-atomic domain
-application with complex data
• Intuitive definition:
-atomic(scalar) values-relation within relations
-mathematical foundation
-violates first normal form
E.g: library information system
1NF Version of Nested Relation
• 1NF Version of books

title author Pub-name Pub-branch keyword

Compilers Smith McGraw-Hill NewYork parsing

Compilers Jones McGraw-Hill NewYork analysis

Networks Jones Oxford London Internet

Network Frick Oxford London Web


4NF Decomposition of Nested Relation

• Remove data in multivalued dependencies

-title->audhor
-title->keyword

-title->pub-name,pub-branch
• Decompose data into 4NF

-(title,audhor)

-(title,keyword)

-(title,pub-name,pub-branch)
4NF Decomposition of data

Title Author Title Author

Compilers Smith Compilers parsing

Compilers Jones Compilers analysis

Networks Frick Networks Internet

Keywords
Authors
Problems with 4NF Schema
• 4NF ->Joins

• 1NF ->eliminates the need for users to perform joins

• One-to-one correspondence

• Large amount of redundancy


Inheritance
Inheritance
• Support the inheritance concepts

• Special relation between objects of different classes "is a", e. g.


Customer Miller is a Person

create type Person

(name varchar(20),address varchar(20))


• Subtype can redefine methods by using overriding method in
place of method in the method declaration
Multiple Inheritance
• Support Multiple Inheritance
• In superclass list, order is important.
– First superclass provides class parameters and members to
subclass.
– Other superclasses provide class members only.
– Member name conflicts resolved in favor of superclass listed
latest (reverse order of superclass list).
Cont..

Define a type
create type Teaching Assistant
under Student,Teacher
• Two occurrences of department
create type Teaching Assistant
under
Student with(departmentas student-dept),
Teacher with(departmentas teacher-dept)
Table Inheritance
• It support multiple type.Entity to exist

Eg: Create table people of Person


• Subtable of people

Eg: Create table student of Student

under people
Create table teachers of Teacher

under people
• Subtable->student and teacher
• Supertables->people
Table Inheritance:Role
• Most-specific type(unlike type inheritance)

e.g: object can be in the student and teachers subtable


simultaneously.
• Object can gain/loss: inserting/deleting object from a subtable
Table inheritance:Consistency
Requirements
• Subtables and Supertables

Each tuple of the supertable(e.g people) can correspond to at most


one tuple in each of subtable(e.g student and teacher)
• Derived from one tuple
Storage Alternatives
• Storage alternative

-only local attributes and primary key


• Each table store all inherited and locally attributes

– Supertables implicitly

– Tuple is faster
Inheritance Operation
• Specialization

– defines subclasses by inheritance of a superclass


– Not all objects of a superclass must be elements of one of its
subclasses.
• Generalization
– combines classes to a collective superclass

– All objects of all subclasses become elements of the superclass

– Often used to define abstract superclasses to simplify


programming, e. g. generalize Branch and ATM to CashLocation
Class Tree and Class Graph
• Specialize subclasses  class tree

animals

vertebrates
invertebrates

mammals

birds
Reference Type
Reference Type
• Object –oriented languages provide create and refer to object.

• Reference are to tuple

• Reference must be scoped


Reference Declaration
• E.g define a type department with a field name and field head

• Type->person
• Table people as scople

Create type Department(


name varchar(20),

head ref(person)scope people)


• Declaration scople people from the instead
Create table department Of Department
(head with option scope people)
Initializing Reference Type
• The Reference type used to ref(p)

• E.g

insert into departments

values(‘cs’,null)

update departments
set head=(select ref(p) from people as p

where name=‘john’)

where name=‘CS’
Query with complex type
Querying with Complex Type
• The name of publisher of each book

select title,publisher.name
from books
• The use dot notation to access fields of the composite attribute

• Collection-valued attributes can be treated relations


• Keyword unnest

Eg
select title from books where ‘database’ in(unnest(keyword-set))
Nesting
• Nesting-> opposite of unnesting

• Similar to aggregation
• Function set()->aggregation operation

• Eg:

select title,audhor,Publisher(pup_name,pub_branch)as publisher,


set (keyword) as keyword-list

from falt-books

groupby title,audhor,publisher
Nesting(cont…)
• Another approach to creating nested relation is to use subqueries in
the select caluse

select title,

(select author from flat-books as M

where M.title=O.title) as author-set,

Publisher(pub-name,pub-branch)as Publisher,

(select keyword from flat-book as N

where N.title=O.title) as keyword- set from flat-book as O


Query Operations
• Class specific operations:

– Each class has its own methods.


– A programmer has to implement these methods.

– Disadvantages:

• A lot of work to do.


• It is not possible to decide whether set of operations is
complete.
• Possibly bad performance.
Cont…
• Generic Operations:

– General database operations provided by the system


– generate class specific operations for every class

– Disadvantage: Encapsulation of objects violated


Generic Query Operations
Operation Object oriented concept
• Projection Supertype
• Selection Subclass (Subset)
• Join Subtype of two types
• Union Superclass (Superset)
• Intersection Subclass of two classes
• Difference Subclass
• Grouping
Defining Class Queries
• SELECT queries only, saved with class.
– New Query toolbar button provides wizard to help build proper
SQL statement
– Build complex statement by hand
.
• Query can use input parameters, to be supplied at runtime
For example:
where amount > :variable
• Code generated when class is compiled.

• %ResultSet class provides OOP access to any class query.


Function and Procedure
Function and Procedure
• Function/procedure can be written in SQL itself

• Specialized datatype(images and geometric objects)

– Eg: function to check is polygons overlap

• Some database support table-valued function

• Set of imperative constructs


loops,if-then-else
SQL Function
• Define a function of book title,return the count of the number of
author
Eg:
create function author-count(name varchar(20))

returns integer

begin
declare a-count integer;

select count(audhor) into a-count from authors where


author.title=name return a=count;
end
SQL Methods
• Method can be viewed as function associated with structured type

• They have an implicit first parameter called Self

• Eg

– Self.a
SQL Function and Procedure(cont..)
• The author-count function
• create procedure author-count-pro(in title varchar(20),out a-count
integer)
begin
select count(author) into a-count
from authors
where author.title=title
End
• Procedure from embedded sql
• Eg
declare a-count integer
call author-count-proc(‘database systems concepts’,a-count);
External language Function/Procedures
• Function and procedure written in c and c++

• Create procedure author-count-proc(in title varchar(20),out count


integer)

language C;

external name ‘usr/avi/bin/author-count-proc’


• Create function author-count(title varchar(20))

return integer

language c
external name ‘usr/avi/bin/author-count’
Benefits &Drawbacks
• Benefits:

more efficient for many operation

more expressive power


• Drawbacks

code to implement function may need to be loaded into database


system

risk of accidental

security risk
Procedural constructs
• Supports a rich variet of procedural construct
• Compound statement
– Is of the from begin..end
– May contain multiple SQL statement
– Begin and end
– Local variable declared in compound statement
Eg:
While and repeat statements
declare n integer default 0;
while n<10 do
set n=n+1
End while repeat set n=n-1 until n=0 End repeat
• For loop

Eg:
declare n integer default 0;

for r as

select balance from account


where branch-name=‘Perryride’

do

set n=n+r.balance
end for
Comparison of object oriented and object
relational databases
Comparison of O-O and O-R Database
• Relational systems
 simple data type

 powerful query language


 high protection
Cont…
• Presistent-programming-language-based OODBs

 Complex data type

 Integration with programming language

 High performace

• Object-relational System

 Complex data type

 Powerful query language

 High protection

Potrebbero piacerti anche