Upload
others
View
10
Download
1
Embed Size (px)
Citation preview
Object-Oriented
DatabasesFoivos Gkoulis
Applications
●Business applications.
●Complex navigational
applications.
●Multimedia applications.
Business applications
●Large amounts of data
●Simple structure
●More or less complex queries.
Complex navigational applications
●Manipulate data with complex structure and/or
relationships
●Efficiently traverse such relationships.
●For example telecommunications.
Multimedia applications
●Store and retrieve images, texts and spatial data.
●Data representable in tables, that require the definition
of application-specific operations.
●Integration of data and operations from different
domains.
Relational DBMS
●Handle and manipulate simple data
●Support a query language(SQL)
●Good performance
●Multi-user support
●Access control
●Reliability
Object-oriented Databases
●Allow for directly representing complex objects and
efficiently supporting navigational applications
●Allow you to store your data at the same format you
want to use your data.
●Traditional Databases
–Business applications.
●Object Oriented Databases
–Complex navigational applications.
–Multimedia applications.
Why object-oriented approach?
Object-oriented approach
●Natural candidate because it provides the required
flexibility not being constrained by the data types and
query languages available.
●Specifies both the structures of complex objects and
the operations to manipulate these structures.
Object-oriented Data Models
Objects
●Identity
●State
●Behavior
Objects
Identity: Different from the identity of any other object
and is immutable during the object lifetime.
State: Consists of the values of the object attributes.
Behavior: Specified by the methods that act on the object
state, which is invoked by the corresponding operations.
Many OODBMs do not require each entity to be
represented as an object, they distinguish between
objects and values.
Differences(objects-values)
Values are universally
known abstractions and
they have the same
meaning for each user.
Objects correspond to
abstractions whose
meaning is specified in the
context of the application.
Differences(objects-values)
Values are built-in in the
system and do not need to
be defined.
Objects must be
introduced in the system
through a definition.
Differences(objects-values)
The information
represented by a value is
the value itself.
The information
represented by an object is
given by the relationships
it has with other objects
and values.
Object-oriented Data Models
Object Identify
Each object is identified uniquely by an OID which
provides it with an identity independent from its value.
OID is unique and does not depend on the state of the
object.
Object Identity
Object identifiers are usually not visible and accessible
directly by the database users, they are used internally
by the system to identify objects and to support object
references through object attribute values.
Object Identity
OID≠Key(Relational model)
●Key is defined as the value of one or more attributes
and it can be modified.
●OID is independent from the value of an object state.
Advantages/Disadvantages OID
●Implemented by the system, the programmer does not have to select the appropriate keys
●Implemented at a low level by the system, better performance is achieved.
●No semantic meaning is
associated with them.
Two different notions of object equality
●Equality by identity: Two objects are identical if they
are the same object, that is, if they have the same
identifier.
●Equality by value: Two objects are equal if the values
for their attributes are equal recursively.
Object State
●State is a complex value that can be built starting from
other objects and values, using some type constructors.
●Minimal set of constructors that a system should
provide include sets,lists and tuples.
●Sets are crucial because they are a natural way to represent real-world collections and multi valued attributes.
●Tuple constructor is important because it provides a natural way to represent the properties of an entity.
●Lists and arrays are similar to sets, but they impose an order on the elements of the collection and are needed in many scientific applications.
Object Behavior
Objects in an object-oriented database are manipulated
through methods. A method definition consists of two
components: a signature and an implementation.
Object Behavior
●Signature: Specifies the method name, the names and
types of method arguments, and the type of result, for
methods returning a result value.
●Implementation: Consists of a set of instructions
expressed in a programming language.
Encapsulation
In a relational DBMS, queries and application programs that act on relations are expressed in an imperative language incorporating statements of the data manipulation language, and they are stored in a traditional file system rather than in a database. In such an approach,therefore, a sharp distinction is made between programs and data and between query language and programming language. In an object-oriented database,data and operations manipulating them are encapsulated in a single structure: the object. Data and operations are thus designed together, and they are both stored in the same system.
Classes
Object-oriented languages provide the notion of class as a basis for instantiation.
Instantiation is the mechanism that offers the possibility of exploiting the same definition to generate objects with the same structure and behavior.
A class acts as a template , by specifying a
structure,that is, the set of instance attributes, which is
a set of methods that define the instance
interface(method signatures) and implement the
instance behavior(method implementations)
Aggregation Hierarchy and Relationships
In almost all object-oriented data models, each attribute
has a domain, that specifies the class of possible
objects that can be assigned as values to the attribute.
Aggregation Hierarchy and Relationships
If an attribute of a class C has a class C’ as a domain, each
C instance takes as value for the attribute an instance of C’
or of a subclass. Moreover, an aggregation relationship is
established between the two classes. An aggregation
relationship between the class C and the class C’ specifies
that C is defined in terms of C’.
Aggregation Hierarchy and Relationships
Because C’ can be in turn defined in terms of other
classes, the set of classes in the schema is organized
into an aggregation hierarchy.
Its not a hierarchy in a strict sense, because class definitions can be
recursive.
Aggregation Hierarchy and Relationships
Relationship is a link between entities in applications.
It exists in many semantic models and in models for
the conceptual design of databases.
Aggregation Hierarchy and Relationships
●They are characterized by a degree,which indicates the
number of entities that participate in the relationship,
and by some cardinality constraints, which indicate the
minimum and maximum number of relationships in
which an entity can participate.
Extent and Persistence Mechanisms.
Besides being a template for defining objects, in some
systems, the class also denotes the collection of its
instances, that is, the class has also the notion of
extend.
Extent and Persistence Mechanisms.
The extend of a class is the collection of all instances
generated for this class. This aspect is important
because the class is the basis on which queries are
formulated.
Extent and Persistence Mechanisms.
An important issue concerns the persistence of class instances, that is , the modalities by which objects are made persistent (inserted in the database) and deleted eventually (removed from the database).In relational databases, explicit statements(like INSERT and Delete in SQL) are provided. In object-oriented databases, two different approaches can be adopted with respect to object persistence:
Extent and Persistence Mechanisms.
●Persistence is an implicit property of all class instances, the creation of an instance also has the effect of inserting the instance in the database,thus, the creation of an instance automatically implies its persistence.
●Persistence is an orthogonal properties of objects, the creation of an instance does not have the effect of inserting the instance in the database. Rather, if an instance has to survive the program that created it, it must be made persistent.
With respect to object deletion, two different
approaches are possible:
●The system provides an explicit delete operation. The possibility of explicitly deleting objects poses the problem of referential integrity, if an object is deleted and other objects refer to it, references are not any longer valid(such references are called as dangling references)
●The system does not provide an explicit delete
operation. A persistent object is deleted only if all
references to it have been removed.
Migration
Objects represent real-world entities, they must be able to reflect the evolution in time of those entities. This can be modeled only if an object can become an instance of a class different from the one from which it has been created. This evolution,known as object migrationallows an object to modify its features,that is, attributes and operations, by retaining its identity.
Inheritance
Inheritance allows a class, called a subclass, to be
defined starting from the definition of another class,
called a superclass, a subclass may in addition have
some specific, non-inherited features.
●Inheritance is a powerful reuse mechanism. By using such a mechanism, when defining two classes their common properties,if any, can be identified and factorized in a common superclass. The definitions of the two classes will, by contrast, specify only the distinguishing specific properties of these classes.
●Some systems allow a class to have several direct
superclasses, in this case we talk of multiple
inheritance. Other systems impose the restriction to a
single superclass, in this case we talk of single
inheritance.
Overriding,Overloading and Late Binding
In order to understand this notions better lets see an
example.
Example
Lets consider the operation display, an operation receiving as input an object and displaying it. Depending on the object type, different display mechanisms are exploited: If the object is a figure, it should appear on the screen, if the object is a person, its data should be printed in some way, if the object is a graph a graphical representation of it should be produced.
In an object-oriented system,the display operation can be defined in a more general class in the class hierarchy. Thus, the operation has a single name and can be used indifferently on various objects. The operation implementation is redefined for each class, this redefinition is known as overriding. As a result, a single name denotes different programs, and the system takes care of selecting the appropriate one at each time during execution.
To support this functionality, the system can no longer bind operation names to corresponding code at compile time, rather, it must perform such binding at run time: This late translation is known as late binding. Thus, the notion of overriding refers to the possibility for a class of redefining the inheritance mechanism allows for specializing a class through additions and substitutions.
Overriding implies overloading, because an operation
shared along an inheritance hierarchy can have
different implementations in the classes belonging to
this hierarchy, therefore the same operation name
denotes different implementations.
Query Languages
Query languages are an important functionality of any DBMS. A query language allows users to retrieve data by simply specifying some conditions on the content of those data. In relational DBMSs, query languages are the only way to access data, whereas OODBMSs usually provide two different modalities to access data. The first one is called navigational and the second access is called associative.
●Navigational access modality is based on object
identifiers and on the aggregation hierarchies into
which objects are organized. Given a certain OID, the
system can access directly and efficiently the object
referred by it and can navigate through objects referred
by the components of this object.
●Associative access modality is based on SQL-like query languages.
●The two different access modalities are used in a complementary way: A query is evaluated to select a set of objects that are then accessed and manipulated by applications through the navigational mechanism.
●Navigational access is crucial in many applications such
as graph traversal. Such type of access is inefficient in
relational systems because it requires the execution of a
large number of join operations. Associative access,by
contrast, has the advantage of supporting the expression of
declarative queries, which reduces application
development time.
Object-oriented query languages offer the possibility to
impose conditions on nested attributes of an object
aggregation hierarchy, through path expressions, which
allows for expressing joins to retrieve the values of the
attributes of an object components.
Two different kind of join can be distinguished
●Implicit join, which derives from the hierarchical
structure of objects.
●Explicit join, which, as in relational query languages,
explicitly compares two objects.
The ODMG standard
●ODMG is an OODMBS standard, which consists of a
data model and a language, whose first version was
proposed in 1993 by a consortium of major companies
producing OODMBSs.
The ODMG standard consists of the following components:
●A data model(ODMG Object Model)
●A data definition language (ODL)
●A query language(OQL)
●Interfaces for the object-oriented programming languages C++ ,Java and Smalltalk and data manipulation languages for those languages.
●ODMG supports both the notion of object and the
notion of value (literal in the ODMG terminology).
The Object Model supports different literal types,
including atomic literals, collection literals and
structured literals.
●Examples of atomic literals are string, char, boolean,
float, short and long.
A collection literal is a collection of elements, which
themselves could be of any literal or object type. The
collection literal types supported by the ODMG Object
Model include set,bag,list,array and dictionary.
●A set is an unordered collection of elements of the same type without any duplicates.
●A bag is an unordered collection of elements that may contain duplicates.
●A list is an ordered collection of elements of the same type.
●An array is a dynamically sized ordered collection of elements that can be located by position.
●A dictionary is an unordered sequence of key-value pairs without any duplicates.
●A structured literal, also known as a structure,
consists of a fixed number of named elements, each of
which could be a literal or object type. The Object
Model supports the following predefined structures:
Date, Interval, Time and Timestamp. In addition, it
supports user-defined structures.
Defining User Structures
Struct Address{
String street_address;
String city;
String state;
String zip;
};
Struct phone{
Short area_code;
Long personal_number;
};
For example
attribute set <phone> phones;
Class Student{
Attribute string name;
Attribute Date dateOfBirth;
//user-defined structured attributes
Defining a Range for an Attribute
If you know all the possible values that an attribute can have, you can enumerate those values in ODL.
class CourseOffering{
attribute string term;
attribute enum section {1,2,3,4,5,6,7,8};
//operation
short enrollment( );
};
Student
Name
DateOfBirth
Address
Phone
Age( )
Gpa( )Register_for(crse,sec,term)
Takes
Taken by
Offers
Defining Relationshipsclass student{
attribute string name;
attribute date dateOfBirth;
attribute Address address;
attribute Phone phone;
//relationship between Student and Course Offering
relationship set<CourseOffering> takes inverse CourseOffering::taken_by;
//operations
short age( );
float gpa( );
boolean register_for(string crse, short sec, string term);
};
class CourseOffering{
(extend courses)
attribute string term;
attribute enum 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 ( );
};
Defining an Attribute with an Object Identifier as Its Value
class Course{
//the dept attribute’s value is an object identifier
attribute Department dept;
//other attributes,operations and relationships..
};
class Department{
attribute short dept_number;
attribute string dept_name;
attribute string office_address;
};
Many-to-many relationship with an association class
Many-to-many relationship broken into two one-to-many relationships
Works onfor
Allocated to Has
Class diagram//emp_id is the primary key for employee
key emp_id)
attribute short emp_id;
attribute string name;
attribute address address;
attribute float salary;
attribute date date_hired;
attribute enum gender(male,female);
//multi valued attribute
attribute set<string> skills;
relationship set <assignment> works_on inverse assignment::allocated_to;
//the following operations don’t return any values
void hire( );
Class diagram
Class Assignment{
(extend assignments)
Attribute Date start_date;
Attribute Date end_date;
Attribute short hours;
Relationship Employee allocated_to inverse Employee::works_on;
Relationship Project for inverse Project::has;
//the following operation assigns an employee to a project
Void assign (short emp, string proj);
Class Project{
(extend projects
//proj_id is the primary key for Project
Key proj_id);
Attribute string proj_id;
Attribute string proj_name;
Attribute enum priority {low,medium,high};
Attribute date begin_date;
Attribute date completion_date;
//multi valued attribute
Attribute set <string> skills_required;
Relationship set <assignment> has inverse Assignment::for;
Long total_emp_hours( );
};
Defining Generalization
ODL supports unary and binary association
relationships, but not relationships of higher degree. It
allows you to represent generalization relationships
using the extends keyword.
Class Employee{
(extend employees)
Attribute short empName;
Attribute string empNumber;
Attribute Address address;
Attribute Date dateHired;
Void printLabel( );
};
Class HourlyEmployee extends Employee{
(extent hrly_emps)
Attribute float hourlyRate;
Float computeWages( );
};
Class SalariedEmployee extends Employee{
( extend salaried_emps)
Attribute float anualSalary;
Attribute boolean stockOptions;
Void contributePension( );
};
Class Consultant extends Employee{
( extent consultants)
Attribute short contractNumber;
Attribute float billingRate;
Float computeFees( );
};
Defining an Abstract Class
Abstract class student {
(extend students
Key stu_number)
Attribute long stu_number;
Attribute string name;
Attribute date dateOfBirth;
Attribute Address address;
Attribute Phone phone;
Relationship set <CourseOffering> takes inverse CourseOffering::taken_by;
Boolean register_for(string crsee, short section, string term);
//abstract operation
Abstract float calc_tuition( );
};
Class GraduateStudent extends Student{
(extend grads)
Attribute char undergrad_major ;
Attribute GRE gre_score;
Attribute Gmat gmat_score;
Float calc_tuition( );
};
Class UndergradStudent extends Students{
( extends undergrads)
Attribute SAT sat_score;
Attribute ACT act_score;
Float calc_tuition( );
};
Defining Other User Structures
Struct GRE{
Score verbal_score;
Score quant_score;
Score analytical_score;
};
Struct GMAT{
Score verbal_score;
Score quant_score;
};
Struct SCORE{
Short scaled_score
Short percentile_score;
};
Creating 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 MBA 669 as follows:
MBA699 course( );
This creates a new instance of Course. The object tag name, MBA 699, can be used to reference this object.
Examples
Jack student (
Name: “Jack Warner”, dateOfbirth: 2/12/74,
Address: {street_address “310 College Rd”, city “Dayton”,state “Ohio”,zip 45468},
Phone: {area_code 937, personal_number 228-2252});
Dan employee ( emp_id: 3678, name: “Dan Bellon”,
Skills:{“Database design”,”OO Modeling”});
Object Query Language
Basic Retrieval Command
Select c.crse_title,c.credit_hrs
From courses c
Where c.crse_code= “MBA 699”
Select s.age
From students s
Where s.name=”John Marsh”
Select s
From students s
Where s.gpa>=3.0
●Instead of using not
●s.address.city!=”Dayton”
Select s
Frm students s
Wheres.gpa>=3.0
And not (s.address.city=”dayton”)
Select s.age
From students s
Where s.gpa <3.0
Select distinct s.age
From students s
Where s.gpa <3.0
Querying Multiple Classes
Select distinct y.crse_code
From courseoffering x,
x.belongs_to y
Where x.term = “Fall 2005”
Select x.enrollment
From courseofferings x,
x.belongs_to y
Where y.crse_code = “ MBA 699”
And x.section = 1
Select c.crse_code,c.crse_title
From students s
s.takes x,
x.belongs_to c
Where s.name = “Mary Jones”
Writing Subqueries
Select distinct struct(cod:c.crse_code,title:c_crse_title,
(select x from c.offers x
Where x.enrollment<20))
From courses c
Select x.name, x.address, x.gpa
From (select s from students s where s.gpa > 3.0)as x
Where x.age>30
Calculating Summary Values
OQL supports all the aggregate operators that SQL
does : count, sum, avg, max and min.
Find the number of students in the university
count(students) Select count(*)
From students s
Select
avg_salary_female:avg(e.salary)
From employees e
Where e.gender=female
max(select salary from
employees)
sum(select salary from
employees)
Calculating Group Summary Values
Select min(e.salary)
From employees e
Group by e.gender
Select *
From projects p
Group by
Low: priority=low,
Medium: priority=medium,
High: priority=high
Qualifying Groups
Select *
From projects p
Group by
Low: priority=low,
Medium: priority=medium,
High: priority=high
Having sum(select x.hours from p.has x)>50
Using a Set in a Query
Select emp_id, name
From employees
Where “Database Design” in skills
Or “OO Modeling” in skills
Select e.emp_id, e.name
From employees e,
e.works_on a,
a.for p
Where “TQM9” in p.proj_id
Select *
From projects p
Where not (“C++Programming” in p.skills_required)
Select e.emp_id,e.name
From employees e
Where exists e in (select x from assignments y
y.allocated_to x)
Select e.emp_id,e.name
From employees e
e.works_on a
Where for all: a.start_date >=1/1/2005
Literature:
J.A. Hoffer, M.B. Prescott, F.R.
McFadden Modern Database
Management 2007, Prentice Hall