Download OQL Examples

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Relational algebra wikipedia , lookup

Relational model wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Transcript
CSE 880
Advanced Database
Systems
OQL: Object Query
Language
S. Pramanik
1
Introduction
1. Proposed by ODMG 2.0 (Object Data Management standards
Group).
2. Similarities to SQL-3 standards (for object relational databases).
3. Overcome Two Major Limitations of SQL:
(a) Attributes cannot be set valued (requirement for 1NF relations).
(b) Allow only simple types and no nesting or recursion of
types.
2
OQL - Features
1. MAIN IDEA:
• Set valued attributes are handled by creating a relation
for each set of multiple values as a relation in the FROM
clause, or by using set valued operator ”IS IN” in the
WHERE Clause.
• This is achived by creating a correlation variable for the set
valued attribute in the FROM clause and using the correlation name in the select clause by having SELECT FROM
WHERE in the SELECT clause. (correlation variable acts
as a table name for each set).
• New complex objects are created in the SELECT clause
by allowing ”Select From Where” in the SELECT clause.
• Complex objects are also created by using constructors
Table-a
Name Address
Tel
n1
a1
{t1, t2}
n2
a2
{t3,t4,t5}
n3
a2
{t3, t6}
SELECT Name, Telephone: (SELECT * FROM
FROM Table-a TA
WHERE Address= a2
Result:
Name
n2
n3
TA.tel)
Telephone
{t3,t4,t5}
{t3,t6}
2. Aggregation hierarchies are accessed by creating query path
expressions (sequence of (period followed by attribute name)).
3. Methods and relationships are used the same way the attributes
are used.
4. Example queries given here for Inheritance is using O2 syntax.
5. Inheritance hierarchies are handled by indicating recursive accesses in the FROM Clause.
3
An Example Database Schema
Assume the following database schema diagram for a Projects Database:
defined in ODL (inverse relationships omitted)
Document
Projects
acronym_document: STRING
name: STRING
classification: STRING
project_name: STRING
objectives: STRING
documents*
work_plan*
sub_project
Technical Report
balance(): NUMBER
participating():*
topics: STRING
start_validity: DATE
end_validity: DATE
amendment_to
Task
Article
date_start: DATE
date_end: DATE
description_task: TEXT
participating
man_year: NUMBER
leader
precedes*
author*
publicxation_type:STRING
publication_place: STRING
date: DATE
Research
Group
group_name:STRING
member*
leader
name: STRING
specialization: STRING
salary:NUMBER
production_bonus: NUMBER
average_salary:NUMBER
monthly_salary(): NUMBER
Figure 1:
Schema for the above database defined in ODL (inverse relationships omitted) is given below:
class Project
(
extent Projects)
{
attribute string project_name;
attribute string objective;
relationship set<Document> document;
relationship set<Task> work_plan;
relationship <Project> sub_project;
number balance();
set<Group> participating()
};
class Task
(
extent Tasks)
4
{
attribute string date_start;
attribute string date_end;
attribute string description_task;
relationship set<Group> participating;
attribute number man_year;
relationship <Researcher> leader;
relationship set<Task> precedes;
};
class Group
(
extent Groups)
{
attribute string group_name;
relationship set<Researcher> member;
relationship <Researcher> leader;
};
class Researcher
(
extent Researchers)
{
attribute string name;
attribute string specialization ;
attribute number salary;
attribute number production_bonus;
attribute average _salary:number;
relationship number monthly_salary();
};
class Document
(
extent Documents)
{
attribute string acronym_document;
attribute string name;
attribute string classification;
};
class TechnicalReport extends Document
(
extent TechnicalReports)
{
attribute string topics;
attribute DATE start_validity;
attribute DATE end_validity;
relationship <TechnicalReport> amendment_to;
};
class Article extends Document
(
extent Articles)
{
attribute string publication_type;
attribute string publication_place;
attribute string date;
relationship set<Researcher> author;
};
5
OQL Examples for the above database
1. It is assumed that the relationships are implemented by attributes.
2. (a) Basic OQL structure is similar to that of SQL:
Select
From
Where
(b) SQL has to be extended to implement object database concepts such as: object ID, complex objects, methods, inheritance, relationships, polymorphism.
(c) Name of objects or the collection name (defined by extent)
used as entry point in the database.
(d) Use of path expressions for navigation through database
(e) Iterator Variable
(f) Result of OQL is a collection of objects of type
Set when select UNIQUE is used
else result is a Bag.
6
Example Queries
Projects
Document
acronym_document: STRING
name: STRING
classification: STRING
project_name: STRING
objectives: STRING
documents*
work_plan*
sub_project
Technical Report
balance(): NUMBER
participating():*
topics: STRING
start_validity: DATE
end_validity: DATE
amendment_to
Task
Article
date_start: DATE
date_end: DATE
description_task: TEXT
participating
man_year: NUMBER
leader
precedes*
author*
publicxation_type:STRING
publication_place: STRING
date: DATE
Research
Group
group_name:STRING
member*
leader
name: STRING
specialization: STRING
salary:NUMBER
production_bonus: NUMBER
average_salary:NUMBER
monthly_salary(): NUMBER
Figure 2:
Query 1:
Select project names for those projects with objective
”Database Design”
Select Struct (ProjectName: P.project_name)
From Projects P
Where P.objective="Database Design"
Struct is a constructor creating structure type:
<attrName1: value1, attrName2: value2, . . .>
Struct is similar to Tuple in O2
Set type is created by Select From Where.
3. Creating more complex structures in the Select Clause:
Select project names, task descriptions, start dates,
end dates and names of task leaders for those projects
with objective: ”Database Design”.
Select Struct(ProjectName: P.project_name,
Tasks: Select Struct(TaskDescription: T.description_task,
StartDate: T.date_start,
EndDate: T.date_end,
TaskLeader: T.leader.name)
From
T)
7
From Projects P, P.work_plan T
/* P.work_plan T is allowed in
ODMG standard*/
Where P.objectives= "Database Design"
We could have also done where P.work_plan defined in SELECT clause:
Select Struct (ProjectName: P.project_name,
Tasks: Select Struct (TaskDescription: T.description_task,
StartDate: T.date_start,
EndDate: T.date_end,
TaskLeader: T.leader.name)
From
P.work_plan T)
From Projects P
Where P.objectives= "Database Design"
8
Document
Projects
acronym_document: STRING
name: STRING
classification: STRING
project_name: STRING
objectives: STRING
documents*
work_plan*
sub_project
Technical Report
balance(): NUMBER
participating():*
topics: STRING
start_validity: DATE
end_validity: DATE
amendment_to
Task
Article
date_start: DATE
date_end: DATE
description_task: TEXT
participating
man_year: NUMBER
leader
precedes*
author*
publicxation_type:STRING
publication_place: STRING
date: DATE
Research
Group
group_name:STRING
member*
leader
name: STRING
specialization: STRING
salary:NUMBER
production_bonus: NUMBER
average_salary:NUMBER
monthly_salary(): NUMBER
Figure 3:
4. Navigation through aggregation hierarchy:
(a) Single valued attributes:
Get descriptions of those tasks with participating
Group leader name is John R.
Select T.description_task
there is no structure here
From Tasks T
Where T.participating.leader.name="John R"
NOTE: In traditional SQL it required joins because there
is no notion of object id or pointers.
(b) Set valued attributes:
i. Select project names of those projects whose
work-plan has a task with leader ”John R”
SELECT P.project_name
FROM Projects P
WHERE "John R" IS IN (SELECT T.leader.name
FROM P.work_plan T)
ii. Select project names and Task descriptions of
those projects where the task leader is ”John
R”
Select Struct (Project_Name: P.project_name, TaskDescriptions:
Select Struct (T.description_task)
From P.work_plan T)
9
From
WHERE
Projects P
"John R" IS IN (SELECT UNIQUE T.leader.name
FROM P.work_plan T)
iii. For each project with project objective ”Database
Design,” get project names, task descriptions,
group names and specialization of the members
in the groups.
Select Struct (ProjectName: P.project_name,
Tasks: Select Struct (TaskDescription: T.description_task,
GroupName: T.participating.group_name,
Members: Select Struct (MemberName: m.name,
Specialization: m.specialization)
From T.participating.member m)
From P.work_plan T)
From
Projects P
Where P.objective= "Database Design"
iv. Get project names for those projects with at
least one document ”Database” and at least one
task with task leader ”John R.”
Select Struct (projectName: P.project_name)
From
Projects P
Where Exists(Select *
From P.work_plan T
Where T.leader.name="John R")
AND
Exists(Select *
From P.document D
Where D.name="database")
5. Use of Methods and Relationships is similar to that of attributes.
10
Document
Projects
acronym_document: STRING
name: STRING
classification: STRING
project_name: STRING
objectives: STRING
documents*
work_plan*
sub_project
Technical Report
balance(): NUMBER
participating():*
topics: STRING
start_validity: DATE
end_validity: DATE
amendment_to
Task
Article
date_start: DATE
date_end: DATE
description_task: TEXT
participating
man_year: NUMBER
leader
precedes*
author*
publicxation_type:STRING
publication_place: STRING
date: DATE
Research
Group
group_name:STRING
member*
leader
name: STRING
specialization: STRING
salary:NUMBER
production_bonus: NUMBER
average_salary:NUMBER
monthly_salary(): NUMBER
Figure 4:
6. Use of Object IDs
(a) Get task descriptions of those tasks whose participating group leader is same as it’s leader.
Select T.description_task
output has no
From Tasks T
structure with attribute name.etc.
Where T.participating.leader==T.leader
(b) Get project names and task descriptions for those
projects whose objective is marketing and project
has a task whose participating group leader is the
same as the leader of the task.
Select Struct (ProjectName: P.project_name,
Tasks: Select w.description_task
From P.work_plan w)
From Projects P
Where P.objective= "marketing" and
Exists(Select *
From P.work_plan T
Where T.participating.leader
== T.leader)
11
Document
Projects
acronym_document: STRING
name: STRING
classification: STRING
project_name: STRING
objectives: STRING
documents*
work_plan*
sub_project
Technical Report
balance(): NUMBER
participating():*
topics: STRING
start_validity: DATE
end_validity: DATE
amendment_to
Task
Article
date_start: DATE
date_end: DATE
description_task: TEXT
participating
man_year: NUMBER
leader
precedes*
author*
publicxation_type:STRING
publication_place: STRING
date: DATE
Research
Group
group_name:STRING
member*
leader
name: STRING
specialization: STRING
salary:NUMBER
production_bonus: NUMBER
average_salary:NUMBER
monthly_salary(): NUMBER
Figure 5:
(c) Get task descriptions of those tasks whose leaders
are authors of some articles.
Select Struct (TaskDescription: T.description_task)
From Task T
Where Exists( Select *
From Article.author Au
Where Exists
(Select *
From Au
Where Au==T.leader)
Note: Au in the from clause is a set of object id’s and Au
in the Where clause is an objcet id in the set.
7. Two Operands in From Clause and Join of objects following
two different aggregation hierarchies.
Documents whose title (name) is the same as the name
of some project.
Select D.acronym_document, D.name, D.classification
From Documents D, Projects P
Where D.name=P.project_name
12
Document
Projects
acronym_document: STRING
name: STRING
classification: STRING
project_name: STRING
objectives: STRING
documents*
work_plan*
sub_project
Technical Report
balance(): NUMBER
participating():*
topics: STRING
start_validity: DATE
end_validity: DATE
amendment_to
Task
Article
date_start: DATE
date_end: DATE
description_task: TEXT
participating
man_year: NUMBER
leader
precedes*
author*
publicxation_type:STRING
publication_place: STRING
date: DATE
Research
Group
group_name:STRING
member*
leader
name: STRING
specialization: STRING
salary:NUMBER
production_bonus: NUMBER
average_salary:NUMBER
monthly_salary(): NUMBER
Figure 6:
8. Inheritance Hierarchy (O2 syntax):
(a) Document names of those documents where classification=”c2”
Select Struct (DocumentName: D.name)
From Documents* D
Where D.classification="C2"
* after Documents in the From Clause indicate that the
query will follow the inheritance hierarchy from Document
class by including Technical reports and Articles.
(b) Names of all documents except technical reports
whose classification is ”C2”.
Select
From
Where
Struct (DocumentName: D.name)
(Documents* Difference TechnicalReports) D
D.classification="C2"
13