45
Object Oriented Databases Bringing objects into databases

Object Oriented Databases Bringing objects into databases

Embed Size (px)

Citation preview

Page 1: Object Oriented Databases Bringing objects into databases

Object Oriented Databases

Bringing objects into databases

Page 2: Object Oriented Databases Bringing objects into databases

An object database schema

ADDRESS Street: String City: String

PERSON SS#: StringName: StringAddress: Address

EMPLOYEE ISA PERSON

Salary: IntegerBoss: EmployeeDepartments: {Department}

COMPANY Name: String Head quarters: Address Departments: {Department} CEO: Employee

DEPARTMENT Name: String Office: Address Boss: Employee Employees: {Employee}

Page 3: Object Oriented Databases Bringing objects into databases

An object database query

SELECT e.NameFROM e in EMPLOYEE, d in

DEPARTMENTWHERE e in d.EmployeesAND e.Address.City = d.Office.City

Page 4: Object Oriented Databases Bringing objects into databases

Object database concepts

Simple values

Complex values

Objects

TypesClasses

25 #11‘bo’

{25,7} [a:1,b:2]

<#11, {25,7}>

PersonCar [ss#:String,age:Integer]

Page 5: Object Oriented Databases Bringing objects into databases

Simple and complex values

A is the set of all attributes

O is the set of all object identifiers

D is the union of integers, reals, strings, and booleans

Elements in O U D are called simple values

Elements of the form [A1:w1, …, An:wn] are tuple values

where Ai is an attribute and wi a value

Elements of the form {w1, …, wn} are set values

A complex value is a tuple value or a set value

Page 6: Object Oriented Databases Bringing objects into databases

Objects

An object is a pair

(o, w)

o is an object identifierw is a value

Page 7: Object Oriented Databases Bringing objects into databases

Examples of objects

A = {SS#, Name, Address, Main office, Departments, CEO}O = {#11, #12, #13, #14, #21, #31, #41}

(#11, [SS#: ‘420101-1100’, Name: ‘Bo Ek’, Address: [Street: ‘Storg. 4’, Town: ‘Karlstad’]])

(#14,[Name: ‘Acme’,Head office: [Street: ‘Storg. 12’, Town: ‘Karlstad’],Departments: {#21, #31, #41},CEO: #11])

Page 8: Object Oriented Databases Bringing objects into databases

Value and object equality

Value equalityTwo objects are equal if their values are equal(o1, w1) = (o2, w2) iff w1 = w2

Object equalityTwo objects are equal if their object identifiers are the same(o1, w1) = = (o2, w2) iff o1 = o2

Page 9: Object Oriented Databases Bringing objects into databases

Surface and deep equality

Surface equality: equality without substituting object references

Deep equality: equality after substituting object references by values

w1 = [A: o1]

w2 = [A: o2]

(o1, w)

(o2, w)

where o1 o2

w1 not surface equal to w2w1 deep equal to w2

Page 10: Object Oriented Databases Bringing objects into databases

Surface and deep equality

w1 = [A: o1, B: 25]

w2 = [A: o2, B: o3]

w3 = [A: o2, B: o4]

(o1, 25)

(o2, 50)

(o3, 25)

(o4, o1)

w1 deep equal to w2 ?

w1 deep equal to w3?

w2 deep equal to w3?

Page 11: Object Oriented Databases Bringing objects into databases

Types and classes

Intuitions:Values with the same structure are described

by a typeObject identifiers for similar objects are

grouped into a class

A class can be associated with a type to specify the structure of the objects of the class

A class can be used as a type for typing references between objects

Page 12: Object Oriented Databases Bringing objects into databases

Types

Let K be a set of classes.

Base types: {integer, float, string, boolean}Reference types: KTuple types: elements of the form

[A1:T1, …, An:Tn], where Ai is an attribute and Ti en typa type

Set types: {T}, where T is a type

A complex type is a tuple type or a set type

Page 13: Object Oriented Databases Bringing objects into databases

An example type

K = {Car, Company, City}

EMPLOYEESS#: IntegerName: StringLives in: CityWorks at: CompanyOwns: {Car}

Base type

Reference type

Set type

Tuple type

Page 14: Object Oriented Databases Bringing objects into databases

Object database schema

An object database schema consists of:

A set of class names KFor each k in K, a type that specifies

the structure of k

Page 15: Object Oriented Databases Bringing objects into databases

Object database extension

Given an object database schema S, an extension for S associates each k in K with a set of object identifiers

Page 16: Object Oriented Databases Bringing objects into databases

An example extension

Animal

Mammal

Bird

Dog

K

#1

#2

#3

#4

#5#1

#1

#2

#2

#3 #4

#5

Page 17: Object Oriented Databases Bringing objects into databases

Types, classes, objects,and values

Person

Bil

#11

#12

#22

#21

[Regnr: String, Färg: String, Ägare: Person]

[Pnr: String, Namn: String, Lön: Integer]

[Pnr: ‘5501010415’, Namn: ‘Per Svensson’, Lön: 25000]

[Regnr: ‘ABC123’, Färg: ‘Röd’, Ägare: #11]

K Types

WO

Page 18: Object Oriented Databases Bringing objects into databases

Query languages

AllmängiltighetDeklarativitetOptimerbarhetSlutenhetUttryckskraftUtvidgningsbarhet

Page 19: Object Oriented Databases Bringing objects into databases

Two simple queries

SELECT aFROM a in EMPLOYEEWHERE Salary > 20000

SELECT SS#, NameFROM EMPLOYEEWHERE Salary > 20000

Page 20: Object Oriented Databases Bringing objects into databases

Path expressions (dot notation)

SELECT NameFROM CompanyWHERE Head_quarters.City = “London”

SELECT Name, Head_quarters.StreetFROM CompanyWHERE Head_quarters.City = “London”

Page 21: Object Oriented Databases Bringing objects into databases

Sets in queries

What does this query mean?

SELECT cFROM c in COMPANYWHERE CEO IN

(SELECT e FROM e in EMPLOYEE WHERE Employee.Address.City =

“Lund”)

Page 22: Object Oriented Databases Bringing objects into databases

Sets in queries

SELECT c.NameFROM c in COMPANYWHERE c.Departments.Office.City =

{“London”, “Lund”}

Page 23: Object Oriented Databases Bringing objects into databases

Sets and dot notation

SELECT c.Departments.Employees.Salary

FROM c in COMPANY

c.Departments.Employees.Salary

{f1, f2, f3}

{a1, a2} {a3, a4} {a5}

100 200 150 100 150{f1, f2, f3}

{{a1, a2}, {a3, a4}, {a5}} ?

{a1, a2, a3, a4, a5} ?

Page 24: Object Oriented Databases Bringing objects into databases

Sets and dot notation

SELECT c.Departments.Employees.SalaryFROM c in COMPANY

{f1, f2, f3}

{a1, a2} {a3, a4} {a5}

100 200 150 100 150

Answer: {100, 150, 200}

Page 25: Object Oriented Databases Bringing objects into databases

Exercises

Which employees work at the same departments as their bosses?

Which (pair of) employees work at the same departments?

Which companies do not have any employee living in London?

In which companies do all employees live in London?

Page 26: Object Oriented Databases Bringing objects into databases

Exercises

Which employees work only at departments that are situated in London?

Which employees do not work at a department in London?

These queries become harder if the attribute “Departments” is removed. Try to write the queries also without this attribute.

Page 27: Object Oriented Databases Bringing objects into databases

Exercises

In which companies are there employees who live in cities where the company does not have any department?

In which companies does the boss of each employee live in the same city as the employee?

Page 28: Object Oriented Databases Bringing objects into databases

Which employees work at the same departments as their bosses?

Page 29: Object Oriented Databases Bringing objects into databases

Which (pair of) employees work at the same departments?

Page 30: Object Oriented Databases Bringing objects into databases

Which companies do not have any employee living in London?

Page 31: Object Oriented Databases Bringing objects into databases

In which companies do all employees live in London?

Page 32: Object Oriented Databases Bringing objects into databases

Which employees work only at departments that are situated in London?

Page 33: Object Oriented Databases Bringing objects into databases

Which employees do not work at a department in London?

Page 34: Object Oriented Databases Bringing objects into databases

In which companies are there employees who live in cities where the company does not have any department?

Page 35: Object Oriented Databases Bringing objects into databases

In which companies does the boss of each employee live in the same city as the employee?

Page 36: Object Oriented Databases Bringing objects into databases

Which employees work at the same departments as their bosses?

SELECT e.NameFROM e in EMPLOYEEWHERE e.Departments =

e.Boss.Departments

Page 37: Object Oriented Databases Bringing objects into databases

Which (pair of) employees work at the same departments?

SELECT e1.Name, e2.NameFROM e1 in EMPLOYEE, e2 in EMPLOYEEWHERE e1.Departments = e2.Departments

Page 38: Object Oriented Databases Bringing objects into databases

Which companies do not have any employee living in London?

SELECT cFROM c in COMPANYWHERE ‘London’ NOT IN

c.Departments.Employees.Address.City

Page 39: Object Oriented Databases Bringing objects into databases

In which companies do all employees live in London?

SELECT cFROM c in COMPANYWHERE

c.Departments.Employees.Address.City = {‘London’}

Page 40: Object Oriented Databases Bringing objects into databases

Which employees work only at departments that are situated in London?

SELECT e.NameFROM e IN EMPLOYEEWHERE NOT EXISTS

(SELECT d FROM DEPARTMENTWHERE e in d.EmployeesAND d.Office.City <> ‘London’)

SELECT e.NameFROM e in EMPLOYEEWHERE e.Departments.Office.City = {‘London’}

Page 41: Object Oriented Databases Bringing objects into databases

Which employees do not work at a department in London?

SELECT e.NameFROM e in EMPLOYEEWHERE NOT EXISTS

(SELECT d FROM d in DEPARTMENTWHERE e in d.EmployeesAND d.Office.City = ‘London’)

SELECT e.NameFROM e in EMPLOYEEWHERE ‘London’ NOT IN e.Departments.Office.City

Page 42: Object Oriented Databases Bringing objects into databases

In which companies are there employees who live in cities where the company does not have any department?

SELECT cFROM c IN COMPANYWHERE c.Departments.Employees.Address.City MINUS

c.Departments.Office.City <> Ø

Page 43: Object Oriented Databases Bringing objects into databases

In which companies does the boss of each employee live in the same city as the employee?

SELECT cFROM c in COMPANYWHERE NOT EXISTS

(SELECT eFROM e in EMPLOYEEWHERE e IN c.Departments.EmployeesAND e.Boss.Address.City <> e.Address.City)

Page 44: Object Oriented Databases Bringing objects into databases

Which employees work at a department in a city where they do not live?

SELECT e.NameFROM e in EMPLOYEE, d in DEPARTMENTWHERE e in d.EmployeesAND d.Office.City <> e.Address.City

Page 45: Object Oriented Databases Bringing objects into databases

Which employees live in the same city as (at least one of) their boss(es)?

SELECT e.NameFROM e in EMPLOYEE, d in DEPARTMENTWHERE e in d.Employees AND e.Address.City =

d.Boss.Address.City