CSE9002/COT4230 - Pravin Shetty > Resume

CSE9002
Week 2
The Relational Data Model
CSE9002
1
Joins of Tables
The joining of attributes depends on certain types of
relationships;
Consider two attributes C1 and C2 which are join
attributes
There are 4 types of relationships possible
(a) the values of C1 and C2 are equal
(b) the values of C1 are a subset of those of C2 (or
vice versa)
(c) the values of C1 and C2 are conjoint - they have
some values in common
(d) the values of C1 and C2 are disjoint - they have
2
no values in common
Joins of Tables
In set theory, these take the forms
(a) C1 = C2
(b) C1  C2 or C2  C1
(c) C1 - C2  0 or C2 - C1  0
(d) C1 - C2 = C1 and C2 - C1 = C2
3
Joins of Tables
There are a number of possible ‘join’ types
allowable in the relational model
They are:
1. Thetajoin
3. Natural join
5. Outer join
2.Equijoin
4. Inner join
6. Left Outer join
7. Right Outer join
8. Full Outer join
4
Joins of Tables
1. Thetajoin This is the case when the ‘condition of
join’, or comparator, is of the form < = > ! or any
combination (>=) as in the case
select * from parts, rates
where parts.partno (< > =) rates.partno
2. If the comparator is =, the join is a equijoin
3. If the select clause contains the name of only
one of the join attributes, this results in a natural
join
(if the clause had been stated as select
parts.partno, rates.partno, then both these
attributes would have been in the result table)
5
Joins of Tables
4. Inner Join
This is based on there being 2 tables - e.g.
members and penalties as in a video club
members, and penalty tables.
This results in only that data which is supported
by the comparator being presented in the result
table.
The only results will be those rows which satisfy
the join condition of (in this case) = (equality).
Any other non-matched rows will not be
displayed.
6
Joins of Tables
5. Outer join.
There may be other data which the user
wanted to ‘see’ but it would have been
excluded.
A case could be where a librarian wants to
see who has incurred a fine for late or non
return of videos, but also wants to see that
the other customers
(a) are listed
(b) are shown as 0 penalty (even though
this is not a value as the row would not
exist in the penalties table)
This generally extends the query to a union
and a substitute attribute.
7
Joins of Tables
6. Left Outer join
This is a type of outer equijoin, and takes
its name from the use of the ‘left’ table as
the dominant table.
The result table will only include values
from the secondary table where the
equijoin is valid.
The result is the ‘intersection’ of the
populations of the 2 tables involved.
The ‘left’ table is the first named table in
the from clause
8
Joins of Tables
7. Right outer join
The dominant attributes are those from the
table named as the rightmost position in
the from clause.
8. Full Outer Join
All rows from both tables named in the
from clause are processed. This is the same
as a union of the left and right outer joins
of the tables involved.
9
Joins of Tables
Some pointers:
1. Some versions of SQL allow for the
nomination of the join type (see example)
2. The correct use of the various joins
depends on the knowledge of table content
and of course the clear understanding and
statement of the required result table
content
3. The complex joins invariably require subqueries which may require the exist or not
exist or the in or not in operators, and the
union function
10
Remote Presentation Client/Server
Desktop
Presentation Services
Presentation Logic
Host
Data Services
File Services
Business Logic
Data Logic
11
3 Tiered
3 Tiered
Client Client/Server
Server
Database
Desktop
DataBase Server
Presentation Services
Data Services
Presentation Logic
File Services
Application Server
Business Logic
Data Logic
12
Mirror Tables
Base Tables (disk located) and Virtual Tables
(memory)
Consider a table ‘employee’ which contains
staff detail
supervisor detail
manager details
Select a.name,b.name,c.name,d.name,e.name
from employee a, employee b, employee c,
employee d, employee e;
would produce a results table of all names in 5
columns of output
13
Joining a Table to Itself
Typical Query: For each employee, list the employee number, name
Manager and Manager’s name
Select X.EMPNO, X.NAME, X.MGR, Y.NAME
from EMP X, EMP Y
where X.MGR = Y.EMPNO
Result:
EMPNO
10
20
30
40
NAME
SMITH
JONES
BLACK
BROWN
MGR
40
40
40
50
NAME
BROWN
BROWN
BROWN
WHITE
The Primary Key and the Foreign are both in the same table
Two virtual tables are created for joining (‘alias’ feature)
14
DIVISION
• Divides a BINARY relation by a UNARY relation and
produces a UNARY relation as a result.
emp-skill
empno
E1
E2
E3
E2
E5
E6
skillcode
S1
S2
S3
S4
S5
S6
skill-reqd
result
skillcode
S2
S4
empno
E2
Divide emp-skill by skill-reqd
to give result
Special note: JOIN, INTERSECTION and DIVISION can be defined
in terms of the other 5 operators (which are known as
the ‘primitive’ operators).
15
A DIVISION example
In the Air Transport Industry, pilot’s records
contain details of the aircraft they are qualified
to fly. And there are also records of the
number and types of aircraft in the hangars
and which Company owns what.
In this case, the table of pilot’s names and the
planes they can fly is the dividend
The details of the planes in the hangars is the
quotient
16
A DIVISION example
The query is to obtain the names of
the pilots who can fly every type of
plane in the hangars
17
Suggested Solution
create table pilotskill (pilot vchar (150) not
null, plane vchar(15) not null);
create table hangar (plane vchar(15));
select pilot from pilotskill ps1, hangar h1
where ps1.plane = h1.plane
group by ps1.pilot
having count(ps1.plane = select count(*)
from hangar);
18
Division
[notice the absence of any ‘division’ operator
- this is effectively performed by the
execution plan]
19
Division Examples
A B
1 J
1 K
1 L
2 J
2 K
3 K
3 L
3 J
C
J
K
L
Result
1
3
20
Division Examples
Name
Jones
Jensen
Jensen
Jensen
Smith
Smith
Rogers
Rogers
Degree
B Sc
B Sc
M Sc
PhD
B Sc
M Sc
B Sc
PhD
R1
Jensen
D1
M Sc
B Sc
PhD
D2
B Sc
M Sc
R2
Jensen
Smith
D3
B Sc
R3
Jones
Jensen
Smith
Rogers
21