View and Outer Join

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

View and Outer Join

Three Levels of Abstractions

Relational
VIEW here! schema studied
in class

Storage details
Hiding Information from Some Users
 Some information should not be released
to all users
 Students(sid, sname, gpa, age)
 Customized and authorized data access
for different users or groups of users
 Create a new table without gpa?
 Extra overhead on coordination of updates: any
updates to one table have to be applied to the other
 Extra overhead on space
Some Frequently Used Information
 Frequently deriving course rosters from
 Students(sid, sname, gpa, age)
 CourseEnrolled(sid, cid, day)
 Writingqueries to join the two tables
many times are tedious
 Can we have a virtual table?
 Roster(sname, cid, day), by joining the two tables
Students and CourseEnrolled
View
 Create a virtual table from Students, only
include the sid, sname, age information
 Students(sid, sname, gpa, age)

CREATE VIEW StudentsLimited AS


SELECT sid, name, age
FROM Students

 Assign a name to a query


 A view does not exist physically, the tuples in a
view are not stored explicitly in a specific table
Renaming Attributes
 Createa virtual table from Students, only
include the sid, sname, age information;
rename sid to stuId and sname to stuName
 Students(sid, sname, gpa, age)

CREATE VIEW StudentsLimited(stuId, stuName, age) AS


SELECT sid, sname, age
FROM Students
Creating View from Multiple Tables
 Create a virtual table Roster from Students and
CourseEnrolled; include sname, cid, and day only
 Students (sid: integer, sname: string, gpa: real, age: integer)
 CourseEnrolled (sid: integer, cid: string, day: date)

CREATE VIEW Roster(stuName, cid, day) AS


SELECT sname, cid, day
FROM Student S, CourseEnrolled E
WHERE S.sid = E.sid

 A user who can access Roster, but not Students or


CourseEnrolled knows the names of students who have
enrollments but cannot find out the gpa and age of a given
student.
Querying a View
 Use the view Roster
 Find all courses enrolled by ‘Bob’
SELECT cid
FROM Roster
WHERE stuName = ‘Bob’

 Find the number of students enrolled in each course

SELECT cid, count(sname)


FROM Roster
GROUP BY cid
Drop View
 DROP VIEW viewname

 Drop View Roster;


 Drop View StudentsLimited
Some Extra Notes on Joins

1
Inner Join
 We have learned the following join:
SELECT S.sid, sname, bid
FROM Sailors S,Reserves R
WHERE S.sid = R.sid

 Returnsonly those sailors who have


reserved boats
 This is called “inner join”, can also be written as
SELECT S.sid, sname, bid
FROM Sailors S INNER JOIN Reserves R ON S.sid = R.sid
SELECT sid, sname, bid
FROM Sailors NATURAL JOIN Reserves
1
SELECT S.sid, sname, bid
FROM Sailors S,Reserves R
WHERE S.sid = R.sid

sid sname rating age sid bid day


22 Dustin 7 45.0 22 101 10/10/96
31 Lubber 8 55.5 95 103 11/12/96
95 Bob 3 63.5

S.sid sname bid


22 Dustin 101
95 Bob 103

1
Left Outer Join
 Left Outer Join returns all matched rows
PLUS
all unmatched rows from the table on the left of the join clause
 Use NULL in fields of non-matching tuples

SELECT S.sid, sname, bid


FROM Sailors S LEFT OUTER JOIN Reserves R
ON S.sid = R.sid

 Returns all sailors & information on whether they have


reserved boats

1
SELECT S.sid, sname, bid
FROM Sailors S LEFT OUTER JOIN Reserves R
ON S.sid = R.sid

sid sname rating age sid bid day


22 Dustin 7 45.0 22 101 10/10/96
95 103 11/12/96
31 Lubber 8 55.5
95 Bob 3 63.5

S.sid sname bid


22 Dustin 101
95 Bob 103
31 Lubber
1
Right Outer Join
 Right Outer Join returns all matched rows,
PLUS
all unmatched rows from the table on the right of the
join clause
SELECT sid, B.bid, bname
FROM Reserves R RIGHT OUTER JOIN Boats B
ON R.bid = B.bid

 Returns all boats & information on which ones are


reserved.

1
SELECT sid, B.bid, bname
FROM Reserves R RIGHT OUTER JOIN Boats B
ON R.bid = B.bid

sid bid day bid bname color


22 101 10/10/96 101 Interlake blue
95 103 11/12/96 102 Interlake red
104 Marine red

sid B.bid bname


22 101 Interlake
102 Interlake
104 Marine

1
Full Outer Join
 Full Outer Join returns all (matched or
unmatched) rows from the tables on both sides
of the join clause
SELECT sid, B.bid, bname
FROM Reserves R FULL OUTER JOIN Boats B
ON R.bid = B.bid

 Returns all boats & all information on


reservations

1
SELECT sid, B.bid, bname
FROM Reserves R FULL OUTER JOIN Boats B
ON R.bid = B.bid

sid bid day bid bname color


22 101 10/10/96 101 Interlake blue
95 103 11/12/96 102 Interlake red
104 Marine red

sid B.bid bname


22 101 Interlake
95
102 Interlake
104 Marine
1
A B C D
Another example 1 2 3 4
R LEFT OUTER 1 2 3 8
A B C
JOIN S 1 3 7 5
1 2 3
2 2 5 null
2 2 5
1 3 7 A B C D
R R RIGHT 1 2 3 4
OUTER JOIN S 1 2 3 8
B C D
1 3 7 5
2 3 4
null 2 7 1
2 7 1
3 7 5 R FULL OUTER
2 3 8 JOIN S ?
S 1
General Format for Joins
SELECT (column_list)
FROM table_name
[INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name
ON qualification_list
WHERE …

 Explicit join semantics needed unless it is an


INNER join (INNER is default)

2
A Summary of Joins

You might also like