20.1 KB · commit 40bb182
Database System Concepts, 7th Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Figure 4.1 — Join Types and Join Conditions (Source: Slide 4)
| Join types |
|---|
| inner join |
| left outer join |
| right outer join |
| full outer join |
| Join conditions |
|---|
| natural |
| on <predicate> |
| using (A1,A2,…,An) |
Syntax:
SELECT * FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Query:
SELECT Student_Name, Department_Name
FROM Student
LEFT JOIN Department
ON Student.Dept_ID = Department.Dept_ID;
| Student_Name | Department_Name |
|---|---|
| Alice | CSE |
| Bob | EEE |
| Carol | CSE |
| David | BBA |
| Emma | NULL |
RIGHT JOIN is used to retrieve all rows from the right table and the matching rows from the left table. It helps in:
Syntax:
SELECT * FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Query:
SELECT Student_Name, Department_Name
FROM Student RIGHT JOIN Department
ON Student.Dept_ID = Department.Dept_ID;
| Student_Name | Department_Name |
|---|---|
| Alice | CSE |
| Carol | CSE |
| Bob | EEE |
| David | BBA |
| NULL | Civil |
FULL JOIN is used to combine the results of both LEFT JOIN and RIGHT JOIN. It helps in:
Syntax:
SELECT * FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Query:
SELECT Student_Name, Department_Name
FROM Student
FULL OUTER JOIN Department
ON Student.Dept_ID = Department.Dept_ID;
| Student_Name | Department_Name |
|---|---|
| Alice | CSE |
| Bob | EEE |
| Carol | CSE |
| David | BBA |
| Emma | NULL |
| NULL | Civil |
CROSS JOIN is used to combine the results of both table with each other.
Syntax:
SELECT * FROM table1
CROSS JOIN table2
Query:
SELECT Student_Name, Department_Name
FROM Student
CROSS JOIN Department;
| Student_Name | Department_Name |
|---|---|
| Alice | CSE |
| Alice | EEE |
| Alice | BBA |
| Alice | Civil |
| Bob | CSE |
| Bob | EEE |
| ... | ... |
select name, course_id
from students, takes
where student.ID = takes.ID;
select name, course_id
from student natural join takes;
select A1, A2, … An
from r1 natural join r2 natural join .. natural join rn
where P ;
Figure 4.2 — The Student Relation (Source: Slide 16)
| ID | name | dept_name | tot_cred |
|---|---|---|---|
| 00128 | Zhang | Comp. Sci. | 102 |
| 12345 | Shankar | Comp. Sci. | 32 |
| 19991 | Brandt | History | 80 |
| 23121 | Chavez | Finance | 110 |
| 44553 | Peltier | Physics | 56 |
| 45678 | Levy | Physics | 46 |
| 54321 | Williams | Comp. Sci. | 54 |
| 55739 | Sanchez | Music | 38 |
| 70557 | Snow | Physics | 0 |
| 76543 | Brown | Comp. Sci. | 58 |
| 76653 | Aoi | Elec. Eng. | 60 |
| 98765 | Bourikas | Elec. Eng. | 98 |
| 98988 | Tanaka | Biology | 120 |
Figure 4.3 — The Takes Relation (Source: Slide 17)
| ID | course_id | sec_id | semester | year | grade |
|---|---|---|---|---|---|
| 00128 | CS-101 | 1 | Fall | 2017 | A |
| 00128 | CS-347 | 1 | Fall | 2017 | A- |
| 12345 | CS-101 | 1 | Fall | 2017 | C |
| 12345 | CS-190 | 2 | Spring | 2017 | A |
| 12345 | CS-315 | 1 | Spring | 2018 | A |
| 12345 | CS-347 | 1 | Fall | 2017 | A |
| 19991 | HIS-351 | 1 | Spring | 2018 | B |
| 23121 | FIN-201 | 1 | Spring | 2018 | C+ |
| 44553 | PHY-101 | 1 | Fall | 2017 | B- |
| 45678 | CS-101 | 1 | Fall | 2017 | F |
| 45678 | CS-101 | 1 | Spring | 2018 | B+ |
| 45678 | CS-319 | 1 | Spring | 2018 | B |
| 54321 | CS-101 | 1 | Fall | 2017 | A- |
| 54321 | CS-190 | 2 | Spring | 2017 | B+ |
| 55739 | MU-199 | 1 | Spring | 2018 | A- |
| 76543 | CS-101 | 1 | Fall | 2017 | A |
| 76543 | CS-319 | 2 | Spring | 2018 | A |
| 76653 | EE-181 | 1 | Spring | 2017 | C |
| 98765 | CS-101 | 1 | Fall | 2017 | C- |
| 98765 | CS-315 | 1 | Spring | 2018 | B |
| 98988 | BIO-101 | 1 | Summer | 2017 | A |
| 98988 | BIO-301 | 1 | Summer | 2018 | null |
Figure 4.4 — student natural join takes (Source: Slide 18)
| ID | name | dept_name | tot_cred | course_id | sec_id | semester | year | grade |
|---|---|---|---|---|---|---|---|---|
| 00128 | Zhang | Comp. Sci. | 102 | CS-101 | 1 | Fall | 2017 | A |
| 00128 | Zhang | Comp. Sci. | 102 | CS-347 | 1 | Fall | 2017 | A- |
| 12345 | Shankar | Comp. Sci. | 32 | CS-101 | 1 | Fall | 2017 | C |
| 12345 | Shankar | Comp. Sci. | 32 | CS-190 | 2 | Spring | 2017 | A |
| 12345 | Shankar | Comp. Sci. | 32 | CS-315 | 1 | Spring | 2018 | A |
| 12345 | Shankar | Comp. Sci. | 32 | CS-347 | 1 | Fall | 2017 | A |
| 19991 | Brandt | History | 80 | HIS-351 | 1 | Spring | 2018 | B |
| 23121 | Chavez | Finance | 110 | FIN-201 | 1 | Spring | 2018 | C+ |
| 44553 | Peltier | Physics | 56 | PHY-101 | 1 | Fall | 2017 | B- |
| 45678 | Levy | Physics | 46 | CS-101 | 1 | Fall | 2017 | F |
| 45678 | Levy | Physics | 46 | CS-101 | 1 | Spring | 2018 | B+ |
| 45678 | Levy | Physics | 46 | CS-319 | 1 | Spring | 2018 | B |
| 54321 | Williams | Comp. Sci. | 54 | CS-101 | 1 | Fall | 2017 | A- |
| 54321 | Williams | Comp. Sci. | 54 | CS-190 | 2 | Spring | 2017 | B+ |
| 55739 | Sanchez | Music | 38 | MU-199 | 1 | Spring | 2018 | A- |
| 76543 | Brown | Comp. Sci. | 58 | CS-101 | 1 | Fall | 2017 | A |
| 76543 | Brown | Comp. Sci. | 58 | CS-319 | 2 | Spring | 2018 | A |
| 76653 | Aoi | Elec. Eng. | 60 | EE-181 | 1 | Spring | 2017 | C |
| 98765 | Bourikas | Elec. Eng. | 98 | CS-101 | 1 | Fall | 2017 | C- |
| 98765 | Bourikas | Elec. Eng. | 98 | CS-315 | 1 | Spring | 2018 | B |
| 98988 | Tanaka | Biology | 120 | BIO-101 | 1 | Summer | 2017 | A |
| 98988 | Tanaka | Biology | 120 | BIO-301 | 1 | Summer | 2018 | null |
select name, title
from student natural join takes, course
where takes.course_id = course.course_id;
select name, title
from student natural join takes natural join course;
course natural left outer join prereq
Figure 4.5 — course natural left outer join prereq (Source: Slide 20)
| course_id | title | dept_name | credits | prereq_id |
|---|---|---|---|---|
| BIO-301 | Genetics | Biology | 4 | BIO-101 |
| CS-190 | Game Design | Comp. Sci. | 4 | CS-101 |
| CS-315 | Robotics | Comp. Sci. | 3 | null |
course natural right outer join prereq
Figure 4.6 — course natural right outer join prereq (Source: Slide 21)
| course_id | title | dept_name | credits | prereq_id |
|---|---|---|---|---|
| BIO-301 | Genetics | Biology | 4 | BIO-101 |
| CS-190 | Game Design | Comp. Sci. | 4 | CS-101 |
| CS-347 | null | null | null | CS-101 |
course full outer join prereq using (course_id)
Figure 4.7 — course full outer join prereq using (course_id) (Source: Slide 21)
| course_id | title | dept_name | credits | prereq_id |
|---|---|---|---|---|
| BIO-301 | Genetics | Biology | 4 | BIO-101 |
| CS-190 | Game Design | Comp. Sci. | 4 | CS-101 |
| CS-315 | Robotics | Comp. Sci. | 3 | null |
| CS-347 | null | null | null | CS-101 |
course inner join prereq on course.course_id = prereq.course_id
Figure 4.8 — course inner join prereq on course.course_id = prereq.course_id (Source: Slide 22)
| course_id | title | dept_name | credits | prereq_id | course_id |
|---|---|---|---|---|---|
| BIO-301 | Genetics | Biology | 4 | BIO-101 | BIO-301 |
| CS-190 | Game Design | Comp. Sci. | 4 | CS-101 | CS-190 |
course left outer join prereq on course.course_id = prereq.course_id
Figure 4.9 — course left outer join prereq on course.course_id = prereq.course_id (Source: Slide 22)
| course_id | title | dept_name | credits | prereq_id | course_id |
|---|---|---|---|---|---|
| BIO-301 | Genetics | Biology | 4 | BIO-101 | BIO-301 |
| CS-190 | Game Design | Comp. Sci. | 4 | CS-101 | CS-190 |
| CS-315 | Robotics | Comp. Sci. | 3 | null | null |
course natural right outer join prereq
Figure 4.10 — course natural right outer join prereq (Source: Slide 23)
| course_id | title | dept_name | credits | prereq_id |
|---|---|---|---|---|
| BIO-301 | Genetics | Biology | 4 | BIO-101 |
| CS-190 | Game Design | Comp. Sci. | 4 | CS-101 |
| CS-347 | null | null | null | CS-101 |
course full outer join prereq using (course_id)
Figure 4.11 — course full outer join prereq using (course_id) (Source: Slide 23)
| course_id | title | dept_name | credits | prereq_id |
|---|---|---|---|---|
| BIO-301 | Genetics | Biology | 4 | BIO-101 |
| CS-190 | Game Design | Comp. Sci. | 4 | CS-101 |
| CS-315 | Robotics | Comp. Sci. | 3 | null |
| CS-347 | null | null | null | CS-101 |
select ID, name, dept_name
from instructor
create view v as < query expression >
where <query expression> is any legal SQL expression. The view name is represented by v.
create view faculty as
select ID, name, dept_name
from instructor
select name
from faculty
where dept_name = 'Biology'
create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;
create view physics_fall_2017 as
select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = 'Physics'
and section.semester = 'Fall'
and section.year = '2017';
create view physics_fall_2017_watson as
select course_id, room_number
from physics_fall_2017
where building= 'Watson';
create view physics_fall_2017_watson as
select course_id, room_number
from physics_fall_2017
where building= 'Watson'
create view physics_fall_2017_watson as
select course_id, room_number
from (select course.course_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = 'Physics'
and section.semester = 'Fall'
and section.year = '2017')
where building= 'Watson';
insert into faculty
values ('30765', 'Green', 'Music');
Reject the insert
Insert the tuple
('30765', 'Green', 'Music', null)
into the instructor relation
create view instructor_info as
select ID, name, building
from instructor, department
where instructor.dept_name = department.dept_name;
insert into instructor_info
values ('69987', 'White', 'Taylor');
Issues
create view history_instructors as
select *
from instructor
where dept_name= 'History';
What happens if we insert
('25566', 'Brown', 'Biology', 100000)
into history_instructors?