Object-Oriented Databases Lesson 12

Download Report

Transcript Object-Oriented Databases Lesson 12

Lesson 12
Object-Oriented
Databases
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
2
Object-Oriented Data Model

Objects
 encapsulate
 interact
code & data into a single unit
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)
3
Object-Oriented Databases
City
Street
Address
Sal_Hist
FID
Fname
SSNO
Name
Salary
Year
State
N
Advises
1
Student
Faculty
M
Teaches
N
1
Has
Sname
1
Spouse
Birthdate
Figure. An Entity Relationship Schema Diagram
4
Object-Oriented Database
City
Street
Address
Sal_Hist
FID
Fname
SSNO
Name
Salary
Year
State
N
Advises
1
Student
Faculty
M
N
1
Teaches
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)
Has
1
Sname
Spouse
Figure. The Structure of a Nested Relational Database Schema NRDS
Birthdate
5
Object-Oriented Database
SSNO = 721009897
Address(Street, City, State)
= (90 N 70 E, Provo, Utah)
Name = Susan Tang
f2
s1
Fname = David Hays
Sal_Hist(Year, Salary)
={ (1991, 23K), (1994, 27K) }
FID = 5624
a1
SSNO = 123456789
Address(Street, City, State)
= (123 Perry, Orem, Utah)
Name = Joe Young
a2
s2
Fname = Chris Smith
Sal_Hist(Year, Salary)
= { (1989, 25K), (1993, 30K) }
FID = 2134
f1
t1
h1
sp1
Sname = Mary Smith
Birthday = July 15, 1955
Figure. An Instance of the Entity Relationship Schema
6
Object-Oriented Databases
City
Street
Address
Sal_Hist
FID
Fname
SSNO
Name
Salary
Year
State
N
Advises
1
Student
Faculty
M
N
Teaches
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 ObjectOriented Database Schema OODS
7
Object-Oriented
Databases
Salary
Year
Sal_Hist
FID
Fname
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_age
end
Faculty
1
Has
1
Sname
Spouse
Birthdate
Figure. The structure of an ObjectOriented Database Schema OODS
8
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
9
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
10
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 Department
type
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.
11
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)

12
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 instance
variables 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
13
Object-Oriented Data Model
person
employee
customer
officer
teller
secretary
Figure. Class hierarchy for the banking example.
person
employee
customer
officer
teller
full-time teller
secretary
part-time teller
full-time secretary
part-time secretary
Figure. Class hierarchy for full- and part-time employees.
14
Object-Oriented Data Model
person
employee
customer
full-time
officer
part-time
full-time teller
teller
part-time teller
secretary
full-time secretary
Figure. Class DAG for the banking example.
part-time secretary
15
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/method
is 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)
16
Object-Oriented Data Model
employee
board
chips
bus
device
instr-set
interfaces
Figure. Containment hierarchy for computer system design database.
17
OO Database Design by EER-to-OO Mapping
STEP 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
18
relevant.
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.
19
Type Phone: tuple ( area_code: integer,
number: integer);
Type Date: tuple ( year: integer,
month: integer,
Person
day: integer);
Class Person
type tuple ( ssn: string,
name: tuple ( firstname: string,
middlename: string,
Student
Faculty
lastname: string),
address: tuple (street: string,
apt_no: string,
city: string,
state: string,
Grad-Std
zipcode: string ),
birthdate: Date,
sex: character )
method age: integer
end
Figure. O2 class declarations for
Class Student inherit Person
part of the UNIVERSITY
type tuple ( class: string,
database (continued on
majors_in: Department,
next page)
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 ):
boolean
end
20
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
end
change_grade ( s: Student,
g: string)
class Course
type tuple ( cname: string,
cnumber: string,
cdescription: string,
sections: set ( Section ),
offering_dept: Department )
end
method update_description (new d: string )
Figure. (continued)
21
Object-Oriented Data Model
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.
22
Object-Oriented Data Model
EMPLOYEE: Name, Address, Birthdate, Age, SSN, Salary, HireDate, Seniority
STUDENT: Name, Address, Birthdate, Age, SSN, Major, GPA
EMPLOYEE subtype-of PERSON: Salary, HireDate, Seniority
STUDENT subtype-of PERSON: Major, GPA
PERSON: Name, Address, Birthdate, Age, SSN
GEOMETRY_OBJECT: Shape, Area, ReferencePoint
RECTANGLE subtype-of GEOMETRY_OBJECT: Width, Height
TRIANGLE subtype-of GEOMETRY_OBJECT: Side1, Side2, Angle
CIRCLE subtype-of GEOMETRY_OBJECT: Radius
GEOMETRY_OBJECT: Shape, Area, CenterPoint
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
23
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)
24