Lectures-11-Object Relational and Object Oriented Databases

36
Lecture 11: Object Database Management Systems Problem with RDBMS that motivate for ODBMS RDMS supports only few built-in data types: Number, decimal, double, date, time char, varchar. Type system mismatch: mismatch between data types in RDBMS and data types of a programming language

description

Object relational datbase management lecture powerponit presentation

Transcript of Lectures-11-Object Relational and Object Oriented Databases

Page 1: Lectures-11-Object Relational and Object Oriented Databases

Lecture 11: Object Database Management Systems

• Problem with RDBMS that motivate for ODBMS• RDMS supports only few built-in data types:

– Number, decimal, double, date, time char, varchar.– Type system mismatch: mismatch between data types

in RDBMS and data types of a programming language

Page 2: Lectures-11-Object Relational and Object Oriented Databases

Advanced Database Applications

• Traditional business application such as order processing, inventory control, banking database, widely use RDBMS.

• But this inadequate for other modern applications such as:– Computer-aided design (CAD)– Computer-aided manufacturing (CAM)– Computer-aided software engineering (CASE)– Digital publishing, etc…

Page 3: Lectures-11-Object Relational and Object Oriented Databases

Application Pull

New applications: Computer Aided Design and Manufacturing, Image and geographic databases, geometric databases.

Led to emergence of Object-oriented database management systems that are designed to meet the requirements of these complex domains:– not limited by data type or query language.– power to designer to specify both structure of complex

objects and their behaviour (operations)

Page 4: Lectures-11-Object Relational and Object Oriented Databases

Limitations of the Relational Model?

• Normalisation creates entities that do not correspond to entities in the real world, leading to many joins during query processing, and complex construction and deletion of data structures

• Semantics - relational databases have poor support for the meaning of a table, a column or data type

• Passive data - behaviour of data not related to data

Page 5: Lectures-11-Object Relational and Object Oriented Databases

Definition of an Object

• An object is a uniquely identifiable entity that contains both:

– attribute values that describe its state

– operations that define its behaviour

– links to other objects.

• Every object has a type (or class in object-oriented terminology) that defines the attributes, operations and association links.

Page 6: Lectures-11-Object Relational and Object Oriented Databases

Definition of an Object

• Objects in OOPL exist only during program execution. This is called transient objects

• Objects in OO database are stored permanently in database. This is called persistent objects where these objects exist after program is terminated and can be retrieved. This allow sharing of objects among multiple programs and applications

Page 7: Lectures-11-Object Relational and Object Oriented Databases

Definition of an Object

• Classes contain collections of objects.

• A class definition consists of a collection of variables (instance) and methods.

• Example shows two variables X, Y and two methods Distance and Equals

CLASS Point {// Properties/Variables:Real X; // X coordinatesReal Y; // Y coordinates// Methods:Distance (IN Point a Point);// Computes the distance // between 2 pointsEquals (IN Point aPoint);// Determines if two Points //have the same coordinates};

Page 8: Lectures-11-Object Relational and Object Oriented Databases

Class Definition - Encapsulation• Example is a class that

use the Point class• The new class

Rectangle do not need to recode the variables and methods of the Point

• Encapsulation - objects can be accessed only through their interfaces, the details are not accessible

• You can use Distance and Equals methods only to manipulate Point objects

//Rectangle Class Using the Point Class

CLASS Rectangle {// VariablesPoint UpperLeftCorner; // Upper left PointPoint LowerRightCorner; // Lower Right Point// Methods:Area();// Computes areas of the rectangleLength(); // Computes the LengthHeight(); // Compute the Height};

Page 9: Lectures-11-Object Relational and Object Oriented Databases

Class Definition - Inheritance• Inheritance – applies to

both data and procedures• Example is a class that

inherit from the Point class.

• A ColorPoint is a point with color

ColorPoint Example (Subclass of Point)

CLASS ColorPoint EXTENDS Point {// VariablesInteger Color; // Integer value denoting a color

// Methods:Brighten(IN Real Intensity); // Computes a new color that is brighter};

Page 10: Lectures-11-Object Relational and Object Oriented Databases

Object Database - User-Defined Types – SQL:2003

• Example shows Point type

• NOT FINAL – mean that subtypes can be defined

• INSTANTIATE – means that instances of the type can be created

CREATE TYPE Point AS ( X FLOAT, -- X coordinate Y FLOAT) – Y coordinate

//MethodsMETHOD Distance(P2 Point) RETURNS FLOAT, -- Computes the distance between 2 points

METHOD Equals (P2 Point) RETURNS BOOLEAN -- Determines if 2 points are equivalentNOT FINAL;INSTANTIABLE

Page 11: Lectures-11-Object Relational and Object Oriented Databases

Object Database - User-Defined Types – SQL:2003

• Example shows ColorPoint type, a subtype of Point

• UNDER – indicates the parent type

• OVERRIDING – indicates that the method overrides the definition in a parent type

CREATE TYPE ColorPoint UNDER Point AS (Color Integer)METHOD Brighten(Intensity INTEGER) RETURNS INTEGER, -- increases color inensityOVERRIDING METHOD Equals (CP2 ColorPoint) RETURNS BOOLEAN -- Determines if 2 points are equivalentFINAL;

Page 12: Lectures-11-Object Relational and Object Oriented Databases

Defining User-Defined Types – Oracle 10g

CREATE TYPE Point AS OBJECT//Properties( X FLOAT(10), -- X coordinate Y FLOAT (10)) -- Y coordinate

//MethodsMEMBER FUNCTION Distance(P2 Point) RETURNS NUMBER-- Computes the distance between 2 points MEMBER FUNCTION Equals (P2 Point) RETURNS BOOLEAN-- Determines if 2 points are equivalentMEMBER PROCEDURE PRINT)NOT FINALINSTANTIABLE;

Page 13: Lectures-11-Object Relational and Object Oriented Databases

Defining User-Defined Types – Oracle 10g

CREATE TYPE ColorPoint UNDER Point( Color INTEGER)

MEMBER FUNCTION Brighten (Intensity INTEGER) RETURN INTEGER-- Increases color intensity MEMBER FUNCTION Equals (CP2 ColorPoint) RETURN BOOLEAN-- Determines if 2 points are equivalentOVERRIDING MEMBER PROCEDURE PRINT)NOT FINALINSTANTIABLE;

Page 14: Lectures-11-Object Relational and Object Oriented Databases

Class Definition - 2

Class Point Properties: pointId: integer xLocation: real yLocation: real Methods: new( ) store( ) retrieve (pointId: integer) : point delete( ) move(x: real, y:real)

Class Line Properties: lineId: integer points: list of point objects Methods: new( ) store( ) retrieve (lineId: integer) : line delete( ) resize(scaleFactor: real) getLength() : real

Page 15: Lectures-11-Object Relational and Object Oriented Databases

Explanation of Methods

• ‘new’ instantiates an object instance and ‘delete’ destroys an object instance.

• ‘store’ takes an existing polygon and stores it.

• ‘retrieve’ returns any stored polygon object with the specified id.

• ‘resize’ allows the polygon object to be resized.

• ‘getArea’ returns the area of the polygon.

Page 16: Lectures-11-Object Relational and Object Oriented Databases

Explanation of Methods• ‘Methods define the behaviour of the object.

• Method consists of a name and a body that performs the behaviour.

• Method is used to change the object state by modifying the attributes

method void updateSalary(float increment)

{

salary = salary + increment

}

Page 17: Lectures-11-Object Relational and Object Oriented Databases

OODB System Manifesto• Complex objects – build

complex objects by applying constructors to basic objects

• Object identity

• Encapsulation

• Types or classes

• Inheritance

• Dynamic binding

• DML computationally complete

• Extensible data types

• Data persistence

• Handling of very large databases

• Concurrent users

• Recovery from software and hardware failures

• Simple querying method

Page 18: Lectures-11-Object Relational and Object Oriented Databases

Strategies for OODB Development

• Extend an existing OO Language

• Provide libraries for an OOD

• Embed database language in an OO programming language

• Extend SQL with OO features

• Develop a new OO programming language with OOD capabilites

Page 19: Lectures-11-Object Relational and Object Oriented Databases

Object Data Management Group (ODMG) – 1993 to 2000

• Object Model (OM):– Is the data model upon which the object definition

language (ODL) and object query language (OQL) are based.

– It is to provide a standard data model for objects databases, like SQL describes a standard data model for relational databases.

Page 20: Lectures-11-Object Relational and Object Oriented Databases

ODMG – 1993 to 2000

• An objects has four characteristics: identifier, name, lifetime and structure

• Object identifier (objectID) is a unique system-wide identifier

• Some object may optionally be given a unique name within a particular ODMS. These names can be used to locate the object

• The lifetime of an object specifies whether it is persistent object (that is database object) or transient object (that is an object in an executing program that disappears after the program terminates)

• The structure of an object specifies how the object is constructed by using the type constructors. The structure specifies whether an object is atomic or not. An atomic object refers to a single object that follows a user-defined type. If an object is not atomic, then it will be composed of other objects.

Page 21: Lectures-11-Object Relational and Object Oriented Databases

Object Definition Language (ODL) and Object Query Language (OQL)

• The ODL defines the classes in a OODBMS:

– Is a language for defining the specifications of object types for ODMG. Equivalent to DDL in traditional DBMS

– Attributes, associations, and operation declarations written in ODL.

– Implementation of operations is carried out in an OOPL.

– It main use is to create object specifications, that is classes and interfaces

– ODL is not a full programming language

Page 22: Lectures-11-Object Relational and Object Oriented Databases

Object Definition Language (ODL) and Object Query Language (OQL)

• OQL:– Provides declaratives access to the object database

using SQL-like syntax– OQL can be standalone language and as a language

embedded in another language– works with C++, Java, or Smalltalk, – is similar to SQL but includes object identity,

operations, inheritance, aggregation and associations.– Access of objects from entry points (named objects and

collections of objects ) and then navigating between objects by using links.

Page 23: Lectures-11-Object Relational and Object Oriented Databases

Object Definition Language (ODL) and Object Query Language (OQL)

• Row type – is a sequence of field name/data type pairs that provides a data type to represent the types of rows in tables.

• Can be used to allow a column of a table to contain row values

CREATE TABLE Branch (branchNo CHAR(4), Address ROW(street VARCHAR(25), city VARCHAR(15), postcode ROW(cityIdentifier VARCHAR(4), subPart VARCHAR(4))));

INSERT INTO Branch VALUES(‘B005’, ROW(‘22 Deer Rd’, ‘London’, ROW(‘SW1’, ‘4WH’)));

Page 24: Lectures-11-Object Relational and Object Oriented Databases

Object Definition Language (ODL) and Object Query Language (OQL)

• User-defined Types (UDT) – referred to Abstract Data Type (ADT), 2 types – distinct type and structured type

• Distinct type – allows differentiation between the same base types.

CREATE TYPE OwnerNumberType AS VARCHAR(5);CREATE TYPE StaffNumberType AS VARCHAR(5);

Page 25: Lectures-11-Object Relational and Object Oriented Databases

Advantages and Disadvantages of OODBMS

• Encapsulation of both state and behaviour is more natural.• Inheritance and aggregation for complex data types.• More expressive query language (navigation + collections)• Improved performance and long-duration transactions.• No theoretical background to the data model.• Lack of universal data model.• Complexity in database management.• Query optimisation compromises encapsulation.• Lack of support for security.

Page 26: Lectures-11-Object Relational and Object Oriented Databases

• ORDBMSs are an evolution of RDBMSs that allow user-defined types (UDTs), supporting inheritance and complex data types, to be used in tables.

• Compare this with OODBMS that store objects directly.

ORDBMS

Page 27: Lectures-11-Object Relational and Object Oriented Databases

ORDBMS - User-defined types - 1

• The power of using UDTs is in the more general case when the UDT consists of several attribute (‘row’) definitions and several procedure definitions:

CREATE TYPE Polygon AS OBJECT (polyID INTEGER,polyName VARCHAR(20),MEMBER FUNCTION getArea RETURN REAL) NOT FINAL;

/Note: NOT FINAL – indicate that we can create subtypes of

this user-defined type

Page 28: Lectures-11-Object Relational and Object Oriented Databases

ORDBMS - User-defined types – 2

• In ORACLE the code to specify the member methods is defined in a CREATE TYPE BODY statement:

CREATE TYPE BODY Polygon ASMEMBER FUNCTION getArea RETURN REAL IS/*code to calculate area of regular polygon /*…..BEGIN…..RETURN area;END getArea;END;

/

Page 29: Lectures-11-Object Relational and Object Oriented Databases

Inheritance of Types

• To create a subtype RegularPolygon of a supertype Polygon:

CREATE TYPE RegularPolygon UNDER Polygon (sidelength DECIMAL(7,2)numberOfSides INTEGER) FINAL;

/

• The RegularPolygon subtype combines two new attributes with the attributes and functions defined in the Polygon type.

Page 30: Lectures-11-Object Relational and Object Oriented Databases

Aggregation of types

• A UDT can be composed of other UDTs.• usedLine Line,whereCREATE TYPE Line AS OBJECT (

lineID INTEGER,polyLeft Polygon,polyRight Polygon,fromNode Point,toNode Point) FINAL;

/

Page 31: Lectures-11-Object Relational and Object Oriented Databases

Using Types in Tables

• Tables can use UDTs in column definitions:

CREATE TABLE StoredShapes (shape Polygon,creationDate DATE,author VARCHAR(30) );

• The first name of the person can be accessed by: SELECT n.shape.polyName FROM StoredShapes n;

Page 32: Lectures-11-Object Relational and Object Oriented Databases

SQL DML for Types in Tables

• All data must be specified when inserting into UDTs:

INSERT INTO storedShapes VALUES (Polygon(9, ‘A’),‘2004-11-24’, ‘Smith’);

• Functions can be accessed from the UDTs:

SELECT n.shape.getArea() FROM storedShapes n;

Page 33: Lectures-11-Object Relational and Object Oriented Databases

Problems with RDBMS and ORDBMS

Impedance mismatch: SQL is a declarative language that handles rows of data,

while high-level programming languages such as C++, Java and C# are procedural languages that handle only one row of data at a time.

Data-type conversion: SQL and PLs have different built-in data types - need to translate between DB and PL representations.

Portability: A program or data structure written in one OO database should be transferable to another OO database.

Page 34: Lectures-11-Object Relational and Object Oriented Databases

What You Should Now Know

• How to create object types and the associated object type body.

• How to create sub-types and allow object types to have sub-types.

• How to have object types with object types.• How to select, insert, update and delete object types.• What an ORDBMS and an OODBMS are and why

they are used.

Page 35: Lectures-11-Object Relational and Object Oriented Databases

Creating Object Types

• Classes define attributes and methods• Attributes are used to store an object’s state and

methods are used to model an object’s behavioursCREATE OR REPLACE TYPE address AS OBJECT(line1 VARCHAR2(20),line2 VARCHAR2(20),city VARCHAR2(20),state_code VARCHAR2(2),zip VARCHAR2(13));/

Page 36: Lectures-11-Object Relational and Object Oriented Databases

Creating Object Types• Classes define attributes and methods• Attributes are used to store an object’s state and

methods are used to model an object’s behavioursDECLARE off_add address :=address('19 J','R Rd','Vancouver','NJ','00000');BEGIN DBMS_OUTPUT.PUT_LINE(off_add.line1||' '||off_add.line2);

DBMS_OUTPUT.PUT_LINE(off_add.city||', '||off_add.state_code||' '||off_add.zip);END;/