Title: ?12? SQL????????
1?12? SQL????????
- 12-1 SQL???????
- 12-2 ??????
- 12-3 ????EquiJoins????
- 12-4 ??????
- 12-5 ??????
- 12-6 ???????
- 12-7 SQL???
- 12-8 ???????
212-1 SQL???????-??
- SQL????(Join)????????????,????????????????????????
?,???????????,???????????????,??,?????????????????
????? - ???Classes?????????sid?????eid,??????,???????????
???????
312-1 SQL???????-??
- SQL?????????????????????,????????,???????,????????
?,???? - ???????
- ????
- EquiJoins?????
- ???????
- ???????
412-1 SQL???????-?????
512-1 SQL???????-??????
612-2 ??????-??
- SQL?CROSS JOIN???????????????????????(Cartesian
Product),?????????????????????
712-2 ??????-??1
- SQL????Ch12-2-1.sql
- ???Students?????sid?name???Classes??????????c_no??
???eid??????????,???? - SELECT Students.sid, Students.name, Classes.c_no,
Classes.eid - FROM Students CROSS JOIN Classes
812-2 ??????-??2
- SQL????Ch12-2-2.sql
- ??????????WHERE????Students?Classes????????????,??
????????sid??,???? - SELECT Students.sid, Students.name, Classes.c_no,
Classes.eid - FROM Students
- CROSS JOIN Classes
- WHERE Students.sid Classes.sid
912-3 ????EquiJoins????
- 12-3-1 ?????
- 12-3-2 EquiJoins????
1012-3-1 ?????-??
- ???(Theta Join)???SQL???????????gt???lt???gt???lt??
?ltgt?????????2?????
1112-3-1 ?????-??
- SQL????Ch12-3-1.sql
- ?Classes???????????????????,???????????,????
- SELECT c1.c_no, c1.eid, c1.sid AS C1_SID,
- c1.score AS C1_SCORE, c2.sid AS C2_SID,
- c2.score AS C2_SCORE
- FROM Classes c1, Classes c2
- WHERE c1.c_no c2. c_no
- AND c1.eid c2.eid
- AND c1.score gt c2.score
1212-3-2 EquiJoins????-??
- EquiJoins??????????????????????????(Relationship)?
????,????????????????,????????? - SELECT table1.column1, table2.column2 ,
table3.column3 - FROM table1, table2., table3
- WHERE table1.column tabel2.column
- and table1.column table3.column
- EquiJoins????WHERE???????????????????????
1312-3-2 EquiJoins????-??1
- SQL????Ch12-3-2-1.sql
- ?Students?Classes????????????,????????????,????
- SELECT FROM Students, Classes
- WHERE Students.sid Classes.sid
1412-3-2 EquiJoins????-??2
- SQL????Ch12-3-2-2.sql
- ?Courses?Classes????????????????????????,????
- SELECT Courses., Classes.room
- FROM Courses, Classes
- WHERE Courses.c_no Classes.c_no
1512-4 ??????
- 12-4-1 ??????
- 12-4-2 INNER JOIN??
1612-4 ??????
- ??????????(Natural Join)????SQL???INNER
JOIN??????,?????
1712-4-1 ??????-??
- ?????????????SQL??????????????INNER
JOIN,??,????????EquiJoins?????SQL??,??????????????
??
1812-4-1 ??????-??
- SQL????Ch12-4-1.sql
- ?Students?Classes?????????????????,????
- SELECT Students., Classes.eid,
Classes.c_no,Classes.time,Classes.room - FROM Students, Classes
- WHERE Students.sid Classes.sid
1912-4-2 INNER JOIN??-??
- SQL???INNER JOIN????????????????,?????????????????
???
2012-4-2 INNER JOIN??-??1
- SQL????Ch12-4-2-1.sql
- ???Students?????sid?name???Classes???????????c_no?
????eid????????????????????,???????sid,???? - SELECT Students.sid, Students.name, Classes.c_no,
Classes.eid - FROM Students
- INNER JOIN Classes
- ON Students.sid Classes.sid
2112-4-2 INNER JOIN??-??2
- SQL????Ch12-4-2-2.sql
- Transact-SQL??????Ch12-4-2-1.sql???????,??INNER
JOIN????Courses???,??????????,???? - SELECT Students.sid, Students.name,
- Courses., Classes.eid
- FROM Courses INNER JOIN
- (Students INNER JOIN Classes
- ON Students.sid Classes.sid)
- ON Classes.c_no Courses.c_no
2212-4-2 INNER JOIN??-??2
2312-4-2 INNER JOIN??-??3
- SQL????Ch12-4-2-3.sql
- Transact-SQL???????Ch12-4-2-2.sql???????,??INNER
JOIN????Instructors???,??????????,???? - SELECT Students.sid, Students.name,
- Courses., Instructors.
- FROM Instructors INNER JOIN
- (Courses INNER JOIN
- (Students INNER JOIN Classes
- ON Students.sid
Classes.sid) - ON Classes.c_no Courses.c_no)
- ON Classes.eid Instructors.eid
2412-4-2 INNER JOIN??-??3
2512-5 ??????
- 12-5-1 LEFT JOIN???????
- 12-5-2 RIGHT JOIN???????
- 12-5-3 FULL JOIN????????
2612-5 ??????
- SQL???OUTER JOIN??????????????????????,??INNER
JOIN???????????????????????????????,??????OUTER
JOIN??,???? - ?????(LEFT JOIN)
- ?????(RIGHT JOIN)
- ??????(FULL JOIN)
2712-5-1 LEFT JOIN???????-??
- ????????????????,?????????????,??????????????????
2812-5-1 LEFT JOIN???????-??
- SQL????Ch12-5-1.sql
- Students??????Classes????????????,???????sid,????S
tudents????????,???? - SELECT Students.sid, Students.name, Classes.c_no,
Classes.score - FROM Students LEFT JOIN Classes
- ON Students.sid Classes.sid
2912-5-1 LEFT JOIN???????-??
3012-5-2 RIGHT JOIN???????-??
- ????????????????????,??????????????????
3112-5-2 RIGHT JOIN???????-??1
- SQL????Ch12-5-2-1.sql
- Classes??????Students??????????????,???????sid,???
?Students????????,???? - SELECT Classes.c_no, Classes.score, Students.sid,
Students.name - FROM Classes RIGHT JOIN Students
- ON Classes.sid Students.sid
3212-5-2 RIGHT JOIN???????-??1
3312-5-2 RIGHT JOIN???????-??2
- SQL????Ch12-5-2-2.sql
- ????JOIN????Students?Courses?Classes???,????
- SELECT Students.sid, Students.name, Courses.,
Classes.eid - FROM Courses RIGHT JOIN
- (Students INNER JOIN Classes ON Students.sid
Classes.sid) - ON Classes.c_no Courses.c_no
3412-5-2 RIGHT JOIN???????-??2
3512-5-3 FULL JOIN????????-??
- ???LEFT JOIN????????RIGHT JOIN???????,FULL
JOIN????????????????????????
3612-5-3 FULL JOIN????????-??
- SQL????Ch12-5-3.sql
- Students??????Classes?????????????,???????sid,????
- SELECT Students.sid, Students.name, Classes.c_no,
Classes.score - FROM Students FULL JOIN Classes
- ON Students.sid Classes.sid
3712-5-3 FULL JOIN????????-??
3812-6 ???????
- 12-6-1 ?????????
- 12-6-2 UNION????
- 12-6-3 INTERSECT????
- 12-6-4 EXCEPT????
3912-6-1 ?????????-1
- ??UNION??????????????????,???????,???????,???????
?? - SELECT column1, column2 FROM table1
- UNION
- SELECT column1, column2 FROM table2
- ??INTERSECT???????????????,?????????
- SELECT column1, column2 FROM table1
- INTERSECT
- SELECT column1, column2 FROM table2
4012-6-1 ?????????-2
- ??EXCEPT????????SELECT?????,????????SELECT?????,?
???????? - SELECT column1, column2 FROM table1
- EXCEPT
- SELECT column1, column2 FROM table2
4112-6-2 UNION????
- SQL????Ch12-6-2.sql
- ?Students?Instructors??????name???????????????????
,???? - SELECT name
- FROM Students
- UNION
- SELECT name
- FROM Instructors
4212-6-3 INTERSECT????
- SQL????Ch12-6-3.sql
- ?Students?Instructors??????name???????????????????
??????,???? - SELECT name
- FROM Students
- INTERSECT
- SELECT name
- FROM Instructors
4312-6-4 EXCEPT????
- SQL????Ch13-6-4.sql
- ?Students?Instructors??????name????????????Student
s???,????Instructors????????,???? - SELECT name
- FROM Students
- EXCEPT
- SELECT name
- FROM Instructors
4412-7 SQL????
- 12-7-1 ??????
- 12-7-2 ?????????
- 12-7-3 ?????????
- 12-7-4 ?????????
4512-7 SQL????
- SQL???(Subquery)?????????????,??????SQL???????????
???,???????(Nested Query)? - ????????????SELECT????,????,??????????????,???????
?????
4612-7-1 ??????-??
- ???????SQL????,????????SELECT???WHERE??,??????????
??????,?????????SELECT??? - ??SQL?????????,?????????,???????????????,???????
4712-7-1 ??????-??
- ????????SELECT???WHERE???GOUP BY???HAVING??,??????
?????????????,????????,???? - SELECT column1, column2, , columnN
- FROM table1
- WHERE column (SELECT column
- FROM table2
- WHERE conditions)
4812-7-1 ??????-????
- ??????SQL????????
- ??????SELECT???????????,????????????????
- ??????,???????ORDER BY??,????????ORDER
BY??,??????GROUP BY?????? - ???????????,????????IN??????
- BETWEEN/AND?????????????,???????????
4912-7-2 ?????????-??1
- SQL????Ch12-7-2-1.sql
- ?Students???????name??????sid,????Classes?????????
???????,???? - SELECT COUNT() FROM Classes
- WHERE sid
- (SELECT sid FROM Students WHERE name'???')
5012-7-2 ?????????-??2
- SQL????Ch12-7-2-2.sql
- ?Students?????????GPA??????????????,????
- SELECT sid, name, tel, GPA FROM Students
- WHERE GPA gt
- (SELECT AVG(GPA) FROM Students)
5112-7-3 ?????????-??
- SQL??????EXISTS?IN?????????,EXISTS????????????????
??,?????????????? - IN???????????????????????,??????????????
5212-7-3 ?????????-EXISTS??(??1)
- SQL????Ch12-7-3-1.sql
- ?Students?????Classes??????CS222?????????,??
- SELECT sid, name, tel, GPA FROM Students
- WHERE EXISTS
- (SELECT FROM Classes
- WHERE c_no CS222 and Students.sid
Classes.sid)
5312-7-3 ?????????-EXISTS??(??2)
- SQL????Ch12-7-3-2.sql
- ?Classes?Courese????????221-S?100-M?????????,????
- SELECT FROM Courses
- WHERE EXISTS
- (SELECT FROM Classes
- WHERE (room221-S or room100-M) and
Courses.c_noClasses.c_no)
5412-7-3 ?????????-EXISTS??(??3)
- SQL????Ch12-7-3-3.sql
- ?????,?????????Ch13-6-3-2.sql?SQL??????????,????
- SELECT DISTINCT Courses. FROM Courses, Classes
- WHERE (Classes.room'221-S' or Classes.room'100-M
') and Courses.c_noClasses.c_no
5512-7-3 ?????????-IN??(??1)
- SQL????Ch12-7-3-4.sql
- ?Courses?Classes???????S004???????,????
- SELECT FROM Courses
- WHERE c_no NOT IN
- (SELECT c_no FROM Classes WHERE sid'S004')
5612-7-3 ?????????-IN??(??2)
- SQL????Ch12-7-3-5.sql
- ?Students?Classes?Instructors?????????????????????
,???? - SELECT FROM Instructors
- WHERE eid IN
- (SELECT eid FROM Classes
- WHERE sid(SELECT sid FROM Students
- WHERE name '???'))
5712-7-3 ?????????-IN??(??3)
- SQL????Ch12-7-3-6.sql
- Ch13-6-3-5.sql???????????????????,????
- SELECT DISTINCT Instructors.
- FROM Instructors, Classes, Students
- WHERE Instructors.eid Classes.eid
- and Classes.sid Students.sid
- and Students.name '???'
5812-7-4 ?????????-??
- ?SQL??????EXISTS?NOT???????,??????????????????,???
????????? - ?????SQL??????EXISTS?NOT????????
- ????????????,??6?????????????,????
- RnS R (R-S) S-(S-R)
5912-7-4 ?????????-????(??)
- SQL????Ch12-7-4-1.sql
- ?Students?Instructors??????name????????????Student
s????????,???????Instructors???,???? - SELECT name FROM Students
- WHERE NOT EXISTS (SELECT name FROM Instructors
WHERE Instructors.name Students.name )
6012-7-4 ?????????-????(??)
- ???R?S??????????????,???????,????
- ?????R-S??????????,?Transact-SQL?MySQL
MaxDB?ANSI-SQL??????? - ??????R-S???????????,??????R
Temp????,?????R?S?????????
6112-7-4 ?????????-????(???)
- SQL????Ch12-7-4-2.sql
- ?Students?Instructors??????name????R-S?????,??????
??Temp???,???? - SELECT name
- INTO Temp
- FROM Students
- WHERE NOT EXISTS (SELECT name FROM Instructors
- WHERE Instructors.name Students.name
)
6212-7-4 ?????????-????(???)
- SQL????Ch12-7-4-3.sql
- ???R (R-S)????,?????R Temp,??????????,????????
????????,???? - SELECT name FROM Students
- WHERE NOT EXISTS (SELECT name FROM Temp
- WHERE name Students.name )
6312-8 ???????
- 12-8-1 UPDATE?JOIN?????
- 12-8-2 DELETE?JOIN?????
6412-8-1 UPDATE?JOIN?????-??
- SQL????Ch12-8-1.sql
- ?Classes?????????????????300-K?????,?????900am,??
?? - UPDATE Classes SET Classes.time 900am
- FROM Classes INNER JOIN Instructors
- ON Classes.eid Instructors.eid
- WHERE Instructors.name ??? and Classes.room
300-K
6512-8-1 UPDATE?JOIN?????-??
- ??SQL????Classes???????????,????
- SELECT eid, sid, c_no, room, DATEPART(Hour, time)
AS hour - FROM Classes
6612-8-2 DELETE?JOIN?????-??
- SQL????Ch12-8-2.sql
- ?Classes???????????????300-K?????,????
- DELETE Classes FROM Classes INNER JOIN
Instructors - ON Classes.eid Instructors.eid
- WHERE Instructors.name ??? and Classes.room
300-K
6712-8-2 DELETE?JOIN?????-??
- ??SQL????Classes???????????,????
- SELECT eid, sid, c_no, room FROM Classes