Previous chapter
Join Expressions
Joined Relations
๋ Relation์ ๊ฐ์ง๊ณ ๋๋ค๋ฅธ Relation์ ์ฐฝ์ถํ๋ ์คํผ๋ ์ดํฐ
โข
2๊ฐ ์ด์์ ํ
์ด๋ธ๋ก๋ถํฐ rows๋ฅผ ์กฐํฉํ๋ค.
โข
Natural Join
โข
Inner join
โข
Outer Join
Natural Join in SQL
select name, course_id
from students, takes
where student.ID = takes.ID;
SQL
๋ณต์ฌ
๋ค์ ์ฟผ๋ฆฌ์ ๊ฐ๋ค.
select name, course_id
from student natural join takes
SQL
๋ณต์ฌ
์ผ๋ฐํํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
select A1, A2, An
from r1 natural join r2 ... natural join rn
where P;
SQL
๋ณต์ฌ
attribute์ name์ ์ฒดํฌํ์ฌ ๋์ผํ ํญ๋ชฉ์ด ์๋์ง ์ฒดํฌํ๋ค.
name์ ์ฒดํฌ
Danger in Natural Join
๋์ผํ attribute name์ ๋ชจ๋ ์ฒดํฌํ๊ธฐ ๋๋ฌธ์ ํน์ ์กฐ๊ฑด๋ง ์ฒดํฌํ๊ณ ์ถ์ ๊ฒฝ์ฐ ์๋ํ์ง ์์ ์ ๋ณด๊ฐ ์ถ๋ ฅ๋ ์ ์๋ค.
select name, title
from student natural join takes natural join course;
SQL
๋ณต์ฌ
student์ takes๊ฐ natural join์ ์งํํ relation์ course๋ฅผ natural join์ ์งํํ๋ฉด course_id์ dept_name ๋ชจ๋ ์ฒดํฌ๊ฐ ๋๋ค.
๋ชจ๋ ๊ณตํต๋ attribute๊ฐ ์ผ์นํ๋๊ฒ ์๋์ง ์ฒดํฌํ๊ธฐ ๋๋ฌธ์ course_id๋ง ๊ฐ์๊ฒ ์๋์ง ์ฒดํฌํ๊ณ ์ถ๋ค๋ฉด ์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์ง์ผํ๋ค.
select name, title
from student natural join takes, course
where takes.course_id = course.course_id;
SQL
๋ณต์ฌ
Natural Join with Using Clause
using ํค์๋๋ฅผ ์ฌ์ฉํด์ ํน์ attribute๋ฅผ ์ง์ ํด์ค ์ ์๋ค. (using์ด ์๋ค๋ฉด default option)
select name, title
from (student natural join takes) natural join course using (course_id)
SQL
๋ณต์ฌ
Join Conditions
โข
on : Inner Join
select *
from student join takes on (student_ID = takes_ID)
SQL
๋ณต์ฌ
on ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
select *
from student, takes
where student_ID = takes_ID;
SQL
๋ณต์ฌ
inner Join์ ์กฐ๊ฑด์ ํด๋นํ์ง ์์ผ๋ฉด ๋ฒ๋ ค์ง๋ค.
4์ e๊ฐ t2์ ์๊ธฐ ๋๋ฌธ์ ๋ฒ๋ ค์ง
Outer Join
๋ณด์กดํ๊ณ ์ถ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด์กดํ๋ ํํ.
๋น ๋ฐ์ดํฐ๋ null๊ฐ์ ๊ธฐ๋ณธ์ผ๋ก ์ด๋ค.
Left Outer Join
Right Outer Join
Full Outer Join
Join Types and Conditions
Join Condition
โข
๋ relations์์ ์ด๋ค tuples๊ฐ ๋งค์นญ๋๊ฒ ํ ์ง ์ ์
Join types
โข
ordering Issues??? ๋ค์์๊ฐ์ ๊ณ์โฆ..
View
View Definitions and Use
instructor์ ์ ๋ณด๋๋ ํ์ํ์ง๋ง ๊ทธ ์ค salary ์ ๋ณด๋ ํ์ํ์ง ์์ ์ฌ๋์ ์๊ฐํด๋ณด์.
salary ์์ด instructor์ View๋ฅผ ์์ฑ
create view faculty as
select ID, name, dept_name
from instructor
SQL
๋ณต์ฌ
Biology Department์ instructor์ ๋ชจ๋ ์ ํ
select name
from faculty
where dept_name = 'Biologyโ
SQL
๋ณต์ฌ
Department Salary Total์ View๋ฅผ ์์ฑ
create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;
SQL
๋ณต์ฌ
Using Other Views
v2๋ผ๋ View๋ฅผ ๋ง๋ค๊ณ v2๋ก๋ถํฐ ์๋ก์ด View v1์ ๋ฝ์๋ด๊ณ ์ถ๋ค๊ณ ํ ๋, v1์ v2์ ์ง์ ์ ์ผ๋ก ์์กด๋๋ค(depend directly)๊ณ ํ๋ค.
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โ;
SQL
๋ณต์ฌ
create view physics_fall_2017_watson as
select course_id, room_number
from physics_fall_2017
where building= 'Watson';
SQL
๋ณต์ฌ
physics_fall_2017_watson์ physics_fall_2017์ depend directlyํ๋ค.
์๊น view๋ ์ผ์ข
์ ์๋ธ์ฟผ๋ฆฌ โ ๋ณ์ํ์ ๊ฐ๊น๋ค๊ณ ํ๋๋ฐ ์ ์ฝ๋๋ฅผ ์๋์ ๊ฐ์ด ๋ฐ๊ฟ ์ ์์ ๊ฒ์ด๋ค.
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';
SQL
๋ณต์ฌ
๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ๋ณํ๋ ์ ์์ ๊ฒ์ด๋ค.
repeat
e(1)์์ relation v(i)๋ฅผ ์ฐพ์๋ผ
v(i)๋ฅผ ์ ์ํ๋ ๊ฒ์ผ๋ก v(i)๋ฅผ ๋์ฒดํด๋ผ
until e1์ View๊ฐ ๋์ด์ ์์๋๊น์ง
SQL
๋ณต์ฌ
Materialized Views
์์์ ์ผ๋ก View๋ฅผ ์ ์ฅํ๊ฒ ๋ค! View๋ค์ด ๋ง์ด ์ฐ์ผ ๊ฒ์ด๊ณ , select๋ฌธ์ด ์ค๋ฒํค๋๊ฐ ํฌ๋ค๊ณ ํ๋จํ๋ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ๋ ์คํ ๋ฆฌ์ง์ ์ ์ฅ๋๋ ๊ฒฝ์ฐ๋ ์์.
๋จ, Materialized Views๋ ๋ณธ์ฒด Relation์ด ์
๋ฐ์ดํธ๊ฐ ๋๋ฉด ์ ๋ณด๊ฐ ์
๋ฐ์ดํธ๊ฐ ์๋์ผ๋ก ๋์ง ์๊ธฐ ๋๋ฌธ์, ๋ณธ์ฒด Relation์ด ์
๋ฐ์ดํธ ๋ ๋๋ง๋ค View๋ฅผ ์
๋ฐ์ดํธํด์ ์ ์ง๋ณด์๋ฅผ ํด์ค์ผ ํ๋ค. ์ด์ Update์ ๋ฐฉ๋ฒ์ ์.
Update of a View
instructor๋ก๋ถํฐ ๋น๋กฏ๋ faculty View์ ์๋ก์ด tuple์ ๋ฃ๋๋ค๊ณ ๊ฐ์ ํ์.
insert into faculty
values('30765'm 'Green', 'Mist');
SQL
๋ณต์ฌ
๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ ๊ฒ์ด๋ค.
Some Updates Cannot be Translated Uniquely
create view instructor_info as
select ID, name, building
from instructor, department
where instructor.dept_name= department.dept_name;
SQL
๋ณต์ฌ
insert into instructor_info
values ('69987', 'White', 'Taylor');
SQL
๋ณต์ฌ
์์์ ๋ณด๋ฉด instructor์ department์ dept_name์ด ๊ฐ์ ๊ฒ์ ํ์ธํ๊ณ View๋ฅผ ์งฐ๋ค.
dept_name
And Some Not at All
View Updates in SQL
๋ฐ๋ผ์ SQL์์ View ์
๋ฐ์ดํธ๋ ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์๋ง ํ๋ฝํ๋๋ก ํ๋ค.
๊ทธ๋ฅ View๋ ์ฝ๋ ์ฉ๋๋ก๋ง ์ฐ์.
Transaction
์ฌ๋ฌ๊ฐ์ ์ฐ์ฐ์ ํ๋์ ์ฐ์ฐ์ผ๋ก ์ทจ๊ธํ๋ ์น๊ตฌ
begin; op1; op2; commit;
SQL
๋ณต์ฌ
์ด๋ ๋ ์คํผ๋ ์ด์
์ด ๋ชจ๋ ์ ์คํ๋๋๊ฐ, ๋ ๋ชจ๋ reject๋๊ณ ๋๋๋๊ฐ๋ฅผ ๋ณด์ฅํ๋ค.
๋ฐ๋ผ์ Transaction์ ๋ค์๊ณผ ๊ฐ์ ์ํ์ค ํ๋๋ก ๋ชจ๋ ๋๋์ผ ํ๋ค.
๊ฒฐ๊ตญ Transaction์ Atomicํ์ฌ์ผ ํ๋ค.
โข
๋ชจ๋ ์๋ฃ๋๊ฑฐ๋ ๋กค๋ฐฑํ์ฌ ์ ๋๋ก ์ผ์ด๋์ง ์์ ๊ฒ ์ฒ๋ผ ํด์ผ ํ๋ค.
Transactions์ Isolation
Integrity Constraint
Constraints on a Single Relation
โข
not null
โข
primary key
โข
unique
โข
check(P)
Not Null Constraints
name varchar(20) not null
budget numeric(12,2) not null
SQL
๋ณต์ฌ
Unique Constraints
The Check Clause
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')))
SQL
๋ณต์ฌ
Referential Integrity
????????????
Cascading Actions in Referential Integrity
create table course (
(โฆ
dept_name varchar(20),
foreign key (dept_name) references department
on delete cascade
on update cascade,
. . .)
SQL
๋ณต์ฌ
cascade๋์ set null์ด๋ set default ๊ฐ์ผ๋ก ์ค์ ๋ ์๋ ์๋ค.
Integrity Constraint Violation During Transactions
create table person (
ID char(10),
name char(40),
mother char(10),
father char(10),
primary key ID,
foreign key father references person,
foreign key mother references person)
SQL
๋ณต์ฌ
constraint violation์์ด tuple insert๋ฅผ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ๋๊ฐ?
โข
father, mother์ null๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ํ๊ณ ๋ชจ๋ ์ฌ๋์ ๋ฃ์ ๋ค ์
๋ฐ์ดํธ ํ๋ฉด ๋๋ค.
Complex Check Conditions
Assertions
SQL Data Types and Schemas
Built-in Data Types in SQL
โข
date : โyyyy-mm-ddโ
โข
time : โHH:MM:SSโ
โข
timestamp : date + time
โข
interval ์๊ฐ์ ๊ฐ๊ฒฉ โ1โ day
Large-Object Types
ํฐ ํ์ผ์ large-object๋ก ๊ตฌ์ฑํ๋ค.
ํ์ผ์ ํฌ๊ธฐ๊ฐ ๋๋ฌด ํฌ๋ค๋ฉด ์คํ ๋ฆฌ์ง์ ๋ฐ๋ก ์ ์ฅํด๋๊ณ ์์น๋ฅผ ์ง์ ํด์ฃผ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
User-Defined Types
create type Dollars as numeric (12,2) final
SQL
๋ณต์ฌ
create table department
(dept_name varchar(20),
building varchar (15),
budget Dollars);
SQL
๋ณต์ฌ
Domains
create domain person_name char(20) not null
SQL
๋ณต์ฌ
example:
create domain degree_level varchar(10)
constraint degree_level_test
check (value in ('Bachelors', 'Masters', 'Doctorate'));
SQL
๋ณต์ฌ
Index Creation
Authorization
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ผ๋ถ์ ๋ํ ์ฌ๋ฌ ํํ์ ๊ถํ์ ์ฌ์ฉ์์๊ฒ ํ ๋นํ ์ ์๋ค.
โข
Read - ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ๊ถํ (reading ๊ฐ๋ฅ, modification์ ๋ถ๊ฐ๋ฅ)
โข
Insert - ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ ๊ถํ (Insert ๊ฐ๋ฅ, modification์ ๋ถ๊ฐ๋ฅ)
โข
Update - ๋ฐ์ดํฐ๋ฅผ ๊ฐฑ์ ํ ๊ถํ (modification ๊ฐ๋ฅ, delete๋ ๋ถ๊ฐ๋ฅ)
โข
Delete - ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ ๊ถํ.
์ด๋ฐ ์ ํ์ ๊ถํ์ ํน๊ถ(privilege)๋ผ ๋ถ๋ฅธ๋ค.
๋ฆด๋ ์ด์
์ด๋ ๋ทฐ์ ๊ฐ์ DB ์ผ๋ถ์ ์ด๋ฐ ์ ํ์ privilege๋ฅผ
์ ์ฒด ์ฌ์ฉ์์๊ฒ ๋ถ์ฌํ๊ฑฐ๋
๋ถ์ฌํ์ง ์๊ฑฐ๋(๊ธ์งํ๊ฑฐ๋)
์ผ๋ถ๋ง ๋ถ์ฌํ ์ ์๋ค.
๋ฐ์ดํฐ์ ๋ํ ๊ถํ๊ณผ ํจ๊ป, ๋ฆด๋ ์ด์
์ ์์ฑ, ์์ , ์ญ์ ์ ๊ฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง ๋ณ๊ฒฝ ๊ถํ๋ ์ฌ์ฉ์์๊ฒ ๋ถ์ฌํ ์ ์๋ค. ์ผ๋ถ ํํ์ ๊ถํ์ ์์ ํ ์ฌ์ฉ์๋ ์์ ์ ๊ถํ์ ํ ์ฌ์ฉ์์๊ฒ ์ ๋ฌํ๊ฑฐ๋ ์ด์ ์ ๋ถ์ฌ๋ ๊ถํ์ ์ฒ ํํ ์ ์๋ค.
โข
Index - ์ธ๋ฑ์ค์ ์์ฑ๊ณผ ์ญ์
โข
Resources - ์ Relation์ ์์ฑ
โข
Alteration - Relation์ ์ Attribute์ ๋ํ ์ถ๊ฐ ํน์ ์ญ์
โข
Drop - Relation์ ์ญ์
Authorization Specification in SQL
grant๋ฌธ์ ๊ถํ์ ๋ถ์ฌํ๋ ์ฉ๋๋ก ์ฌ์ฉ๋๋ค.
grant <privilege list> on <relation or view> to <user list>
SQL
๋ณต์ฌ
<user list> ์ ๋ค์ด์ฌ ์ ์๋ ํ๋ผ๋ฏธํฐ
โข
user-id
โข
public : ๋ชจ๋ ์ ์ ์๊ฒ ํน๊ถ์ ๋ถ์ฌํ๋ค.
โข
A role : ์ญํ ?
Privileges in SQL
โข
select : Relation์ ์ ๊ทผ/์ฝ๊ธฐ ํน์ view๋ฅผ ์ด์ฉํด ์ง์ํ ์ ์๋ค.
grant select on <relation> to U1, U2, U3...
SQL
๋ณต์ฌ
โข
insert : tuple์ ์ง์ด๋ฃ๋ ๋ฅ๋ ฅ
โข
Update : SQL update statement๋ฅผ ์ด์ฉํด ์
๋ฐ์ดํธํ๋ ๋ฅ๋ ฅ
โข
delete : ํํ์ ์ญ์ ํ๋ ๋ฅ๋ ฅ
Revoking Authorization in SQL
revoke๋ฌธ์ ํน๊ถ์ ํ์งํ๋ ์ง์๋ฌธ์ด๋ค.
revoke <privilege-list> on <relation or view> from <user list>
SQL
๋ณต์ฌ
โข
<privilege-list>
โฆ
๋ชจ๋ ๊ถํ์ ๋ฐํํ๋ ค๋ฉด all ํค์๋๋ฅผ ์ธ ์ ์๋ค.
โฆ
public?
โข
๊ฐ์ ํน๊ถ์ด ๋ค๋ฅธ ์ ์ ์๊ฒ ๋๋ฒ ํ ๋น๋์์ ๊ฒฝ์ฐ, ๋ฐํ ํ์๋ ํน๊ถ์ด ์ ์ง๋ ์ ์๋ค.
Roles
์ญํ (Role)์ ๋ค์ํ ์ ์ ๋ก๋ถํฐ ๊ฐ ์ ์ ๊ฐ DB์ ์ ๊ทผ, ๊ฐฑ์ ํ ์ ์๋ ๋ฒ์๋ฅผ ๊ตฌ๋ณํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ด๋ค.
์ญํ ์์ฑ
create role <name>
SQL
๋ณต์ฌ
role์ด ์์ฑ๋ ํ์ ๋ค์๊ณผ ๊ฐ์ด ์ ์ ์๊ฒ role์ ๋ถ์ฌํ ์ ์๋ค.
grant <role>to <users>
SQL
๋ณต์ฌ
A โ B ์ํ์ ๋กค์์ A๋ฅผ ์ญ์ ํ์๋ B๋ผ๋ ๋กค์ด ํจ์ฉ์ด ์์ํ
๋ฐ B๋ ์๋์ ์ผ๋ก ์ญ์ ๋๋๊ฐ?
ํ๋ก์ธ์ค๋ก ๋ฐ์ง๋ฉด
Next chapter
