資料庫ch4–1 join

慈慈
Nov 2, 2020

--

Join Expression

Natural join

把有相同attribute且值一樣的 合併
其他attribute照抄
⚠️寫在from裡面⚠️
natural join可以無限join下去,但順序很重要

select name, course_id
from students, takes
where student.ID = takes.ID;
select name, course_id
from student natural join takes

上面兩種寫法一樣~

select name, title
from (student natural join takes) join course using (course_id)

特別指定要用什麼attribute來join

select *
from student join takes on student.ID = takes.ID

注意

⛔️attribute名稱一樣,但意義一樣?⛔️
⛔️所有的common attribute值相同才會合併⛔️

Inner join

inner join會搭配on使用
如果只有inner join沒有on,結果就像Cartesian product
⚠️只要有natural,common attribute就會合併⚠️

Outer join

有兩個表格 A, B
希望輸出A表格的所有資料
無論兩表格有沒有找到匹配common attribute(相同的值)
都要輸出,若沒有的資料就放null

又分為left outer join, right outer join, full outer join

course nature left outer join prereq
course nature right outer join prereq
course nature full outer join prereq

--

--

No responses yet