agenda tma03 m877 block 3 databases with objects

42
Agenda TMA03 M877 Block 3 Databases with Objects

Upload: annis-payne

Post on 16-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Agenda  TMA03  M877 Block 3 Databases with Objects

Agenda

TMA03 M877 Block 3 Databases with Objects

Page 2: Agenda  TMA03  M877 Block 3 Databases with Objects

Object-Oriented Technology

Page 3: Agenda  TMA03  M877 Block 3 Databases with Objects

Objects

An object has state and behaviour.

The state of an object is given by its instance variables.

The behaviour of an object is given by its methods.

Each object has an unique identifier - OID.

Page 4: Agenda  TMA03  M877 Block 3 Databases with Objects

Classes

Each object is an instance of a class. Objects which are instances of the same class

have the same instance variables and methods.

class Salaried_employee instance variables staff_no string name string address Address department Department annual_salary Payment deduction_rates Deduction_rates cumulative_totals Cumulative_totals methods monthly_payment() "Answer the monthly payment due the employee using annual_salary and deduction_rates of receiver. Update cumulative_totals of receiver."

Method heading

Page 5: Agenda  TMA03  M877 Block 3 Databases with Objects

Instance Variables

Instance variables are references to objects. For example,

Object referenced by an instance variable has a class. For example, staff_no references object that is an

instance of the class string.

name := 'Patricia Pantani'

Page 6: Agenda  TMA03  M877 Block 3 Databases with Objects

Object Identifier - OIDs

Page 7: Agenda  TMA03  M877 Block 3 Databases with Objects

Relationships

In object-oriented language, relationship (association) between classes are represented via instance variables.

Department

SalariedEmployee

DepartmentHead DepartmentMembers

Merit SalariedEmployee

Page 8: Agenda  TMA03  M877 Block 3 Databases with Objects

Department Class

class Department instance variables department_name string department_head Salaried_employee department_members set (Salaried_employee) methods transfer_to(aSalaried_employee, aDepartment) "If aSalaried_employee is department_head of the receiver then answer false; else if aSalaried_employee is an element in department_members of the receiver then delete aSalaried_employee from department_members of the receiver and insert aSalaried_employee as an element in department_members in aDepartment, set department in aSalaried_employee to reference aDepartment and answer true; else answer false."

Page 9: Agenda  TMA03  M877 Block 3 Databases with Objects

1:1 and 1:N Relationships

1:1 Relationship - DepartmentHead Define department_head instance variable in Department instances.

1:N Relationship - DepartmentMembers From :N side to :1 side,

Define the department instance variable in Salaried_employee instanaces

From :1 side to :N side, Define the department_members instance

variable in Department instances

Page 10: Agenda  TMA03  M877 Block 3 Databases with Objects

Messages

Objects communicate with each others via message and message answer. A message is a request to a receiver (an

object) for some behaviour (method) A message answer is the requested

behaviour sent back to the originated object. Instance variable has a pair of assessor

methods Get method : department_name() Set method : department_name(aString)

Page 11: Agenda  TMA03  M877 Block 3 Databases with Objects

Encapsulation

Instance variables (state) and methods (behaviour) of an object instance are encapsulated and hidden from any other object instances.

Encapsulation separates the external behaviour (messages) from the details of implementation (instance variables and methods).

Public methods can be invoked via messages from other objects, but NOT for private methods.

Page 12: Agenda  TMA03  M877 Block 3 Databases with Objects

Subclasses and Inheritance A subclass inherits all instance variables and

methods of the superclass. Additional instance variables and methods can be

defined for the subclasses. Methods defined in the superclass will be overridden

if the methods are re-defined in the subclass.

class Meritorious_Salaried_employee as subclass of Salaried_employee instance variables merit_award Payment methods monthly_payment() "Answer the monthly payment of the employee using annual_salary, deduction_rates and merit_award of the receiver. Update cumulative_totals of receiver."

Page 13: Agenda  TMA03  M877 Block 3 Databases with Objects

Polymorphism

An instance of a subclass is also an instance of its superclass.

Overriding polymorphism:- The version of a method to be used is

determined by the class of the object receiving the message – if the method exists in the most specific class, then it is used, else the superclass is searched for the method, and so on until the method is found.

Page 14: Agenda  TMA03  M877 Block 3 Databases with Objects

Object-Oriented Database Technology

Page 15: Agenda  TMA03  M877 Block 3 Databases with Objects

Schema

An object-oriented DBMS manages a stored collection of objects.

Each object has a unique OID. A schema in object-oriented DBMS gives a

logical definition of the stored data. A schema contains NO implementation details. Classes have attributes and operations. In terms of the ODMG (Object Data

Management Group), a schema is specified using the Object Definition Language (ODL).

A class definition consists of properties (attributes followed by relationships) and operations. Optionally it may include an extent and keys.

Page 16: Agenda  TMA03  M877 Block 3 Databases with Objects

Extent, Keys & Relationships

An extent is a object reference to the set containing all instances of the class in the database. e.g. Salaried_employees is a reference to all

instances of the class Salaried_employee Keys are uniqueness constraints that are enforced by

the DBMS for all instances of the class. Relationship :-

Defined by a pair of named traversal paths that allow user processes to navigate between objects in the relationship:

DBMS maintains the consistency of traversal paths and their inverses and provides public operations that permit the insertion and deletion of member instances and the navigation between instances

Page 17: Agenda  TMA03  M877 Block 3 Databases with Objects

Schema Class Definition: Salaried Employees

class Salaried_employee( extent salaried_employees keys staff_no, (name, address)){ attribute string staff_no; attribute string name; attribute Address address; attribute Payment annual_salary; attribute Deduction_rates deduction_rates; attribute Cumulative_totals cumulative totals; relationship Department is_member_of inverse Department::has_members; relationship Department is_head_of inverse Department::head_is; Payment monthly_payment() "Answer the monthly payment due to the employee using annual_salary and deduction_rates of receiver. Update cumulative_totals of receiver.";};

Page 18: Agenda  TMA03  M877 Block 3 Databases with Objects

Schema Class Definitions:Departments

class Department( extent departments){ attribute string department_name; relationship Salaried_employee head_is; inverse Salaried_employee::is_head_of relationship set <Salaried_employee> has_members inverse Salaried_employee::is_member_of; Boolean transfer_to(in Salaried_employee transfer_employee, in Department to_department) "If transfer_employee is head_is of the receiver then answer false; else if transfer_employee is in has_members of the receiver then delete transfer_employee from has_members of the receiver and insert transfer_employee into has_members of to_department, and answer true; else answer false."};

Page 19: Agenda  TMA03  M877 Block 3 Databases with Objects

Operations

Behaviour of a schema classs is specified as a set of operation signatures (type of value returned, name of operation and type & name of each argument).

Operations merely consist of an operation signature without codes. The language binding will represent operations by methods which have appropriate codes in the corresponding object-oriented programming language.

Page 20: Agenda  TMA03  M877 Block 3 Databases with Objects

Constraints and Inheritance

Apart from uniqueness constraints, other constraints (such as participation conditions) need to be represented via operations.

Extends mechanism is used for a class to inherit both behaviour and state from another class.

class Meritorious_Salaried_employee extends Salaried_employee( extent merits){ attribute Payment merit_award; Payment monthly_payment() "Answer the monthly payment of the employee using annual_salary, deduction_rates and merit_award of the receiver. Update cumulative_totals of receiver.";};

Page 21: Agenda  TMA03  M877 Block 3 Databases with Objects

Data Manipulation

OQL (Object Query Language): It is not computationally complete but is easy

to use (and has strong similarities with SQL). It does not provide explicit update operators.

OML (Object Manipulation Language) It is defined for each object-oriented

programming language for which a language binding has been given by the ODMG 3.0 specification.

It extends the programming language so that it can retrieve and modify objects from database.

Page 22: Agenda  TMA03  M877 Block 3 Databases with Objects

Why Objects and Databases?

Page 23: Agenda  TMA03  M877 Block 3 Databases with Objects

Perceived Problems of Relational Database Technology

Lack of support of new data type. E.g. audio, image and video

Separation of data and process. Structural limitations

Each value represented in a relation is atomic – the value is always one value and never a group of values.

Impedance mismatch SQL manipulates collections of occurrences (tables

made up of rows) whereas host language manipulates individual occurrences (individual rows).

Page 24: Agenda  TMA03  M877 Block 3 Databases with Objects

Why Object-Oriented Database?

Attractive characteristics of object-oriented technology: Components-based development

Complex objects are built from simple objects. Reuse code.

Encapsulation – data semantics A computationally complete DML

Avoid impedance mismatch between SQL and host language.

Subclasses and inheritance Direct representation of M:N relationship.

Page 25: Agenda  TMA03  M877 Block 3 Databases with Objects

SQL 'Objects' – (Object Relational Database)

Page 26: Agenda  TMA03  M877 Block 3 Databases with Objects

User-Defined Types

Object capabilities of SQL are provided by User-Defined Types (UDT).

There are two kinds of UDT: Distinct type Structured type

Page 27: Agenda  TMA03  M877 Block 3 Databases with Objects

Distinct Types A distinct type is defined in terms of its source type,

which represents the values of the distinct type. It provides encapsulation – a distinct type has both state

and behaviour. Implicit creation of an ordering based on the source type. Cast and transform functions are created implicitly.

Cast functions converts values of distinct type to and from its source type.

Transform functions converts values of distinct type to and from host variable.

Functions, such as arithmetic or comparison, for manipulating values are created explicitly.

Page 28: Agenda  TMA03  M877 Block 3 Databases with Objects

Distinct Type

CREATE TYPE ids_of_students AS CHAR(3)

student_id ids_of_students

student_id < CAST('s51' AS ids_of_students)

SELECT student_id

INTO :s_identifier

FROM student

WHERE student_name = 'John'

Page 29: Agenda  TMA03  M877 Block 3 Databases with Objects

Structured Types

A structured type is defined as having a number of attributes and a number of methods.

The creation of a structured type includes the implicit creation of special kind of function, called constructor function, which has the same name as the structured type, has no parameter and returns a value of that type.

Cast and transform functions need to be created explicitly.

Page 30: Agenda  TMA03  M877 Block 3 Databases with Objects

Structured Types

For each attribute of a structured type, two methods are implicitly created. An observer method has a name that is the

same as that of the attributes; it has no parameter; when invoked it returns the value of the attribute for the instance of the structured type for which the method is invoked.

A mutator method has a name that is the same as that of the attributes; it has a single parameter whose type is the same as that of the attributes; when invoked, it returns the instance of the structured type that is the same as the instance for which the method is invoked apart from that attributes having a value given by the argument on invocation.

Page 31: Agenda  TMA03  M877 Block 3 Databases with Objects

Structured TypeCREATE TYPE st_address AS( location VARCHAR(30),-- name or number place VARCHAR(30),-- road, street, etc site VARCHAR(30),–- town, city, etc area VARCHAR(30),–- county, state, etc mail_code VARCHAR(20) –- postcode, zipcode, etc)

DECLARE var_address st_address

SET var_address = NEW st_address() -- Constructor functionSET var_address = var_address.location('Meadowfield')SET var_address.location = 'Meadowfield' -- Mutatorvar_address.location() -- Observer

ALTER TYPE st_addressADD METHOD string_address()RETURNS VARCHAR(150)

CREATE METHOD string_address() FOR st_addressRETURN SELF.location()||', '||SELF.place()||','|| SELF.site() ||', '||SELF.area()||', '||SELF.mail_code()

Page 32: Agenda  TMA03  M877 Block 3 Databases with Objects

Subtypes

Subtypes provides Inheritance (single inheritance) Overriding Extension

Substitutablility allows an instance of a subtype to substitute an instance of the supertype.

An instance of a type is also an instance of all its supertypes.

Cast and transform functions are required for a subtype that has additional attributes.

Page 33: Agenda  TMA03  M877 Block 3 Databases with Objects

Subtypes

CREATE TYPE world_address

UNDER st_address AS

(country VARCHAR(30))

OVERRIDING METHOD string_address()

RETURNS VARCHAR(150)

CREATE METHOD string_address()

FOR world_address

RETURN (SELF AS st_address).string_address||','

||SELF. country()

Page 34: Agenda  TMA03  M877 Block 3 Databases with Objects

Composite Values – Collection Types

Collection type includes arrays and multisets. Collection

Is a value of a collection type Comprises zero or more elements The number of elements in a collection is its

cardinality A collection type is defined by a type

constructor and an element data type Each element in a collection is a value of

the element data type

Page 35: Agenda  TMA03  M877 Block 3 Databases with Objects

Arrays

Array is an ordered collection. Every element has an ordinal position.

An array has a maximum cardinality. Two arrays are equal if the elements in the

same position in each array are the same.

Page 36: Agenda  TMA03  M877 Block 3 Databases with Objects

Multisets

A multiset is an unordered collection. No means to reference individual element in a

multiset. Duplicate elements are allowed. No maximum cardinality. Two multisets are equal if they both contain

the same elements, and the same number of each element.

Page 37: Agenda  TMA03  M877 Block 3 Databases with Objects

Rows

A row type is defined as a sequence of fields where a field has a field name and data type.

A row type does not have any object-oriented properties – no methods and no encapsulation.

Main benefits: Enabling function to return multiple values. Collection of row type Collection of rows with column being a row

type, this nested table.

Page 38: Agenda  TMA03  M877 Block 3 Databases with Objects

Array, Multiset, Row Type

--Arraytel_nums VARCHAR(15) ARRAY[5]SET tel_num = ARRAY['0870 3330087', '0115 9625451',

'07721 123456']ARRAY(SELECT DISTINCT staff_name FROM staff, student WHERE staff_no = counsellor_no)

--MultisetSELECT counsellor_no, COLLECT(student_name) AS students FROM studentGROUP BY counsellor_no

--Row typeDECLARE row_address ROW( position VARCHAR(30), place VARCHAR(30), site VARCHAR(30, area VARCHAR(30), mail_code VARCHAR(20))

Page 39: Agenda  TMA03  M877 Block 3 Databases with Objects

Using Structured Types with Tables

Page 40: Agenda  TMA03  M877 Block 3 Databases with Objects

Usage of Structured Type

Structured type can be used in two different ways when defining tables: Define columns of a table

Capable of representing 'objects' in tables. Require Cast and Transform functions to

support interaction with application processes.

Define rows of a table Attributes of the structured type becomes

columns of the table (typed table). Capable of being a referenceable table using

the REF type.

Page 41: Agenda  TMA03  M877 Block 3 Databases with Objects

Structured Types for Columns

CREATE TABLE staff

( staff_no CHAR(4),

staff_name VARCHAR(12) NOT NULL,

staff_address st_address,

staff_region CHAR(1),

PRIMARY KEY (staff_no)

)

SELECT staff_name, staff_address.location() AS location,

staff_address.place() AS place,

staff_address.site() AS site

FROM staff

WHERE staff_address.area() = 'Wessex'

Page 42: Agenda  TMA03  M877 Block 3 Databases with Objects

Structured Types for Tables

CREATE TYPE st_student AS( student_id CHAR(3), student_name VARCHAR(12) NOT NULL, student_address st_address, registration_date DATE NOT NULL, student_region CHAR(1)) METHOD derive_region() RETURNS CHAR(1)

CREATE TABLE student OF st_student( PRIMARY KEY(student_id), REF IS row_id SYSTEM GENERATED)