Upload
samantha-willis
View
226
Download
6
Embed Size (px)
Citation preview
Relational operators
1
Lecture 7
Relational Operators
Relational operators
2
Relational data model
relational data objects
relational operators
relational data integrity
Relational operators
3
Relational operators
preamble why do we need relational operators? what kind of operations do we need to perform? how many operators would we like to have? what do you think a “powerful set of operators” means?
procedural / declarative relational algebra / relational calculus
Relational operators
4
Relational algebra operators
basic primitive 4 “set specific” operations 4 “relation specific” operations relational closure
Relational operators
5
“Set specific” operators
specialised for relations type compatibility attribute name inheritance candidate key inheritance
they are union intersection difference Cartesian product exemplify them with a couple of relations of your choice
Relational operators
6
“Relation specific” operators
restriction <relation name> WHERE <condition>
projection <relation name> [ <attr_1>, <attr_2>, …, <attr_n> ]
join <relation name> JOIN <relation name>
difference <relation name> DIVBY <relation name>
Relational operators
7
Restriction
conditional expressions on atomic values primitive data types
• supported in SQL
• non-conditional scalar operators cannot be used in conditional expressions; however, they can be introduced via EXTEND
domains • no support provided in SQL
• PostgreSQL - extendable set of operators on atomic value
atomic / non-atomic conditions nested conditions logical expressions
Relational operators
8
Join
natural join theta-join
can be expressed via a Cartesian product and a restriction; how?
properties commutative associative
Relational operators
9
Join
Supplier SupplyCity TelephoneE-A Inc Coventry 01203 456678E & E Ltd London 0181 3994567H200 London 0171 1233456S-TV Tokyo 0081 3 11122233
PruductID ProductName Cost InStock SupplierPID23 Washing machine 289 2 E-A IncXX24A Dish dryer 399 0 H20000012 Power supply extension 14.99 15 E-A IncMM25y Television set 395 0 S-TVMM45x Television set 555 0 S-TV
Relational operators
10
Join
PruductID ProductName Cost InStock Supplier SupplyCity TelephonePID23 Washing machine 289 2 E-A Inc Coventry 01203 456678XX24A Dish dryer 399 0 H200 London 0171 123345600012 Power supply extension 14.99 15 E-A Inc Coventry 01203 456678MM25y Television set 395 0 S-TV Tokyo 0081 3 11122233MM45x Television set 555 0 S-TV Tokyo 0081 3 11122233
Relational operators
11
Division
Supplier PartS1 P1S1 P2S1 P3S1 P4S2 P2S2 P3S2 P5S3 P3S3 P5S4 P3
PartP1P2P3
Contracts Set1
PartP3P5
PartP5
Set3Set2
Relational operators
12
Division
SupplierS2S3S4
Contracts DIVBY Set1
SupplierS2S3
SupplierS1
Contracts DIVBY Set2 Contracts DIVBY Set3
Relational operators
13
Relational algebra operators
closure nested expressions
primitive set restriction, projection, Cartesian product, union, difference
Relational operators
14
Examples
SName Age Address
Students
TName Position Salary
Tutors
SName MName TName MName
MName Type Level Credits Syllabus
Registrations Teaching
Modules
Relational operators
15
Examples
get the name of the tutors who teach at least one module get the name position and salary of all the tutors who teach
at least one module get the name of the tutors who do not teach any module get the name and address of all the students who take level
one courses get the name of students who take all optional modules ... think of other queries and devise relational algebra
expressions for them
Relational operators
16
Extensions
EXTEND you can think as allowing scalar computations in WHERE
clauses
SUMMARISE projections combined with the performance of summary
calculations
Relational operators
17
Implementation
DML in SQL relational operators can be implemented via SELECT
statements SQL provides a richer set of operations than just the set of
basic relational algebra operations• e.g. sub-queries
Relational operators
18
The optimiser
optimiser for a specific implementation, the module that ‘decides’ what
is the best strategy of evaluating an expression (a query)(note that evaluation record level operations)
example• get the addresses of all students who take the “AI” module
• 3000 students; 4 modules per student, on average; some 100 students for the AI module;
• ((Students JOIN Registration) WHERE Mname = “AI”) [Sname, Address]
• (Students JOIN (Registration WHERE Mname = “AI”)) [Sname, Address]
Relational operators
19
Summary
relational algebra operators (set and relation specific) relational closure extensions implementation - SQL the optimiser
relational calculus