Title: Relational Algebra
1?????? ??????
Based on the lecture notes of Prof. Sagiv
2???? ??????? ?????(?? ?????? "????? ?????")
- The Relational Data Model
?????? ?????? ?????? ?? ????? ?????
3??? ??? ????? ????
- ??? ????? ?????? ?????
- ??????, ????? ???? ????? ?????? ???? ???? ??????,
?? ?????? ????
???? ????? ????? ?????
?? ?? ????? ???? ???????
4??? ??? ?????? ?????
- ????? ??? ????? n-?? ?? ?????
- ????????? ??? n-??? ????? ?? ??? ????? ?? ?? ???
?????? ????, ?????? - (240,white,3) ? (white,240,3)
- ??? ?????? ?? ???? ??????? ???? ??????? ?????
???? ?? ???
5????
- ?? ????? ??? ??? ???? ??????? ??? ????? ??????
??? ?????, ????? ??? ??????? ??? ???? - ????? ????????? (???, ???? ???????), ????????
?????? ??????? - ?????? ??????? ????? ??? ????? 1 ???'
6????? ?? ??? (?? ?? ?????)
- ????? ?????????? (???? ???????) ?? ??? ????? ????
- ????? ?? ???? ????? ???
- sid, sname, year
- ???? ??? ?????, ?? ???? ??
??????? ???? ???? ???? ???
?????? ?? ???? ?? - ????? ?????? ?? ????? ?? ?????
7??? ??? ????? ????? ?? ??????
- ??????? ??? ??? ????? ????? ?? ??????
- ??? ??????? ???? ?? ???? ?????????
- ????, ??? ??????? ???? ?? ???? ???? ??????
- ????? ?????? ??????, ??? ??? ?? ???? ?????, ??
???? ????? ????? ?????? ???? ????? (??? ??????
?????? ???? ??? ???????? SQL) - ??????? ??????, ??? ??? ???? ?????, ????? ??
????? ?????? ???? ??? ??? ????
8??????
- ??? ??? ????? ????? ?? ??????
- ??? ??????? ???? ?? ???? ?????????
- ????? ?????????? ?? ???? ????? ????
- ????? ?????? ?? ????? ?? ????
9?????? ??????
10?????? ??????
- ?????? ?????? ??? ???? ?? ?????? ?? ?????,
??????? ???? ???? ??????? - ?????? ?? ?? ????? ??? ???
- ????? ???? ???? ?????? ?? ?????? ?????? ?? ??????
?? ?????? ??????, ????? ???? ????? ???????
??????? - ?????? ?????? ????? ???? ?????? ??????? ?? ????
?????? ?????
11???? ?????? ???? ???? ?? ??????
- ?????? ??????? ?? ??????, ???? ?????, ????? ?????
- ?????? ??? ?????? ?? ?? ????? ??? ??? ???? ?????
???? ?????? ????? ??? ?? ????? ????? ?? ?????? - ?????? ???????? ???? ??? ?????
- ??? ????????
- ??? ????????
- ?????? ???????? ???? ?????? ?????? ?????
12????? ??????
S Students T Teachers R Students, their
courses, and their teachers
13??????? ?? ?????? ??????
- ?????? ?????? ????? ?? ??????? ?????
- ????
- ?????
- ????? ??????
- ?????
- ????
- ???? ?? ?????? ?????? ?? "????? ??"
- ?????? ?????? ?????? ?????? ????? ??????? ????????
14????
15???? Projection
- ????? ????? ??? ?????? (????? ?? ??? ???)
- ????? ????? ????? ??? ????????
- ?????? ????? ?????? ??????? ???? ?????? ??????
??????? - ?????? ???? ??? ????? ?? ??????, ?????? ???? ??
???? ??? ??? ?????, ?????? ???? ??????? ????
????? - ??????? ????? ????? ?"? ???????? ??
??????? ?? ????? ??????? ??????
16????? ?? ????
- ??? ???? ???? ?? ??? R ?? ??????? tid ?- course
?????? ?? ?????? - ??????
R
17???? ?????? ???? ? SQL?
?"? ?????? ? Select
Select distinct tid, course From R
18????? ?????? ????? ????
R
19?????
20Selection ?????
- ????? ?????? ??? ?????? (????? ?? ??? ???)
- ????? ?????? ????? ??? ?? ???????
- ?????? ?? ???? ??? (??? ?? ????) ???? ?????? ????
???? ?????? - ??????? ?????? ????? ?"? ???????? ??
??????? ?? ????? ???? ?????? ??????
21????? ?? ?????
R
- ??? ????? ????? R ?? ?? ??????? ?? ????? ??-id
??? ??? 20, ?????? ?? ?????? - ??????
22???? ?????? ????? ? SQL?
?"? ?????? ? From
Select distinct From R Where tid20
23???? ?? !
- ????? ????? (Projection) ??????? ?? ????? ??????
??????? ? select ? SQL - ????? ?????? (Selection) ??????? ?? ????? ??????
??????? ? Where ? SQL - ?????, ????? select ????? ?????? ????? ??????? ??
????? ?? SQL
24????? ?? ???? ??????
- ?????? ??? ???? ?? ????? ??????? ????? ?????
??-id ??? ??? 20 - ??????
R
??? ???? ?????? ?? ???? ?? ?????? ???????
25????? ?????
S
- ??? ???? ????????? ???? ???????
- ??????
26?????? ????? ?????? ????????? ??????
27????? ?? ????? ??????
- ????? ?????? ?? ????? ?????? ??? , ???? C
??? ????? - ????? ????? ???????? ???????? ?"? ?????? ??????
?? ?- (??? ???? ??????) - ?????? ??? ??????
- attribute1 op attribute2 ??
- attribute1 op constant
28?????? ??????
- ?????? ??????? (op) ??
- ?? ????? ????? ??????? (char string), ??? ??????
???? ??? ??? ??????, ?????? -
-
29???? ??
- ??????? ?? ????? ?? ?????? ????????? ????????
????????? ???????? ? SQL ???? count, sum, avg
???' - ?????? ??? ?? ?????? ????????? ???????? ???????
?????? (Group By) ?????? ? SQL
30?????
S
- ??? ?- id ?? ????????? ???? ??? jones ??? ??????
???? ?????? ?? ??????? - ??????
31????? ??????
- Cartesian Product
- (Cross Product)
32????? ?????? Cross Product
- ????? ?????? ??? ????? ???????, ??????? ?"? ,
???? ????? ?? ????? ?? ??? ??? ?? ?? ????? ??
???? ???? - ???? ??????? ?????? ???? ????? ????? ???????
?????? ???????? - ???? ??????? ?????? ???? ?????? ????? ???????
?????? ????????
33????? ?????? (????)
S Students T Teachers R Students, their
courses, and their teachers
34(No Transcript)
35???? ?????? ??? ????? ?????? ?? ??????
????????. ?????? ????? ?????? ???????? ??????
????? ????????? ???????, ?????? ????? ?? ????
?????????? ????. ??????, ??????? ?? ????????
?????? ?????? ?"? ???????, ??? ????? ?????? ??
???? ?? ????? ????. ??? ??? ????? ?? ????? ???
?????? ?? ?? ???? ?????? ??? ????????, ?????
?????? ??????? ????? R.tid ??????? ??????? ?????
????? T.tid
36?????? ????? R ? T
37????? ?????? ?????
- ?????? ????? ???? ????? ?????
- ????? ??????
- ????? ?????? ???? ?????? ??????? ????? ?? ?? ???
????????? ???????? ???? ?????? - ????? ???? ?? ????? ?? ???? ??? ?? ?? ?????
?????? - ?????, ??? ?????? ?????? ?? ???? ?? ?????? ??
????? ????
38?????? ????? ?- R ?? ????? ?- T ????? ????????
???? ?? ?????????? ????????
39?????? ?????? ?????? ?? ??????
R ? T ?R.tid,sid,course,tname,dept?R.tidT.tid
(R ? T)
40?????? ??? ?????? ????? R ? T
R ? T ?R.tid,sid,course,tname,dept?R.tidT.tid
(R ? T)
41??????? ?????? ???????????? ????? ????
42?? ???? ?????? ??????? ?? ??????? ?????? ?"?
sid202?
?tname (?sid202 (R ? T))
?tname (?sid202 )R( ? T)??
43??? ???? ?????? ?? ????????? ????? ????
????? ???? ??? ?????????? ??????????
?year (?tnamelevy (S ? R ? T))
?year (S ? R ? ?tnamelevy)T()??
44????? ?????? ???? ???????? ?????? ???????
45???? ????? ?????? ?? ?? ????? ?????? ???????
R ? T
46?? ?????? ??????
R ? T
47?????
48?????Union
- ????? ??? ?????? ????? ???? ????, ????? ???
??????, ????? - ???? ?????? ???? ????? ?? ?????????, ??????
- ?????? ????? ???? ??????? ??? ????? ???? type
(???? ?? ??????? ?? ????? type ??? ???????) - ?? ?? ???????? ?????????? (?? ???? ?????????), ??
?????? ??? - ???? ?????? ???? ???? ??????, ??????
- ?????? ????? ???? ????? ??? ????? ???? type (??
????? type)
49?????
50????
51????Difference
- ????? ??? ?????? ???? ???? ???? ????? ???? ???
?????, ????? ??? ??????, ??? ?????? ????? ??
????? ??????
52?????
53?????
54????? Intersection
- ????? ???? ???? ?????? ????? ?? ?????
- ??? ????? ????? ?????? ????? ?????? ????? ??????
?????
55?????
56????? ?????? ?????? ????? ?????????
57???? ?? ???? ???????????? ???????
- ?????? ?????? ?????????? ??? ??????
- ????, ????? ?????? ????? ????? ?????? ????? ??
???? ????????? - ?????, ???? ??????? ?????? ??????
- ?????? ??????? ????? ??? ??????? ???'
58?????
- ?????? ??????? ?? R ?- T, ???? ????? ?? ??? ????
??????? ????? ???? ??? ?"? ?????? ???? - ?????? ????? (??????? ????) ???????? ??????
??????? ??? ???????? (????? 1 ??? ??????? ?????) - ?????? ?? ????? ??? ??? ?? ???? ?????? ???? (???,
??? ??? ?????? ?? ???? ??)
59(No Transcript)
60(No Transcript)
61(No Transcript)
62????? ?????
- ???? ???? ?? ?????? ????? ?? R ?- T ????? ???
- ?? ?????? ????? ??? ??? ??? ?? ???? ?????? ????
63(No Transcript)
64????
- ?? ?????? ?????? ??????? ?????? ??????, ?? ????
???? ?????? ?"? ?????? - ?????? ?????? 1 ?- 4
- ?????? ????? 1 ?????
65????? ??
66??? ????? ???
- ????? ????? ?? ?????? ????? ?? ???? ??????????
??? ????? (?????, ?? ????) ?? ?????? - ??? ????? ???? ?????? ????? ??????, ??? ????
?????? ?? ?? ?????? ?????
67????? ?? Renaming
- ?????? ?????? ?"?
- ??????, ????? ?? ??????
- ????? ?? ???? ?? ?????? ???????
????? ?????? ?? ????? P(tid1,sid,course,tid2,tnam
e,dept) - ?? ????? ?? ?????? ??? P ????????? ??????? ????
?????????? - ??????? ?????? ?? ?? ??????? ???? ?????
68?????
69????? Join
- ????? ?????? ??? ????? ????? ?? ????? ??????
?????? (??????? ??????? ?? ????) - ????? ?????
- condition join ?? theta join (????? ?????)
- equi-join (??? ??????)
- natural join (????? ???? ????? ??? ????)
70????? ????? Condition Join
- ????? ????? ??? ????? ?????? ????
- C ??? ????? ?????? ??????? ?????? ( , )
????????? ?????? - ????? ?? ?????? ??? ??? ??????? ????? A ?? R
???? ??????? ????? B ?? S - ???? ?? ??? ???? ?????????? ?? ???????, ???
- ??????
71???? ??????
- ??????
- ???? ????? ?????? (?????? ??????) ??? C, ????
??????? ?????? (???? ???? ?? ????????) ????? ???
C? - ?- C ??????? ??????? ??????
- ?- C ??????? ??????? ??? ???? ?????????
72R ?R.tidT.tid T
73?????? ?????? ?? Equi-Join
- ????? ?????? ??? ????? ?????? ????
- ???? C ??? ????? (??????????) ?? ??????? ??????
- ?????, ?? ?????? ??? ?????? ??? ??????? ????? A
?? R ???? ??????? ????? B ?? S - ??????
???? ??? C ???? C ??? ??? ?????? ???? 70
74 ????? ???? Natural Join (????? ??? ????)
- ?????? ???? ?????? ?? ?? ????? ??????????
???????? ?- R ??- S ???????? ?? ???? ??? ??? ???
?????? ??????
??? ?????? ????? ?? ??? ?????? ???????? ???
?????? ????? ?? ?? ??????? ????????
75??? ??? ???????? ?? ???? ????
76??? ???
- ??? ??? ????? ?? ?????? ????? ????? ???
- ?????? ?? ????? ?????? ??? ???? ?? ??? ????
?????? ??? (??? ???? ???????) - ???? ??????? ?? ???? ?? ????? ??? ???? ?? ??????
?????? ?? ??? ??? - ?????? ??????, ???? ?? ????? ?? ?????? ?????? ??
??????, ???? ?? ?????? ?? ??? ?????? ???
77??? ??? ????? ?????? ?????????
- ????? ????? ????? ??? ??? ????? ???? ?? ?????????
- ?? ?????? ?????, ??????? ?"? ()
- ?????, ?????? ????? ??? ????? ??? ????? ???? ??
????????? (???, ????? ??? ????? ????) - ??? ???
- ??? ????? ????? ?? ?????? ?????
78???? ?? ????? ????????? ????
- Ø ???? ?? ?????? ?????
- ?????? ?Ø(R) ????? ???? ?? R ?? ????? ???? ??
????????? - ?????? ??? ??? ??? ???? ????, ????? ?????? ???
??? ???? ????????? ????? - ??? ????? ????? ?? ?????? ????? ?? R ???? ???
- ??? ??? ?? R ??? ???
???? ?? Ø ?????? ????? ???? "??" ?? "??" ????,
??? ?? ???????? ??????? ?? ????? db?
79????
- ??? ?????? ?? ????? ?????? ?? ??? ???? ?????? ???
??? ???? ?????
80?????? ??????????
81?????? ??????????
- ????? ??? ????????? ?? ???? ??????? ?????? ??????
(??? ?????? ?? ????? ?????) ?? ?????? ????? ??
?????, ????? ?????? ?????? ??????? ?????? ???
????? ???? ????? ??????? - ???? ?????? ?? ???????????
- ???? ?????? ???? ???????????
82???? ??????? ?? ???????
- ???? ?? ??? ???? ????? ??
- ????? ???? ?? ???????
83????? ?????? ???? ???????
- ??? ??????? ???????? ??? ?????? ?? ?? ???? ??????
???? ????? - ??????, ?????? YXY ???? ?????? 2YX
- ??? ?????? XY ??????? X2Y ???? ??????, ?? ??
?????? ???? ????? ?? ?? Y0 - ??????? ???????? ?? "???? ?????? ???? ?????" ???
??? ?????? ???? ????? ??? ???? ?????? ?? ?????
???? ???????
84????? ?????? ?? ???????
- ??? ??????? ??????? ?????? ??? ?????? ?? ?? ????
?????? ???? ?????, ????? - ?? ?? ?????? ???? ????? ??? ???? ?????? ?? ?????
???? ???? ?????? - ????? ???? ??????? ??????
?year (S ? R ? ?tnamelevy)T()
?year (?tnamelevy (S ? R ? T))
85???? ?????? ?? ?????????? ???? ?????
- ????? ????????? ?????? ???? ?? ???? ?????, ???
?????? ???? ???? ????? ???? - ??? ??? ?????? ?? ??? ???? ???????? ????? ???
???? ???? ???? ??? ??????
?year (S ? R ? ?tnamelevy)T()
?year ( ?tnamelevy (S ? R ? T))
86????? ?????? ?????? ?? ???????
- ?????? ????? ?????? ???? ?? ??????? ??? ?????
????? ??? ??????? ?????? ?????? ????? ??? ????? - ??? ?????? ?? ?????? ??????? ?????? ?????????????
????? ?????? - ?? ????? ????? ????? ????? ??, ???? ???? ????
?????? ???????? (????????) ?? ?????? ????? ????
???? ?????? ??? - ???? ?? ?? ?????? ????? ?????, ?????? ?????
"??????????? ?? ???????"
87????? ?? ????? ?????????? ??? ?????
- ?????? ?? ????? ????? ?? ????? ???? ???? ???? (??
??? ????? ???? ????? ????) ????? ?????? - ???, ????? ?? ?????? ??? ????? ??? ????? ??????
???? ?? ?????? - ?????? ?????, ?????? ??????, ???? ????? ??????
????? ??? ?????? ????? ????? ??????? ????? ???
?????
88?????
- ?????? ?????? ????, ??????? ?????? ?? ?"? ??
???????? ?? ??????? - ??????? ??????
?year (?tnamelevy (S ? R ? T))
?year (S ? R ? ?tnamelevy)T()
89????? ?????
- ?????, ????? ??? ????? ??????????? ???? ????????
((S ? R) ? T) ?- (S ? (R ? T)) ??? ??????, ?????
?? ?????? ???? ????? - ??? ???? ?????? ???? ????? ??????!
- ????? ???? ???????? ?? ????? ?????? ???? ?????
- ???? ?????? ????????? ???? R, S, ?- T
- ???? ((S ? R) ? R) ?- (S ? (R ? R))