Download presentation
Presentation is loading. Please wait.
Published bySky Lovelace Modified over 9 years ago
1
Advanced Database: Seminar # 12: Object-Oriented Databases1 Object Oriented Databases Seminar 12 Exploring ODMG - OQL Using Lambda-DB
2
Advanced Database: Seminar # 12: Object-Oriented Databases2 Example 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 set tutees inverse Student::tutor; relationship Department worksFor inverse Department::staff; relationship set teaches inverse Unit::taughtBy; }; class Student : Person (extent Students) { attribute string major; relationship Lecturer tutor inverse Lecturer::tutees; relationship Course enrolledOninverse Course::hasStudents; relationship set takesinverse Unit::takenBy;}; class Department (extent Departments) { attribute string name; relationship set staffinverse Lecturer::worksFor; relationship set offers inverse Course::offeredBy; }; class Course (extent Courses) { attribute string name; relationship Department offeredByinverse Department::offers; relationship set hasStudentsinverse Student::enrolledOn; relationship set hasUnitsinverse Unit::partOf; }; class Unit (extent Units) { attribute string name; attribute string code; relationship set takenBy inverse Student::takes; relationship set taughtByinverse Lecturer::teaches; relationship set partOfinverse Course::hasUnits; }; };
3
Advanced Database: Seminar # 12: Object-Oriented Databases3 Object Creation – the Database // Department instances %d1 := Department(name:"Computing"); %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:"Julia Robert", address: "Durham", worksFor: d2); %i3 := Lecturer(name:"David Meyer", address: "Sunderland", worksFor:d1); %i4 := Lecturer(name:"Alex Shearer", address: "Newcastle", worksFor:d3); %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", worksFor:d1); %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");
4
Advanced Database: Seminar # 12: Object-Oriented Databases4 Object Creation – continued… // link Unit objects with Course objects c1->update()->hasUnits.add(u1); c2->update()->hasUnits.add(u1); c1->update()->hasUnits.add(u2); c2->update()->hasUnits.add(u2); c3->update()->hasUnits.add(u3); c1->update()->hasUnits.add(u4); c3->update()->hasUnits.add(u4); c4->update()->hasUnits.add(u5); c4->update()->hasUnits.add(u6); // link with students u1->update()->takenBy.add(s1); u1->update()->takenBy.add(s2); u1->update()->takenBy.add(s7); u2->update()->takenBy.add(s8); u2->update()->takenBy.add(s1); u2->update()->takenBy.add(s2); u2->update()->takenBy.add(s7); u3->update()->takenBy.add(s3); u3->update()->takenBy.add(s4); u3->update()->takenBy.add(s1); u3->update()->takenBy.add(s10); u3->update()->takenBy.add(s11); u3->update()->takenBy.add(s9); u4->update()->takenBy.add(s2); u4->update()->takenBy.add(s4); u4->update()->takenBy.add(s1); u4->update()->takenBy.add(s10); u4->update()->takenBy.add(s7); u4->update()->takenBy.add(s9); u5->update()->takenBy.add(s5); u5->update()->takenBy.add(s6); u5->update()->takenBy.add(s11); u5->update()->takenBy.add(s12); u6->update()->takenBy.add(s5); u6->update()->takenBy.add(s11); u6->update()->takenBy.add(s12);
5
Advanced Database: Seminar # 12: Object-Oriented Databases5 Querying using OQL Simple Query Named objects – entry points Path expressions Explicit Joins select l.name from l in Lecturers The query returns names of lecturers such that the type of the result is bag. Departments The query returns all Department objects. The type of the query is set. select struct(Lname:l.name, Dname:l.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 bag. This query has an implicit join. select struct(Lname:l.name, Dname:d.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 bag.
6
Advanced Database: Seminar # 12: Object-Oriented Databases6 OQL Queries – continued … Unnesting Query – to unnest a collection A Nesting Query – to create nested collection select struct(Dname:d.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 bag >. 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 bag. select struct(LName:l.name, SName:s.name) from l in Lecturers, s in l.tutees
7
Advanced Database: Seminar # 12: Object-Oriented Databases7 OQL Queries – continued … Aggregation select distinct struct(Lname:l.name, Dname:l.worksFor.name, TuteesNo: count(l.tutees) from l in Lecturers where count(l.tutees) >= 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 set. The query can be written more efficiently in two steps as: maxNo := max(select count(t.tutees) from t in Lecturers) select distinct struct(Lname:l.name, Dname:l.worksFor.name, TuteesNo: count(l.tutees) from l in Lecturers where count(l.tutees) >= maxNo 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 bag. select struct(Uname:u.name, Tno: count(u.taughtBy), Sno: count(u.takenBy)) from u in Units
8
Advanced Database: Seminar # 12: Object-Oriented Databases8 Using 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/setupOQLdemo Go to the folder university by cd /data/cm036/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 >query.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/cm036/YourUserName
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.