Title: Object Oriented Databases
1Object Oriented Databases
- Seminar 12
-
- Exploring ODMG - OQL
- Using Lambda-DB
2Example Schema in ODL
- module University
- interface Person
- attribute string name
- attribute string address
- attribute date birthDate
- class Lecturer Person (extent Lecturers)
- attribute short room
- relationship setltStudentgt tutees inverse
Studenttutor - relationship Department worksFor inverse
Departmentstaff - relationship setltUnitgt teaches inverse
UnittaughtBy - class Student Person (extent Students)
- attribute string major
- relationship Lecturer tutor inverse
Lecturertutees - relationship Course enrolledOn inverse
CoursehasStudents - relationship setltUnitgt takes inverse
UnittakenBy
3Object Creation the Database
// Department instances d1 Department(name"Co
mputing") d2 Department(name"Maths") d3
Department(name"Engineering") // Lecturer
instances Date_Type doBirth doBirth.year
1965 doBirth.month 12
doBirth.day 25 i1 Lecturer(name"Neil
Eliot", address "South Shields", birthDate
doBirth, worksFor d1) i2 Lecturer(name"Juli
a Robert", address "Durham", worksFor d2) i3
Lecturer(name"David Meyer", address
"Sunderland", worksFord1) i4
Lecturer(name"Alex Shearer", address
"Newcastle", worksFord3) i5
Lecturer(name"Norman Paton", address
"Northumberland", worksFor d3) i6
Lecturer(name"David Livingston", address
"Northumberland", worksFor d1) i7
Lecturer(name"M Akhtar Ali", address
"Newcastle", worksFord1) i8
Lecturer(name"Patric Patterson", address
"Newcastle", worksFor d2) // Course
instances c1 Course(name"Bsc Computing for
Business", offeredBy d1) c2
Course(name"MSc Computing Conversion",
offeredBy d1) c3 Course(name"Bsc
Mathematics", offeredBy d1) c4
Course(name"Bsc Electrical Engineering",
offeredBy d1) // Student objects s1
Student(name"Jane Smith", address "Durham",
major "Computing", tutor i1, enrolledOn
c1) s2 Student(name"Mark Brown", address
"Newcastle", major "Computing", tutor i3,
enrolledOn c2) s3 Student(name"Andrew
Tailor", address "Fenham", major "Maths",
tutor i2, enrolledOn c3) s4
Student(name"Samanta Fox", address "Gosforth",
major "Maths", tutor i1, enrolledOn c2) s5
Student(name"Sandra Falcon", address
"Jesmond", major "Engineering", tutor i5,
enrolledOn c4) s6 Student(name"Pedro
Sampaio", address "Morpeth", major
"Engineering", tutor i3, enrolledOn c1) s7
Student(name"Sarah Khan", address "Durham",
major "Computing", tutor i1, enrolledOn c1)
s8 Student(name"Ian Prat", address
"Newcastle", major "Computing", tutor i3,
enrolledOn c2) s9 Student(name"Andrew
Shah", address "Fenham", major "Maths",tutor
i2, enrolledOn c3) s10 Student(name"Alex
Fernandes", address "Gosforth", major "Maths",
tutor i1, enrolledOn c2) s11
Student(name"Hena Kattak", address "Jesmond",
major "Engineering", tutor i5, enrolledOn
c4) s12 Student(name"Maria Willian",
address "Morpeth", major "Engineering", tutor
i3, enrolledOn c1) // Unit objects u1
Unit(name "Advanced Databases", code "CM036")
u2 Unit(name "OO Modelling", code
"CM031") u3 Unit(name "Elementrary
Algebra", code "MATH065") u4 Unit(name
"Descret Mathematics", code "MATH045") u5
Unit(name "Tranformers", code "EE038")
u6 Unit(name "Electronic Beams", code
"EE073")
4Object Creation continued
// link Unit objects with Course
objects c1-gtupdate()-gthasUnits.add(u1)
c2-gtupdate()-gthasUnits.add(u1) c1-gtupdate()-gth
asUnits.add(u2) c2-gtupdate()-gthasUnits.a
dd(u2) c3-gtupdate()-gthasUnits.add(u3) c1-gtupd
ate()-gthasUnits.add(u4)
c3-gtupdate()-gthasUnits.add(u4) c4-gtupdate()-gtha
sUnits.add(u5) c4-gtupdate()-gthasUnits.add(u6)
// link with students
u1-gtupdate()-gttakenBy.add(s1)
u1-gtupdate()-gttakenBy.add(s2)
u1-gtupdate()-gttakenBy.add(s7)
u2-gtupdate()-gttakenBy.add(s8)
u2-gtupdate()-gttakenBy.add(s1)
u2-gtupdate()-gttakenBy.add(s2)
u2-gtupdate()-gttakenBy.add(s7)
u3-gtupdate()-gttakenBy.add(s3)
u3-gtupdate()-gttakenBy.add(s4)
u3-gtupdate()-gttakenBy.add(s1)
u3-gtupdate()-gttakenBy.add(s10)
u3-gtupdate()-gttakenBy.add(s11)
u3-gtupdate()-gttakenBy.add(s9)
u4-gtupdate()-gttakenBy.add(s2)
u4-gtupdate()-gttakenBy.add(s4)
u4-gtupdate()-gttakenBy.add(s1)
u4-gtupdate()-gttakenBy.add(s10)
u4-gtupdate()-gttakenBy.add(s7)
u4-gtupdate()-gttakenBy.add(s9)
u5-gtupdate()-gttakenBy.add(s5)
u5-gtupdate()-gttakenBy.add(s6)
u5-gtupdate()-gttakenBy.add(s11)
u5-gtupdate()-gttakenBy.add(s12)
u6-gtupdate()-gttakenBy.add(s5)
u6-gtupdate()-gttakenBy.add(s11)
u6-gtupdate()-gttakenBy.add(s12)
5Querying using OQL
- Simple Query
-
- Named objects entry points
- Path expressions
- Explicit Joins
The query returns names of lecturers such that
the type of the result is bagltstringgt.
select l.name from l in Lecturers
The query returns all Department objects. The
type of the query is setltDepartmentgt.
Departments
select struct(Lnamel.name, Dnamel.worksFor.name)
from l in Lecturers
The query returns names of lecturers and their
departments. Note that this in this query,
l.worksFor.name is path expression. The query
returns names of lecturers and their department
by joining them (but the query does not include
any join condition). The type of the result is
bagltLnamestring, Dnamestringgt. This query has
an implicit join.
select struct(Lnamel.name, Dnamed.name) from l
in Lecturers, d in Departments where l.worksFor
d and d.name "Computing"
Here the user explicitly tells that a join should
be performed on the basis of matching OIDs
(l.worksFor and d denote an OID of a Department
object). The type of the result is
bagltLnamestring, Dnamestringgt.
6OQL Queries continued
- Unnesting Query to unnest a collection
- A Nesting Query to create nested collection
select struct(LNamel.name, SNames.name) from l
in Lecturers, s in l.tutees
The query retrieves names of lecturers and their
tutees in pairs. The query iterates over the
collection Lecturers via the variable l then
iterates over l.tutees (a set of Student objects)
via the variable s, which unnests this
collection then projects out name from l and
name from s. The type of the query result is
bagltLNamestring, SNamestringgt.
select struct(Dnamed.name,
AccStaff (select l.name from l in
d.staff)) from d in Departments
The query returns names of departments and their
employees. The inner query iterates over the
collection staff and projects out lecturer name
and creates a collection, and in the outer query
the result of the inner query is assigned to the
variable AccStaff. The type of the query is
bagltDnamestring, AccStaffbagltstringgtgt.
7OQL Queries continued
select struct(Unameu.name, Tno
count(u.taughtBy), Sno count(u.takenBy)) from
u in Units
The query retrieves names of units and the number
of teaching staff and the students who take these
units. The type of the query result is
bagltUNamestring, Tnolong, Snolonggt.
select distinct struct(Lnamel.name,
Dnamel.worksFor.name, TuteesNo
count(l.tutees) from l in Lecturers where
count(l.tutees) gt max(select count(t.tutees)
from t in Lecturers)
This query retrieves the names of lecturers and
their departments such that the lecturers have
maximum tutees. The type of the query is
setltLNamestring, Dnamestring, TuteesNolonggt.
The query can be written more efficiently in two
steps as maxNo max(select count(t.tutees)
from t in Lecturers) select distinct
struct(Lnamel.name, Dnamel.worksFor.name,
TuteesNo
count(l.tutees) from l in Lecturers where
count(l.tutees) gt maxNo
8Using Lambda-DB
- Open a DOS/command window.
- Connect to cgapp2 using your Unix user account
and password by writing - telnet cgapp2 on DOS prompt.
- From now on every thing you type is
case-sensitive. - Run the script for setting up seminar12 files by
/home/makhtarali/setupOODBdemo - Go to the folder university by
- cd /data/cg096/YourUserName/university
- Replace YourUserName by your unix login name.
- Run make build to create your own workspace if
you have not done so in week 10 or 11. - Run make to compile the schema in university.odl
and the program populate.oql for constructing
objects. - Run ./populate to populate the database.
- Run make query to compile the queries in
query.oql file. - Run ./query gtquery.out to execute the queries
and write its output to query.out file. - Run more query.out to view the results.
- Just before you logout, run the following
commands - cd /
- rm rf /data/cg096/YourUserName