106
Object-Oriented Databases Foivos Gkoulis

Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

  • Upload
    others

  • View
    10

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Object-Oriented

DatabasesFoivos Gkoulis

Page 2: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Applications

●Business applications.

●Complex navigational

applications.

●Multimedia applications.

Page 3: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Business applications

●Large amounts of data

●Simple structure

●More or less complex queries.

Page 4: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Complex navigational applications

●Manipulate data with complex structure and/or

relationships

●Efficiently traverse such relationships.

●For example telecommunications.

Page 5: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 6: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Relational DBMS

●Handle and manipulate simple data

●Support a query language(SQL)

●Good performance

●Multi-user support

●Access control

●Reliability

Page 7: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 8: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●Traditional Databases

–Business applications.

●Object Oriented Databases

–Complex navigational applications.

–Multimedia applications.

Page 9: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Why object-oriented approach?

Page 10: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 11: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Object-oriented Data Models

Objects

●Identity

●State

●Behavior

Page 12: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 13: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Many OODBMs do not require each entity to be

represented as an object, they distinguish between

objects and values.

Page 14: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 15: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 16: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 17: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 18: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 19: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 20: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 21: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 22: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 23: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 24: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Object Behavior

Objects in an object-oriented database are manipulated

through methods. A method definition consists of two

components: a signature and an implementation.

Page 25: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 26: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 27: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 28: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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)

Page 29: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 30: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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’.

Page 31: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 32: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 33: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 34: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 35: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 36: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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:

Page 37: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 38: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

With respect to object deletion, two different

approaches are possible:

Page 39: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 40: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 41: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 42: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 43: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 44: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Overriding,Overloading and Late Binding

In order to understand this notions better lets see an

example.

Page 45: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 46: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 47: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 48: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 49: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 50: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 51: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 52: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 53: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 54: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 55: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 56: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 57: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●ODMG supports both the notion of object and the

notion of value (literal in the ODMG terminology).

Page 58: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

The Object Model supports different literal types,

including atomic literals, collection literals and

structured literals.

Page 59: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●Examples of atomic literals are string, char, boolean,

float, short and long.

Page 60: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 61: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 62: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

●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.

Page 63: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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;

Page 64: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Class Student{

Attribute string name;

Attribute Date dateOfBirth;

//user-defined structured attributes

Page 65: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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( );

};

Page 66: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Student

Name

DateOfBirth

Address

Phone

Age( )

Gpa( )Register_for(crse,sec,term)

Takes

Taken by

Offers

Page 67: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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);

};

Page 68: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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 ( );

};

Page 69: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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;

};

Page 70: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Many-to-many relationship with an association class

Page 71: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Many-to-many relationship broken into two one-to-many relationships

Works onfor

Allocated to Has

Page 72: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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( );

Page 73: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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);

Page 74: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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( );

};

Page 75: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 76: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations
Page 77: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Class Employee{

(extend employees)

Attribute short empName;

Attribute string empNumber;

Attribute Address address;

Attribute Date dateHired;

Void printLabel( );

};

Page 78: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Class HourlyEmployee extends Employee{

(extent hrly_emps)

Attribute float hourlyRate;

Float computeWages( );

};

Page 79: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Class SalariedEmployee extends Employee{

( extend salaried_emps)

Attribute float anualSalary;

Attribute boolean stockOptions;

Void contributePension( );

};

Page 80: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Class Consultant extends Employee{

( extent consultants)

Attribute short contractNumber;

Attribute float billingRate;

Float computeFees( );

};

Page 81: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Defining an Abstract Class

Page 82: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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( );

};

Page 83: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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( );

};

Page 84: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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;

};

Page 85: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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.

Page 86: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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”});

Page 87: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Object Query Language

Basic Retrieval Command

Select c.crse_title,c.credit_hrs

From courses c

Where c.crse_code= “MBA 699”

Page 88: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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”)

Page 89: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select s.age

From students s

Where s.gpa <3.0

Select distinct s.age

From students s

Where s.gpa <3.0

Page 90: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Querying Multiple Classes

Select distinct y.crse_code

From courseoffering x,

x.belongs_to y

Where x.term = “Fall 2005”

Page 91: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select x.enrollment

From courseofferings x,

x.belongs_to y

Where y.crse_code = “ MBA 699”

And x.section = 1

Page 92: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select c.crse_code,c.crse_title

From students s

s.takes x,

x.belongs_to c

Where s.name = “Mary Jones”

Page 93: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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

Page 94: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select x.name, x.address, x.gpa

From (select s from students s where s.gpa > 3.0)as x

Where x.age>30

Page 95: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Calculating Summary Values

OQL supports all the aggregate operators that SQL

does : count, sum, avg, max and min.

Page 96: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Find the number of students in the university

count(students) Select count(*)

From students s

Page 97: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select

avg_salary_female:avg(e.salary)

From employees e

Where e.gender=female

max(select salary from

employees)

sum(select salary from

employees)

Page 98: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Calculating Group Summary Values

Select min(e.salary)

From employees e

Group by e.gender

Page 99: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select *

From projects p

Group by

Low: priority=low,

Medium: priority=medium,

High: priority=high

Page 100: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

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

Page 101: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Using a Set in a Query

Select emp_id, name

From employees

Where “Database Design” in skills

Or “OO Modeling” in skills

Page 102: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select e.emp_id, e.name

From employees e,

e.works_on a,

a.for p

Where “TQM9” in p.proj_id

Page 103: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select *

From projects p

Where not (“C++Programming” in p.skills_required)

Page 104: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select e.emp_id,e.name

From employees e

Where exists e in (select x from assignments y

y.allocated_to x)

Page 105: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Select e.emp_id,e.name

From employees e

e.works_on a

Where for all: a.start_date >=1/1/2005

Page 106: Object-Oriented Databasespokorny/dj/prezentace/3_140.pdf · 2018. 12. 11. · Object-oriented Databases ... In a relational DBMS, queries and application programs that act on relations

Literature:

J.A. Hoffer, M.B. Prescott, F.R.

McFadden Modern Database

Management 2007, Prentice Hall