lesson 12 object-oriented databases. 2 object-oriented database n oodbm u db is a collection of...

Post on 17-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Lesson 12

Object-Oriented Databases

2

Object-Oriented Database

OODBM

DB is a collection of objects

each object represents a physical entity and an idea of interest to the DB application

new trend in data modeling and DB processing

Goal of Object-Oriented Data Modeling

maintain direct correspondence between real-world and database objects

use concepts of class or abstract data type to encapsulate structural properties & operations on types of objects

3

Object-Oriented Data Model Objects

encapsulate code & data into a single unit

interact w/ others by message passing

consist of variables that contain data for the objects; the value of each variables by itself is an

object

state of an object: set of values for the attribute called instance variable of the object

contain methods: a method is a body of code, also called behavior of an object

flexible in modifying the definitions (e.g., methods) & variables of objects (e.g., referencing)

4

Object-Oriented Databases

Student Faculty

Spouse

Name

Address

Street

City

State

SSNO

Advises

Teaches

FnameSal_Hist

Year Salary

FID

Has

Sname

Birthdate

Figure. An Entity Relationship Schema Diagram

N

M

1

N1

1

5

Object-Oriented Database

Student Faculty

Spouse

Name

Address

Street

City

State

SSNO

Advises

Teaches

FnameSal_Hist

Year Salary

FID

Has

Sname

Birthdate

N

M

1

N1

1

Student(Name, Address(Street, City, State), SSNO) Faculty(Fname, Sal_Hist(Year, Salary), FID) Advises(SSNO, FID) Teaches(SSNO, FID) Spouse(Sname, Birthdate)Has(FID, Sname)

Figure. The Structure of a Nested Relational Database Schema NRDS

6

Object-Oriented Database

Figure. An Instance of the Entity Relationship Schema

SSNO = 721009897Address(Street, City, State) = (90 N 70 E, Provo, Utah)

Name = Susan Tang

SSNO = 123456789Address(Street, City, State) = (123 Perry, Orem, Utah)

Name = Joe Young

Fname = David HaysSal_Hist(Year, Salary) ={ (1991, 23K), (1994, 27K) }FID = 5624

Fname = Chris SmithSal_Hist(Year, Salary) = { (1989, 25K), (1993, 30K) }FID = 2134

Sname = Mary SmithBirthday = July 15, 1955

s1

f2

a1

a2

t1

s2 f1

h1

sp1

7

Object-Oriented Databases

Type Date: tuple (Month: integer, Day: integer, Year: integer);Class Student type tuple (SSNO: string, /* key */ Name: string, Address: tuple (Street: string, City: string, State: string), Advisor: Faculty, Teachers: set (Faculty))end

Figure. The structure of an Object- Oriented Database Schema OODS

Student Faculty

Name

Address

Street

City

State

SSNO

Advises

Teaches

FnameSal_Hist

Year Salary

FID

N

M

1

N

8

Object-Oriented Databases

Class Faculty type tuple ( FID: string, /* key */ Fname: string, Sal_Hist: set ( tuple( Year: integer, Salary: real) ), Advisees: set (Student), Teaches: set (Student), Spouse_of: Spouse) method Add_advisee (Std: Student), Average_salary, Raise_curr_salary (Percent: real)End

Class Spouse type tuple ( Sname: string, /* key */ Birthdate: Date, Spouse_of: Faculty) method Compute_ageend Figure. The structure of an Object-

Oriented Database Schema OODS

Faculty

Spouse

FnameSal_Hist

Year Salary

FID

Has

Sname

Birthdate

1

1

9

name Susan_Tang: Student; /* a persistent root to hold a single Student object */name Joe_Young: Student; /* a persistent root to hold a single Student object */name David_Hays: Faculty; /* a persistent root to hold a single Faculty object */name Chris_Smith: Faculty; /* a persistent root to hold a single Faculty object */name Mary_Smith: Spouse; /* a persistent root to hold a single Spouse object */

Susan_Tang->SSNO = “721009897”,Susan_Tang->Name = “Susan Tang”,Susan_Tang->Address = tuple(Street: “90 N 70 E”, City: “Provo”, State: “Utah”),Susan_Tang->Advisor = David_Hays;

Joe_Young->SSNO = “123456789”,Joe_Young->Name = “Joe Young”,Joe_Young->Address = tuple(Street: “123 Perry”, City: “Orem”, State: “Utah”),Joe_Young->Advisor = Chris_Smith,Joe_Young->Teachers = set(Chris_Smith);

David_Hays->FID = “5624”,David_Hays->Fname = “David Hays”,David_Hays->Sal_Hist = set ( tuple (Year: 1991, Salary: 23K), tuple (Year: 1994, Salary: 27K)),David_Hays->Advisees = set (Susan_Tang);

Chris_Smith->FID = “2134”,Chris_Smith->Fname = “Chris Smith”,Chris_Smith->Sal_Hist = set ( tuple (Year: 1989, Salary: 25K), tuple (Year: 1993, Salary: 30K)),Chris_Smith->Advisees = set (Joe_Young),Chris_Smith->Teaches = set (Joe_Young),Chris_Smith->Spouse_of = Mary_Smith;

Mary_Smith->Sname = “Mary Smith”,Mary_Smith->Birthdate = tuple (Month: 7, Day: 15, Year: 1995),Mary_Smith->Spouse_of = Chris_Smith;

Figure. An Instance of OODS

10

method body Add_advisee(std: Student) in class Faculty{self->Advisees += set(std); /* += is the set union operation used to add std to a set of advisees */}

method body Average_salary: float in class Faculty{float sum = 0;int cnt = 0;

for (fac in self->Sal_Hist) { sum += fac->salary; /* add up salary */ cnt++; }return(sum/cnt);}

method body Compute_age: integer in class Spouse /* Calculate a spouse’s age, */{ /* using spouse’s birthday and today’s date

*/int i = 0;Date d = today(); /* self: object for which the method is invoked */if (d->month < self->birthday->month || (d->month == self->birthday->month && d->day < self->birthday->day)) - - i;return(d->year - self->birthday->year + i);}

method body Raise_curr_salary(percent: float): float in class Faculty{Date d = today();

for (fac in self->Sal_Hist) /* self: object for which the method is invoked */ if (d->year == fac->year) { fac->salary = fac->salary * (1 + percent); break; }} Figure. Definitions of Methods in OODS using O2C

11

Define class Employee:type tuple( name: string, ssn: string, birthdate: Date, sex: char, dept: Department );

operations age(e: Employee): integer, create_new_emp: Employee, destroy_emp(e: Employee): boolean;

define class Departmenttype tuple( dname: string, dnumber: integer, mgr: tuple (manager: Employee, startdate: Date), locations: set(string), employees: set(Employee), projects: set(Project) )

operations number_of_emps(d: Department): integer, create_new_dept: Department, destroy_dept (d: Department): boolean, add_emp (d: Department, e: Employee): boolean, (* adds a new employee *) remove_emp (d: Department, e: Employee): boolean, (* removes an employee *);

Figure. Using OODDL to define Employee and Department classes.

12

Object-Oriented Data Model Objects

each object maintains unique identity, represented by object identifier generated by the system and is independent of its attributes values (tuple identity)

Classes primitive class: a class which has associated instances,

but no attributes, e.g., integer, string, and boolean correspond to abstract data types (encapsulate structural

properties of objects and specify valid operations on data of objects)

contain groups of similar objects, instances of a class objects in the same class share a common definition (may

have different values of variables)

13

Object-Oriented Data Model Class Hierarchy:

allows users to derive a new class (subclass) from an existing class (super class)

users may also specify additional attributes and methods for the subclass

Specialization (subclass of a class, e.g., student: undergraduate/graduate), structural inheritance &

behavioral inheritance

a) structural inheritance: subclass inherits instancevariables of its superclass (e.g., graduate student has name) but not vice versa

b) behavioral inheritance: subclass inherits all methods applied to its superclass (e.g., GPA can be computed) but not vice versa

14

Object-Oriented Data Modelperson

employee

officer teller secretary

customer

Figure. Class hierarchy for the banking example.

person

employee

officer teller secretary

customer

Figure. Class hierarchy for full- and part-time employees.

full-time teller part-time teller full-time secretary part-time secretary

15

Object-Oriented Data Modelperson

employee

customer

Figure. Class DAG for the banking example.

full-time part-time teller secretary

officer full-time teller part-time teller full-time secretary part-time secretary

16

Object-Oriented Data Model Single Inheritance: a class inherits attributes and methods

from only one class; a hierarchical structure

Multiple Inheritance: a subclass inherits variables and methods from multiple

superclass, a rooted directed graph structure

ambiguous inheritance problem: if same variable/methodis inherited from more than one superclass

Object Containment: ability to define complex/composite objects from previously

defined objects in a nested/hierarchical manner

non-hierarchical containment: an object is contained in several objects

allow data (objects) to be viewed in different ways (sub-part/ whole)

17

Object-Oriented Data Model

employee

Figure. Containment hierarchy for computer system design database.

board bus device instr-set

chips interfaces

18

OO Database Design by EER-to-OO MappingSTEP 1: Create an oo class for each EER class. The type of the OO class should include all

the attributes of the EER class by using a tuple constructor at the top level of the type. Multivalued attributes are declared by using the set, bag, or list constructors. If the values of the multivalued attribute for an object should be ordered, the list constructor is chosen; if duplicates are allowed, the bag constructor should be chosen. Composite attributes are mapped into a tuple constructor.

STEP 2: Add reference attributes for each binary relationship into the oo classes that participate in the relationship. The attributes may be created in one direction or in both directions. The attributes are single-valued for relationships in the 1:1 or N:1 direction; they are set-valued or list-valued for relationships in the 1:N or M:N direction. If a binary relationship is represented by references in both directions, declare the references to be inverses of one another, if such a facility exists. If relationship attributes exist, a tuple constructor can be used to create a structure of the form <reference, relationship attributes>, which is included instead of the reference attribute.

STEP 3: Include appropriate methods for each class. These are not available from the EER schema and must be added to the database design as needed. A constructor method should include code that checks any constraints that must hold when a new object is created. A destructor method should check any constraints that may be violated when an object is deleted. Other methods should include any further constraint checks that are relevant.

19

OO Database Design by EER-to-OO Mapping

STEP 4: An OO class that corresponds to a subclass in the EER schema inherits the type and methods of its superclass(es) in the OO schema. Its specific attributes and references are specified as discussed in steps 1 and 2.

STEP 5: Weak entity types that do not participate in any relationships except their identifying relationship can be mapped as though they were composite multivalued attributes of the owner entity type, by using the set(tuple(…)) constructor.

STEP 6: n-ary relationships with n > 2 can be mapped into a separate object type, with appropriate references to each participating object type. These references are based on mapping a 1:N relationship from each participating entity type to the n-ary relationship. M:N binary relationships may also use this option, if desired.

20

Type Phone: tuple ( area_code: integer, number: integer);Type Date: tuple ( year: integer, month: integer, day: integer);Class Person type tuple ( ssn: string, name: tuple ( firstname: string,

middlename: string, lastname: string), address: tuple (street: string,

apt_no: string, city: string, state: string, zipcode: string ), birthdate: Date, sex: character ) method age: integerendClass Student inherit Person type tuple ( class: string, majors_in: Department, minors_in: Department, registered_in: set (Section), transcript: set ( tuple ( grade: character, section: Section ))) method grade_point_average: real, change_class: boolean, change_major ( new_major: Department ):

booleanend

Figure. O2 class declarations for part of the UNIVERSITY database (continued on next page)

Person

Student Faculty

Grad-Std

21

Class Grad_Student inherit Student type tuple ( degrees: set ( tuple ( college: string, degree: string, year: integer )), advisor: Faculty )end

Class Faculty_Student inherit Student type tuple ( salary: real, rank: string, foffice: string, fphone: Phone,

grants: set ( string ), advise: set ( Student ),

belongs_to: set ( Department ), chair: Department teach: set( Section ))

method promotte_faculty (rank: string), give_raise ( percent: real )end

class Department type tuple ( dname: string, office: string, dphone: Phone, members: set ( Faculty ), major: set ( Student ),

minor: set ( Student ), chairperson: Faculty, courses: set ( Course ) ) method add_major ( s: Student ), remove_major ( s: Student )end

Class Section type tuple ( sec_num: integer, qtr: Quarter, year: Year, transcript: set(tuple(

stud: Student, grade: character)),

register: set( Student ), course: Course, teacher: Instructor )

method change_grade ( s: Student, g: string)end

class Course type tuple ( cname: string, cnumber: string, cdescription: string, sections: set ( Section ), offering_dept: Department )

method update_description (new d: string )end

Figure. (continued)

22

Q1: select tuple (fname: s.name.firstname, lname: s.name.lastname) from s in Student where s.majors_in.dname = “Computer Science”

Q2: select tuple (fname: s.name.firstname, lname: s.name.lastname) transcript: select tuple (

sec_no: sc.section.sec_num, quarter: sc.section.qtr, year: sc.section.year, grade: sc.grade) from sc in sec) from s in Student, sec in s.transcript where s.majors_in.dname = “Computer Science”

Figure. Two queries in O2SQL.

Object-Oriented Data Model

23

EMPLOYEE: Name, Address, Birthdate, Age, SSN, Salary, HireDate, SenioritySTUDENT: Name, Address, Birthdate, Age, SSN, Major, GPA

EMPLOYEE subtype-of PERSON: Salary, HireDate, SenioritySTUDENT subtype-of PERSON: Major, GPA

PERSON: Name, Address, Birthdate, Age, SSN

GEOMETRY_OBJECT: Shape, Area, ReferencePoint

RECTANGLE subtype-of GEOMETRY_OBJECT: Width, HeightTRIANGLE subtype-of GEOMETRY_OBJECT: Side1, Side2, AngleCIRCLE subtype-of GEOMETRY_OBJECT: Radius

GEOMETRY_OBJECT: Shape, Area, CenterPointRECTANGLE subtype-of GEOMETRY_OBJECT (Shape = ‘rectangle’): Width, HeightTRIANGLE subtype-of GEOMETRY_OBJECT (Shape = ‘triangle’): Side1, Side2, AngleCIRCLE subtype-of GEOMETRY_OBJECT (Shape = ‘circle’): Radius

Object-Oriented Data Model

24

Name All_Person: set (Person) /*a persistent root to hold all persistent Person objects*/

name John_Smith: Person; /*a persistent root to hold a single Person objects*/

run body {o2 Person p = new Person; /*creates a new Person object p*/

*p = tuple (ssn: “333445555”, name: tuple (firstname, “Franklin”, middlename: “T”, lastname: “Wong”), address: tuple (number: 638, street: “Voss Road”, city: “Houston”, state: “Texas”, zipcode: “77079”), birthdate: tuple (year: 1945, month: 12, day: 8), sex: M);

All_Person += set (p); /*p becomes persistent by attaching to persistent root*/

/*new values in persistent named object John Smith*/John_Smith->ssn = “123456789”,John_Smith->name: tuple(firstname: “John”, middlename: “B”, lastname: “Smith”),John_Smith->address: tuple(number: 731, street: “Fondren Road”, city: “Houston”, state: “Texas”, zipcode: “77036”),John_Smith->birthdate: tuple(year: 1955, month: 1, day: 9),John_Smith->sex: M;

}Figure. (continued)

top related