Chapter 4: Intermediate SQL
Chapter 4: Intermediate SQL โข ์กฐ์ธ ํํ์ (Join Expressions) โข ๋ทฐ (Views) โข ๋ฌด๊ฒฐ์ฑ (Integrity Constraints)
์กฐ์ธ ๋ฆด๋ ์ด์
(Joined Relations) โข ์ฃ ์ธ ์ฐ์ฐ์ ๋ ๋ฆด๋ ์ด์
์ ์ทจํด ๋ ๋ค๋ฅธ ๋ฆด๋ ์ด์
์ ๊ฒฐ๊ณผ๋ก ๋๋ ค์ค๋ค. โข ์ฃ ์ธ ์ฐ์ฐ์ ์ฃผ์ด์ง ์กฐ๊ฑด ํ์ ๋ ๋ฆด๋ ์ด์
๋ด์ ํํ๋ค์ด ๋งค์นญ๋๋ ๊ฒฐ๊ณผ๋ง์ ํฌํจํ๋ ์นดํฐ์
๊ณฑ ์ฐ์ฐ์ด๋ค. ๋ํ ์กฐ์ธ ๊ฒฐ๊ณผ์ ์ด๋ค ์ ํธ๋ฆฌ๋ทฐํธ๊ฐ ๋ํ๋ ์ง๋ฅผ ์ง์ ํ๋ค. โข ์ด๋ค ๋ถ๊ฐ์ ์ธ ์ฐ์ฐ์ ์ผ๋ฐ์ ์ผ๋ก from์ ๋ด์ ๋ถ ์ง์ ํํ์์ผ๋ก ์ฌ์ฉ๋๋ค. โข select *from instructor natural join teaches;
์กฐ์ธ ์ฐ์ฐ โ Example โข Relation course โข Relation prereq โข Observe that prereq informationis missing for CS-315 and courseinformationis missing for CS-347
์์ฐ ์กฐ์ธ (Natural Join) โข Natural join matches tuples with the same values for all common attributes, and retains only one copy of each common column โข select *from instructor natural join teaches;
Natural Join Example โข List the names of instructors along with the course ID of the courses that they taught. โข select name, course_idfrom instructor, teacheswhere instructor.ID = teaches.ID; โข select name, course_idfrom instructor natural join teaches; โข select name, course_idfrom instructor join teaches using(ID);
Outer Join โข ์ ๋ณด ์์ค์ ํผํ๊ธฐ ์ํ ์กฐ์ธ ์ฐ์ฐ์ ํ์ฅ. โข ์กฐ์ธ์ ๊ณ์ฐํ๊ณ ๋ค๋ฅธ ๋ฆด๋ ์ด์
์ ํํ๊ณผ ๊ทธ ๊ฐ์ด ์ผ์นํ์ง ์๋ ์ด๋ค ๋ฆด๋ ์ด์
์ ํํ๋ค์ ์ฃ ์ธ์ ๊ฒฐ๊ณผ์ ์ถ๊ฐํ๋ค. โข ๋ ๊ฐ์ ์ฌ์ฉํ๋ค.
Left Outer Join โข coursenatural left outer joinprereq
Right Outer Join โข coursenatural right outer joinprereq
Full Outer Join โข coursenatural full outer joinprereq
Joined Relations โข ์กฐ์ธ ์ฐ์ฐ์ ๋ ๋ฆด๋ ์ด์
์ ์ทจํด ๋ ๋ค๋ฅธ ๋ฆด๋ ์ด์
์ ๊ฒฐ๊ณผ๋ก ๋๋ ค์ค๋ค. โข ์ด๋ค ๋ถ๊ฐ์ ์ธ ์ฐ์ฐ์ ์ผ๋ฐ์ ์ผ๋ก from์ ๋ด์ ๋ถ ์ง์ ํํ์์ผ๋ก ์ฌ์ฉ๋๋ค. โข ์กฐ์ธ ์กฐ๊ฑด (Join condition)- ๋ ๋ฆด๋ ์ด์
๋ด์ ์ด๋ค ํํ๋ค์ด ๋ถํฉํ๊ณ ์กฐ์ธ ๊ฒฐ๊ณผ์ ์ด๋ค ์ ํธ๋ฆฌ๋ทฐํธ๊ฐ ๋ํ๋ ์ง๋ฅผ ์ ํ๋ค. โข ์ฃ ์ธ ์ ํ (Join type)- ๋ค๋ฅธ ๋ฆด๋ ์ด์
์ ์ด๋ค ํํ๊ณผ ๋ถํฉํ์ง ์๋ ํํ๋ค์ ์ด๋ป๊ฒ ์ทจ๊ธํ ๊ฒ์ธ๊ฐ(์กฐ์ธ ์กฐ๊ฑด์ ๊ทผ๊ฑฐํด)๋ฅผ ์ ํ๋ค.
Joined Relations โ Examples โข select* from course (inner) join prereq oncourse.course_id = prereq.course_id โข select * from course, prereqwhere course.course_id = prereq.course_id โข What is the difference between the above, and a natural join? โข course left outer join prereq oncourse.course_id = prereq.course_id
Joined Relations โ Examples โข course natural right outer join prereq โข coursefullouter join prereq using(course_id)
๋ทฐ(Views) โข ์ด๋ค ๊ฒฝ์ฐ์๋, ์์คํ
์ ๋ชจ๋ ์ฌ์ฉ์์๊ฒ DB์ ์ ์ฒด์ ์ธ ๋
ผ๋ฆฌ์ ๋ชจ๋ธ์ ๊ณต๊ฐํ๋ ๊ฒ์ด ๋ฐ๋์ํ์ง ์์ ์ ์๋ค. โข ์ด๋ค ์ฌ๋์ด ๊ฐ์ฌ์ ID, ์ด๋ฆ, ํ๊ณผ๋ช
์ ์๊ณ ์ ํ๋ ๊ฒฝ์ฐ (๋จ ๊ธ์ฌ๋ ํ์ ์์), ๋ค์๊ณผ ๊ฐ์ ์ง์๋ฅผ ์์ฑํ์ฌ์ผ ํ๋ค. select ID, name, dept_namefrom instructor โข ๋ทฐ (view)๋ ์ด๋ค ์ฌ์ฉ์์ ๋ทฐ๋ก๋ถํฐ ์ด๋ค ๋ฐ์ดํฐ๋ฅผ ์จ๊ธฐ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค. โข ๋ทฐ๋ DB์๊ฐ๋
๋ชจ๋ธ์ ํด๋นํ์ง ์์ผ๋ฉฐ, ์ค์ง ์ฌ์ฉ์์๊ฒ ๋ณด์ด๋ ๋ชฉ์ ์ ๊ฐ์ ๋ฆด๋ ์ด์
(virtual relation)์ ์๋ฏธํ๋ค.
๋ทฐ ์ ์ โข ๋ทฐ๋ฅผ ์์ฑํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ช
๋ น์ ์ฌ์ฉํ๋ค. create view v as < query expression > ์ฌ๊ธฐ์: - <์ง์ ํํ์>์ ์ ๋ฒํ ํํ์์ด๋ค. - ๋ทฐ๋ช
์ v๋ก ํํ๋๋ค. โข ์ผ๋จ ๋ทฐ๊ฐ ์ ์๋๋ฉด, ๋ทฐ๋ฅผ ์์ฑํ๋ ๊ฐ์ ๋ฆด๋ ์ด์
์ ์ฐธ์กฐํ๋๋ฐ ๋ทฐ๋ช
์ด ์ฌ์ฉ๋ ์ ์๋ค. โข ๋ทฐ ์ ์๋ ์ง์ ํํ์์ ํ๊ฐํจ์ผ๋ก์จ ์๋ก์ด ๋ฆด๋ ์ด์
์ ์์ฑํ๋ ๊ฒ๊ณผ๋ ๋ค๋ฅด๋ค. ๋์ , ๋ทฐ ์ ์๋ ๋ทฐ๋ฅผ ์ฌ์ฉํ๋ ์ง์๋ด์ ๋์น๋ ํํ์์ ์ ์ฅํ๋ค.
Example Views โข A view of instructors without their salarycreate view faculty asselect ID, name, dept_namefrom instructor โข Find all instructors in the Biology departmentselect namefrom facultywhere dept_name = โBiologyโ โข Create a view of department salary totalscreate view departments_total_salary(dept_name, total_salary) as select dept_name, sum (salary)from instructorgroup by dept_name;
๋ค๋ฅธ ๋ทฐ ์ ์๋ฅผ ์ด์ฉํ ์๋ก์ด ๋ทฐ ์ ์ โข create view physics_fall_2009as select course.course_id, sec_id, building, room_numberfrom course, sectionwhere course.course_id = section.course_idand course.dept_name = โPhysicsโand section.semester = โFallโand section.year = โ2009โ; โข create view physics_fall_2009_watson as select course_id, room_numberfrom physics_fall_2009where building= โWatsonโ;
๋ทฐ ํ์ฅ โข Expand use of a view in a query/another view create view physics_fall_2009_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 = โ2009โ) where building= โWatsonโ;
๋ค๋ฅธ ๋ทฐ๋ฅผ ์ฌ์ฉํด ์ ์ํ ๋ทฐ โข ํ๋์ ๋ทฐ๋ ๋ค๋ฅธ ๋ทฐ๋ฅผ ์ ์ํ๋ ํํ์์ ์ฌ์ฉ๋ ์ ์๋ค. โข ๋ทฐ ๋ฆด๋ ์ด์
v1์ v2๊ฐ v1์ ์ ์ํ๊ณ ์๋ ํํ์์ ์ฌ์ฉ๋๋ค๋ฉด ๋ทฐ ๋ฆด๋ ์ด์
v2์ ์ง์ ์ข
์ํ๋ค๊ณ ๋งํ๋ค. โข ๋ทฐ ๋ฆด๋ ์ด์
v1์ ์ข
์ ๊ทธ๋ํ๋ด์ v2์์ v1๋ก์ ๊ฒฝ๋ก๊ฐ ์กด์ฌํ๋ฉด ๋ทฐ ๋ฆด๋ ์ด์
v2์ ์ข
์ํ๋ค๊ณ ๋งํ๋ค. โข ๋ทฐ ๋ฆด๋ ์ด์
v๊ฐ ๊ทธ ์์ ์ ์ข
์ํ๋ฉด ์ํ์ ์ด๋ผ ๋งํ๋ค.
๋ทฐ ํ์ฅ โข ๋ค๋ฅธ ๋ทฐ์ ์ฉ์ด๋ก ์ ์๋ ๋ทฐ์ ์๋ฏธ๋ฅผ ์ ์ํ๋ ๋ฐฉ๋ฒ โข ๋ทฐ v1์ ๊ทธ ์์ ์ด ๋ทฐ ๋ฆด๋ ์ด์
์ ์ฌ์ฉ์ ๋ดํฌํ ์ ์๋ ํํ์ e1์ผ๋ก ์ ์๋๋ค ํ์. โข ํํ์์ ๋ทฐ ํ์ฅ์ ๋ค์๊ณผ ๊ฐ์ ๋์น ์ ์ฐจ๋ฅผ ๋ฐ๋ณตํ๋ค. repeat e1์์ ์ด๋ ํ ๋ทฐ ๋ฆด๋ ์ด์
vi๋ฅผ ์ฐพ๋๋ค. ๋ทฐ ๋ฆด๋ ์ด์
vi๋ฅผ vi๋ฅผ ์ ์ํ๊ณ ์๋ ํํ์์ผ๋ก ๋์นํ๋ค. until๋ ์ด์์ ๋ทฐ ๋ฆด๋ ์ด์
์ด e1๋ด์ ์กด์ฌํ์ง ์์ โข ๋ทฐ ๋ฆด๋ ์ด์
์ด ์ํ์ ์ด์ง ์์ ํ ์ด ๋ฃจํ๋ ์ข
๋ฃํ ๊ฒ์ด๋ค.
๋ทฐ์ ๊ฐฑ์ โข faculty ๋ทฐ์ ์๋ก์ด ํํ์ ์ฝ์
ํ์์ค. insert into faculty values (โ30765โ, โGreenโ, โMusicโ); ์ด ์ฝ์
๋ฌธ์ instructor relation ์๋ค์๊ณผ ๊ฐ์ ํํ ์ฝ์
๋ฌธ์ผ๋ก ํํ๋์ด์ผ ํ๋ค. (โ30765โ, โGreenโ, โMusicโ, null)
Some Updates cannot be Translated Uniquely โข create view instructor_info as select ID, name, buildingfrom instructor, departmentwhere instructor.dept_name= department.dept_name; โข insert intoinstructor_info values (โ69987โ, โWhiteโ, โTaylorโ); โข which department, if multiple departments in Taylor? โข what if no department is in Taylor? โข ๋๋ถ๋ถ์ SQL ์์คํ
์์๋ ์ค์ง ๋จ์ํ ๋ทฐ ๊ฐฑ์ ๋ง์ ํ์ฉํ๋ค. ๋ณต์กํ ๋ทฐ์์ ๊ฐฑ์ ์ ๋ณํํ๊ธฐ๊ฐ ์ด๋ ต๊ฑฐ๋ ๋ถ๊ฐ๋ฅํด ํ์ฉ๋์ง ์๋๋ค.
๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด (Integrity Constraints) โข ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ถํ ์๋ ๋ณ๊ฒฝ์ด ๋ฐ์ดํฐ์ ์ผ๊ด์ฑ์ ์์ง ์๋๋ก ๋ณด์ฅํจ์ผ๋ก์จ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฐ๋ฐ์ ์ธ ์์์ผ๋ก๋ถํฐ ๋ณดํธํ๋ค. โข A checking account must have a balance greater than $10,000.00 โข A salary of a bank employee must be at least $4.00 an hour โข A customer must have a (non-null) phone number
๋จ์ผ ๋ฆด๋ ์ด์
์ ๋ํ ๋ฌด๊ฒฐ์ฑ ์ ์ฝ์กฐ๊ฑด โข not null โข primary key โข unique โข check (P), where P is a predicate
Not Null ๊ณผ Unique ์ ์ฝ์กฐ๊ฑด โข not null โข Declare name and budget to be not null name varchar(20) not nullbudget numeric(12,2) not null โข unique ( A1, A2, โฆ, Am) โข ์์ฑ A1, A2, โฆ Am ์ด ํ๋ณดํค๋ฅผ ์์ ๋ํ๋.candidate key. โข ํ๋ณดํค๋ null ๊ฐ์ ๊ฐ์ง ์ ์์ (์ฃผํค์ ๋ค๋ฆ).
check ์ โข check (P) ์ฌ๊ธฐ์ P๋ ์ ์ด์ด๋ค. Example: semester ์์ฑ์ โfallโ, โwinterโ, โspringโ โsummerโ ์ค ํ๋์ ๊ฐ์ ๊ฐ๋๋ค. create table section ( course_id varchar (8), sec_id varchar (8), semester varchar (6), year numeric (4,0), building varchar (15), room_number varchar (7), time slot id varchar (4), primary key (course_id, sec_id, semester, year), check(semester in (โFallโ, โWinterโ, โSpringโ, โSummerโ)));
์ฐธ์กฐ ๋ฌด๊ฒฐ์ฑ(Referential Integrity) โข ์ฃผ์ด์ง ์ ํธ๋ฆฌ๋ทฐํธ์ ์งํฉ์ ๋ํด ํ ๋ฆด๋ ์ด์
์ ๋ํ๋๋ ๊ฐ์ด ๋ค๋ฅธ ๋ฆด๋ ์ด์
์ ์ด๋ค ์ ํธ๋ฆฌ๋ทฐ์ ์งํฉ์๋ ๋ํ ๋ํ๋๋๋ก ํ๋ค. โข Example: ๋ง์ฝ โBiologyโ ๊ฐ instructor๋ฆด๋ ์ด์
ํํ์ ๋ํ๋๋ ํ๊ณผ๋ช
(department name)์ด๋ผ๋ฉด, department ๋ฆด๋ ์ด์
์ โBiologyโ๋ฅผ ์ํ ํํ์ด ์กด์ฌํ์ฌ์ผ ํ๋ค. โข ํ์์ ์ ์ - r1(R1)๊ณผ r2(R2)๋ฅผ ๊ฐ๊ฐ ์ฃผํค K1๊ณผ K2๋ฅผ ๊ฐ์ง ๋ฆด๋ ์ด์
์ด๋ผ ํ์. - r2๋ด ๊ฐ๊ฐ์ t2์ ๋ํด t1[K1] = t2[๏ก]์ธ r1๋ด์ ํํ t1์ด ์กด์ฌํด์ผ ํ๋ค๋ฉด, R2์ ๋ถ๋ถ ์งํฉ ๏ก๋ ๋ฆด๋ ์ด์
r1์ K1์ ์ฐธ์กฐํ๋ ์ธ๋ ํค์ด๋ค. - ์ฐธ์กฐ ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด: ๏๏ก(r2) ๏ ๏K1(r1)
Cascading Actions in Referential Integrity โข create table course ( course_id char(5) primary key,title varchar(20), dept_name varchar(20) references department) โข create table course ( โฆdept_name varchar(20),foreign key (dept_name) references departmenton delete cascade on update cascade, . . . ) โข alternative actions to cascade: set null, set default
Built-in Data Types in SQL โข date: Dates, containing a (4 digit) year, month and date โข Example: date โ2005-7-27โ โข time: Time of day, in hours, minutes and seconds. โข Example: time โ09:00:30โ time โ09:00:30.75โ โข timestamp: date plus time of day โข Example: timestamp โ2005-7-27 09:00:30.75โ โข interval: period of time โข Example: interval โ1โ day โข Subtracting a date/time/timestamp value from another gives an interval value โข Interval values can be added to date/time/timestamp values
์ธ๋ฑ์ค ์์ฑ โข create table student (ID varchar (5),name varchar (20) not null,dept_name varchar (20),tot_cred numeric (3,0) default 0,primary key (ID)) โข create index studentID_index on student(ID) โข ์ธ๋ฑ์ค๋ ๋ ์ฝ๋ ๊ฒ์์ ๊ณ ์์ผ๋ก ์ํํ๊ธฐ ์ํ์ฌ ์์ฑํ๋ค. โข e.g. select * from studentwhere ID = โ12345โ can be executed by using the index to find the required record, without looking at all records of student More on indices in Chapter 11
End of Chapter 4
Figure 4.01
Figure 4.02
Figure 4.03
Figure 4.04
Figure 4.05
Taylor Figure 4.07
Figure 4.06
Figure 4.03