Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda  TMA03  M877 Block 3 Databases with Objects.

Similar presentations


Presentation on theme: "Agenda  TMA03  M877 Block 3 Databases with Objects."— Presentation transcript:

1 Agenda  TMA03  M877 Block 3 Databases with Objects

2 Object-Oriented Technology

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

4 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

5 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'

6 Object Identifier - OIDs

7 Relationships  In object-oriented language, relationship (association) between classes are represented via instance variables. Department Salaried Employee DepartmentHeadDepartmentMembers Merit Salaried Employee

8 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."

9 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

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

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

12 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."

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

14 Object-Oriented Database Technology

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

16 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

17 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."; };

18 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 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." };

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

20 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."; };

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

22 Why Objects and Databases?

23 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).

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

25 SQL 'Objects' – (Object Relational Database)

26 User-Defined Types  Object capabilities of SQL are provided by User- Defined Types (UDT).  There are two kinds of UDT: Distinct type Structured type

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

28 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'

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

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

31 Structured Type CREATE 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 function SET var_address = var_address.location('Meadowfield') SET var_address.location = 'Meadowfield' -- Mutator var_address.location() -- Observer ALTER TYPE st_address ADD METHOD string_address() RETURNS VARCHAR(150) CREATE METHOD string_address() FOR st_address RETURN SELF.location()||', '||SELF.place()||','|| SELF.site() ||', '||SELF.area()||', '||SELF.mail_code()

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

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

34 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

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

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

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

38 Array, Multiset, Row Type --Array tel_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) --Multiset SELECT counsellor_no, COLLECT(student_name) AS students FROM student GROUP BY counsellor_no --Row type DECLARE row_address ROW( position VARCHAR(30), place VARCHAR(30), site VARCHAR(30, area VARCHAR(30), mail_code VARCHAR(20))

39 Using Structured Types with Tables

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

41 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'

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


Download ppt "Agenda  TMA03  M877 Block 3 Databases with Objects."

Similar presentations


Ads by Google