Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 7 Object-Oriented Database.

Similar presentations


Presentation on theme: "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 7 Object-Oriented Database."— Presentation transcript:

1 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 7 Object-Oriented Database

2 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 2 Chapter Outline 1 Overview of O-O Concepts 2 O-O Identity, Object Structure and Type Constructors 3 Encapsulation of Operations, Methods and Persistence 4 Type and Class Hierarchies and Inheritance 5 Other O-O Concepts 6 Overview of the Object Model ODMG 7 The Object Definition Language DDL 8 The Object Query Language OQL

3 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 3 20.1 Overview of Object-Oriented Concepts(1) Main Claim: OO databases try to maintain a direct correspondence between real-world and database objects so that objects do not lose their integrity and identity and can easily be identified and operated upon Object: Two components: state (value) and behavior (operations) Similar to program variable in programming language, except that it will typically have a complex data structure as well as specific operations defined by the programmer

4 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 4 Overview of Object-Oriented Concepts (2) In OO databases, objects may have an object structure of arbitrary complexity in order to contain all of the necessary information that describes the object. In contrast, in traditional database systems, information about a complex object is often scattered over many relations or records, leading to loss of direct correspondence between a real- world object and its database representation.

5 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 5 Overview of Object-Oriented Concepts (4) Some OO models insist that all operations a user can apply to an object must be predefined. This forces a complete encapsulation of objects. To encourage encapsulation, an operation is defined in two parts: signature or interface of the operation, specifies the operation name and arguments (or parameters). method or body, specifies the implementation of the operation.

6 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 6 Overview of Object-Oriented Concepts (5) Operations can be invoked by passing a message to an object, which includes the operation name and the parameters. The object then executes the method for that operation. This encapsulation permits modification of the internal structure of an object, as well as the implementation of its operations, without the need to disturb the external programs that invoke these operations

7 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 7 Overview of Object-Oriented Concepts (6) Some OO systems provide capabilities for dealing with multiple versions of the same object (a feature that is essential in design and engineering applications). For example, an old version of an object that represents a tested and verified design should be retained until the new version is tested and verified: very crucial for designs in manufacturing process control, architecture, software systems …..

8 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 8 Overview of Object-Oriented Concepts (7) Operator polymorphism: This refers to an operation’s ability to be applied to different types of objects; in such a situation, an operation name may refer to several distinct implementations, depending on the type of objects it is applied to. This feature is also called operator overloading

9 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 9 20.2 Object Identity, Object Structure, and Type Constructors (1) Unique Identity: An OO database system provides a unique identity to each independent object stored in the database. This unique identity is typically implemented via a unique, system-generated object identifier, or OID The main property required of an OID is that it be immutable Specifically, the OID value of a particular object should not change. This preserves the identity of the real-world object being represented.

10 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 10 Object Identity, Object Structure, and Type Constructors (2) Type Constructors: In OO databases, the state (current value) of a complex object may be constructed from other objects (or other values) by using certain type constructors. The three most basic constructors are atom, tuple, and set. Other commonly used constructors include list, bag, and array. The atom constructor is used to represent all basic atomic values, such as integers, real numbers, character strings, Booleans, and any other basic data types that the system supports directly.

11 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 11 Object Identity, Object Structure, and Type Constructors (3) Example 1 One possible relational database state corresponding to COMPANY schema

12 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 12 Object Identity, Object Structure, and Type Constructors (4) Example 1 (contd.):

13 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 13 Object Identity, Object Structure, and Type Constructors (5) Example 1 (contd.)

14 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 14 Object Identity, Object Structure, and Type Constructors (6) Example 1 (contd.) We use i 1, i 2, i 3,... to stand for unique system- generated object identifiers. Consider the following objects: o 1 = (i 1, atom, ‘Houston’) o 2 = (i 2, atom, ‘Bellaire’) o 3 = (i 3, atom, ‘Sugarland’) o 4 = (i 4, atom, 5) o 5 = (i 5, atom, ‘Research’) o 6 = (i 6, atom, ‘1988-05-22’) o 7 = (i 7, set, {i1, i2, i3})

15 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 15 Object Identity, Object Structure, and Type Constructors (7) Example 1(contd.) o 8 = (i 8, tuple, ) o 9 = (i 9, tuple, ) o 10 = (i 10, set, {i 12, i 13, i 14 }) o 11 = (i 11, set {i 15, i 16, i 17 }) o 12 = (i 12, tuple, )...

16 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 16 Object Identity, Object Structure, and Type Constructors (8) Example 1 (contd.) The first six objects listed in this example represent atomic values. Object seven is a set-valued object that represents the set of locations for department 5; the set refers to the atomic objects with values {‘Houston’, ‘Bellaire’, ‘Sugarland’}. Object 8 is a tuple-valued object that represents department 5 itself, and has the attributes DNAME, DNUMBER, MGR, LOCATIONS, and so on.

17 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 17 Object Identity, Object Structure, and Type Constructors (9) Example 2: This example illustrates the difference between the two definitions for comparing object states for equality. o 1 = (i 1, tuple, ) o 2 = (i 2, tuple, ) o 3 = (i 3, tuple, ) o 4 = (i 4, atom, 10) o 5 = (i 5, atom, 10) o 6 = (i 6, atom, 20)

18 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 18 Object Identity, Object Structure, and Type Constructors (10) Example 2 (contd.): In this example, The objects o1 and o2 have equal states, since their states at the atomic level are the same but the values are reached through distinct objects o4 and o5. However, the states of objects o1 and o3 are identical, even though the objects themselves are not because they have distinct OIDs. Similarly, although the states of o4 and o5 are identical, the actual objects o4 and o5 are equal but not identical, because they have distinct OIDs.

19 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 19 Object Identity, Object Structure, and Type Constructors (11)

20 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 20 Object Identity, Object Structure, and Type Constructors (12)

21 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 21 20.3 Encapsulation of Operations, Methods, and Persistence (1) Encapsulation One of the main characteristics of OO languages and systems Related to the concepts of abstract data types and information hiding in programming languages

22 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 22 Encapsulation of Operations, Methods, and Persistence (2) Specifying Object Behavior via Class Operations: The main idea is to define the behavior of a type of object based on the operations that can be externally applied to objects of that type. In general, the implementation of an operation can be specified in a general-purpose programming language that provides flexibility and power in defining the operations.

23 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 23 Encapsulation of Operations, Methods, and Persistence (3) Specifying Object Behavior via Class Operations (contd.): For database applications, the requirement that all objects be completely encapsulated is too stringent. One way of relaxing this requirement is to divide the structure of an object into visible and hidden attributes (instance variables).

24 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 24 Encapsulation of Operations, Methods, and Persistence (4)

25 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 25 Encapsulation of Operations, Methods, and Persistence (5) Specifying Object Persistence via Naming and Reachability: Naming Mechanism: Assign an object a unique persistent name through which it can be retrieved by this and other programs. Reachability Mechanism: Make the object reachable from some persistent object. An object B is said to be reachable from an object A if a sequence of references in the object graph lead from object A to object B.

26 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 26 Encapsulation of Operations, Methods, and Persistence (6) Specifying Object Persistence via Naming and Reachability (contd.): In traditional database models such as relational model or EER model, all objects are assumed to be persistent. In OO approach, a class declaration specifies only the type and operations for a class of objects. The user must separately define a persistent object of type set (DepartmentSet) or list (DepartmentList) whose value is the collection of references to all persistent DEPARTMENT objects

27 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 27 Encapsulation of Operations, Methods, and Persistence (7)

28 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 28 20.4 Type and Class Hierarchies and Inheritance (1) Type (class) Hierarchy A type in its simplest form can be defined by giving it a type name and then listing the names of its visible (public) functions When specifying a type in this section, we use the following format, which does not specify arguments of functions, to simplify the discussion: TYPE_NAME: function, function,..., function Example: PERSON: Name, Address, Birthdate, Age, SSN

29 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 29 Type and Class Hierarchies and Inheritance (2) Subtype: When the designer or user must create a new type that is similar but not identical to an already defined type Supertype: It inherits all the functions of the subtype

30 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 30 Type and Class Hierarchies and Inheritance (3) Example (1): PERSON: Name, Address, Birthdate, Age, SSN EMPLOYEE: Name, Address, Birthdate, Age, SSN, Salary, HireDate, Seniority STUDENT: Name, Address, Birthdate, Age, SSN, Major, GPA OR: EMPLOYEE subtype-of PERSON: Salary, HireDate, Seniority STUDENT subtype-of PERSON: Major, GPA

31 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 31 Type and Class Hierarchies and Inheritance (4) Example (2): Consider a type that describes objects in plane geometry, which may be defined as follows: GEOMETRY_OBJECT: Shape, Area, ReferencePoint Now suppose that we want to define a number of subtypes for the GEOMETRY_OBJECT type, as follows: RECTANGLE subtype-of GEOMETRY_OBJECT: Width, Height TRIANGLE subtype-of GEOMETRY_OBJECT: Side1, Side2, Angle CIRCLE subtype-of GEOMETRY_OBJECT: Radius

32 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 32 Type and Class Hierarchies and Inheritance (5) Example (2) (contd.): An alternative way of declaring these three subtypes is to specify the value of the Shape attribute as a condition that must be satisfied for objects of each subtype: RECTANGLE subtype-of GEOMETRY_OBJECT (Shape=‘rectangle’): Width, Height TRIANGLE subtype-of GEOMETRY_OBJECT (Shape=‘triangle’): Side1, Side2, Angle CIRCLE subtype-of GEOMETRY_OBJECT (Shape=‘circle’): Radius

33 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 33 Type and Class Hierarchies and Inheritance (6) Extents: In most OO databases, the collection of objects in an extent has the same type or class. However, since the majority of OO databases support types, we assume that extents are collections of objects of the same type for the remainder of this section. Persistent Collection: This holds a collection of objects that is stored permanently in the database and hence can be accessed and shared by multiple programs Transient Collection: This exists temporarily during the execution of a program but is not kept when the program terminates

34 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 34 20.6 Other Objected-Oriented Concepts (1) Polymorphism (Operator Overloading): This concept allows the same operator name or symbol to be bound to two or more different implementations of the operator, depending on the type of objects to which the operator is applied For example + can be: Addition in integers Concatenation in strings (of characters)

35 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 35 Other Objected-Oriented Concepts (2) Multiple Inheritance and Selective Inheritance Multiple inheritance in a type hierarchy occurs when a certain subtype T is a subtype of two (or more) types and hence inherits the functions (attributes and methods) of both supertypes. For example, we may create a subtype ENGINEERING_MANAGER that is a subtype of both MANAGER and ENGINEER. This leads to the creation of a type lattice rather than a type hierarchy.

36 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 36 Other Objected-Oriented Concepts (3) Versions and Configurations Many database applications that use OO systems require the existence of several versions of the same object There may be more than two versions of an object. Configuration: A configuration of the complex object is a collection consisting of one version of each module arranged in such a way that the module versions in the configuration are compatible and together form a valid version of the complex object.

37 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 37 21.1 The Object Model of ODMG Provides a standard model for object databases Supports object definition via ODL Supports object querying via OQL Supports a variety of data types and type constructors

38 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 38 ODMG Objects and Literals The basic building blocks of the object model are Objects Literals An object has four characteristics 1.Identifier: unique system-wide identifier 2.Name: unique within a particular database and/or program; it is optional 3.Lifetime: persistent vs. transient 4.Structure: specifies how object is constructed by the type constructor and whether it is an atomic object

39 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 39 ODMG Literals A literal has a current value but not an identifier Three types of literals 1.atomic: predefined; basic data type values (e.g., short, float, boolean, char ) 2.structured: values that are constructed by type constructors (e.g., date, struct variables) 3.collection: a collection (e.g., array) of values or objects

40 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 40 ODMG Interface Definition: An Example Note: interface is ODMG’s keyword for class/type interface Date:Object { enum weekday{sun,mon,tue,wed,thu,fri,sat}; enum Month{jan,feb,mar,…,dec}; unsigned short year(); unsigned short month(); unsigned short day(); … boolean is_equal(in Date other_date); };

41 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 41 Built-in Interfaces for Collection Objects A collection object inherits the basic collection interface, for example: cardinality() is_empty() insert_element() remove_element() contains_element() create_iterator()

42 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 42 Collection Types Collection objects are further specialized into types like a set, list, bag, array, and dictionary Each collection type may provide additional interfaces, for example, a set provides: create_union() create_difference() is_subset_of( is_superset_of() is_proper_subset_of()

43 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 43 Object Inheritance Hierarchy

44 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 44 Atomic Objects Atomic objects are user-defined objects and are defined via keyword class An example: class Employee (extent all_emplyees key ssn) { attribute string name; attribute string ssn; attribute short age; relationship Dept works_for; void reassign(in string new_name); }

45 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 45 Class Extents An ODMG object can have an extent defined via a class declaration Each extent is given a name and will contain all persistent objects of that class For Employee class, for example, the extent is called all_employees This is similar to creating an object of type Set and making it persistent

46 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 46 Class Key A class key consists of one or more unique attributes For the Employee class, the key is ssn Thus each employee is expected to have a unique ssn Keys can be composite, e.g., (key dnumber, dname)

47 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 47 Object Factory An object factory is used to generate individual objects via its operations An example: interface ObjectFactory { Object new (); }; new() returns new objects with an object_id One can create their own factory interface by inheriting the above interface

48 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 48 Interface and Class Definition ODMG supports two concepts for specifying object types: Interface Class There are similarities and differences between interfaces and classes Both have behaviors (operations) and state (attributes and relationships)

49 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 49 ODMG Interface An interface is a specification of the abstract behavior of an object type State properties of an interface (i.e., its attributes and relationships) cannot be inherited from Objects cannot be instantiated from an interface

50 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 50 ODMG Class A class is a specification of abstract behavior and state of an object type A class is Instantiable Supports “extends” inheritance to allow both state and behavior inheritance among classes Multiple inheritance via “extends” is not allowed

51 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 51 21.2 Object Definition Language ODL supports semantics constructs of ODMG ODL is independent of any programming language ODL is used to create object specification (classes and interfaces) ODL is not used for database manipulation

52 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 52 ODL Examples (1) A Very Simple Class A very simple, straightforward class definition (all examples are based on the university schema presented in Chapter 4): class Degree { attribute string college; attribute string degree; attribute string year; };

53 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 53 ODL Examples (2) A Class With Key and Extent A class definition with “extent”, “key”, and more elaborate attributes; still relatively straightforward class Person (extent persons key ssn) { attribute struct Pname {string fname …} name; attribute string ssn; attribute date birthdate; … short age(); }

54 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 54 ODL Examples (3) A Class With Relationships Note extends (inheritance) relationship Also note “inverse” relationship class Faculty extends Person (extent faculty) { attribute string rank; attribute float salary; attribute string phone; … relationship Dept works_in inverse Dept::has_faculty; relationship set advises inverse GradStu::advisor; void give_raise (in float raise); void promote (in string new_rank); };

55 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 55 Inheritance via “:” – An Example interface Shape { attribute struct point {…} reference_point; float perimeter (); … }; class Triangle: Shape (extent triangles) { attribute short side_1; attribute short side_2; … };

56 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 56 21.3 Object Query Language OQL is DMG’s query language OQL works closely with programming languages such as C++ Embedded OQL statements return objects that are compatible with the type system of the host language OQL’s syntax is similar to SQL with additional features for objects

57 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 57 Simple OQL Queries Basic syntax: select…from…where… SELECT d.name FROM d in departments WHEREd.college = ‘Engineering’; An entry point to the database is needed for each query An extent name (e.g., departments in the above example) may serve as an entry point

58 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 58 Iterator Variables Iterator variables are defined whenever a collection is referenced in an OQL query Iterator d in the previous example serves as an iterator and ranges over each object in the collection Syntactical options for specifying an iterator: d in departments departments d departments as d

59 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 59 Data Type of Query Results The data type of a query result can be any type defined in the ODMG model A query does not have to follow the select…from…where… format A persistent name on its own can serve as a query whose result is a reference to the persistent object. For example, departments; whose type is set

60 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 60 Path Expressions A path expression is used to specify a path to attributes and objects in an entry point A path expression starts at a persistent object name (or its iterator variable) The name will be followed by zero or more dot connected relationship or attribute names E.g., departments.chair;

61 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 61 Views as Named Objects The define keyword in OQL is used to specify an identifier for a named query The name should be unique; if not, the results will replace an existing named query Once a query definition is created, it will persist until deleted or redefined A view definition can include parameters

62 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 62 An Example of OQL View A view to include students in a department who have a minor: define has_minor(dept_name) as select s from s in students where s.minor_in.dname=dept_name has_minor can now be used in queries

63 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 63 Single Elements from Collections An OQL query returns a collection OQL’s element operator can be used to return a single element from a singleton collection that contains one element: element (select d from d in departments where d.dname = ‘Software Engineering’); If d is empty or has more than one elements, an exception is raised

64 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 64 Collection Operators OQL supports a number of aggregate operators that can be applied to query results The aggregate operators and operate over a collection and include min, max, count, sum, avg count returns an integer; others return the same type as the collection type

65 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 65 An Example of an OQL Aggregate Operator To compute the average GPA of all seniors majoring in Business: avg (select s.gpa from s in students where s.class = ‘senior’ and s.majors_in.dname =‘Business’);

66 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 66 Membership and Quantification OQL provides membership and quantification operators: ( e in c ) is true if e is in the collection c ( for all e in c: b ) is true if all e elements of collection c satisfy b ( exists e in c: b ) is true if at least one e in collection c satisfies b

67 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 67 An Example of Membership To retrieve the names of all students who completed CS101: select s.name.fname s.name.lname from s in students where 'CS101' in (select c.name from c in s.completed_sections.section.of_course);

68 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 68 Ordered Collections Collections that are lists or arrays allow retrieving their first, last, and ith elements OQL provides additional operators for extracting a sub-collection and concatenating two lists OQL also provides operators for ordering the results

69 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 69 An Example of Ordered Operation To retrieve the last name of the faculty member who earns the highest salary: first (select struct (faculty: f.name.lastname, salary f.salary) from f in faculty ordered by f.salary desc);

70 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7- 70 Grouping Operator OQL also supports a grouping operator called group by To retrieve average GPA of majors in each department having >100 majors: select deptname, avg_gpa: avg (select p.s.gpa from p in partition) from s in students group by deptname: s.majors_in.dname having count (partition) > 100


Download ppt "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 7 Object-Oriented Database."

Similar presentations


Ads by Google