๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ5: ๐’ฆ๐‘œ๐“‡๐‘’๐’ถ ๐’ฐ๐“ƒ๐’พ๐“‹/Database(COSE371)

[๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค] CH4. Intermediate SQL

Join Expressions

  • ๋‘ ๊ฐœ์˜ relation์„ join ํ•ด์„œ ์ƒˆ๋กœ์šด relation์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ
  • Cartesian product!!
  • ๋ณดํ†ต from clause์—์„œ ์‚ฌ์šฉ๋จ

Join conditions

Using Clause

-> attribution์„ ์ž˜๋ชป ํ•ฉ์น˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ์”€!

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

์ด๋ ‡๊ฒŒ join ์–ด์ฉŒ๊ตฌ using (์ €์ฉŒ๊ตฌ) ๋ผ๊ณ  ๋ช…์‹œํ•ด์„œ ๊ทธ๋ƒฅ ๊ฒน์น˜๋Š” ๊ฑธ ๋‹ค ํ•ฉ์น˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŠน์ • attribute๋ฅผ ๊ธฐ์ค€์œผ๋กœ join ํ•˜๊ฒ ๋‹ค๊ณ  ํ•ด์ฃผ๋Š” ๊ฒƒ!
์ด ๊ฒฝ์šฐ Duplication columns์€ ์ œ๊ฑฐ๋œ๋‹ค.

Join Condition (on)

์ด predicate๋Š” where์ ˆ ์ฒ˜๋Ÿผ ์“ด๋‹ค.
์š” ์•„์ด๋Š” attribute ์ด๋ฆ„์ด ๊ฐ™์„ ํ•„์š” ์—†์ด ๊ฐ๊ฐ ์‚ฌ์šฉ์ž ์ง€์ •ํ•ด์„œ ๊ฐ’์ด ๊ฐ™์€ ๊ฒƒ๋ผ๋ฆฌ join ํ•˜๋Š” ๊ฒƒ.

select *
from student join takes on student_ID = takes_ID

์ด ๊ฒฝ์šฐ student_ID ์™€ takes_ID๋ฅผ ๋น„๊ตํ•ด์„œ ๊ฐ™์€ ๊ฐ’๋ผ๋ฆฌ ํ•ฉ์นœ๋‹ค.
์„œ๋กœ ๊ฐ™์€ attribute๋ฅผ ํ•ฉ์น˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ฉ์น  ๋•Œ ํ•œ์ชฝ columns์„ ์‚ญ์ œํ•˜์ง€ ์•Š๊ณ  ๋‘ ๊ฐœ ๋‹ค ๋ณด์กดํ•œ๋‹ค.
(Duplicate columns are NOT removed)

์œ„ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜๋ฏธ์ด๋‹ค.

select *
from student, takes
where student_ID = takes_ID

Natural join

  • ๋ชจ๋“  ๊ณตํ†ต๋œ attribute๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ฉ์น˜๊ณ  ์ค‘๋ณต๋˜๋Š” ํŠœํ”Œ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์œ ์ง€ํ•จ
select name, course_id
from students, takes,
where student.ID = takes.ID;

์œ„ ์ฟผ๋ฆฌ๋ฅผ natural join์„ ์‚ฌ์šฉํ•˜๋ฉด

select name, course_id
from student natural join takes;

์ด๋ ‡๊ฒŒ ํ‘œํ˜„ ๊ฐ€๋Šฅ!
์ด ๋•Œ ๊ฒน์น˜๋Š” ID ๋ผ๋Š” attribute ๋Š” ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๋‚จ๋Š”๋‹ค.

์—ฌ๋Ÿฌ relation์„ join ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

select A1, A2,...,An
from r1 natural join r2 natural join ... natural join rn
where P;

์กฐ์‹ฌํ•  ๊ฒƒ! ์ฝค๋งˆ ์•ˆ์”€!
๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ์“ฐ๋ ค๋ฉด ๋ชจ~๋“  n๊ฐœ์˜ relation์—์„œ attribution์ด ๊ฒน์น˜๋Š”๊ฒŒ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

Dangerous in Natural Join

  • ์ด๋ฆ„๋งŒ ์„œ๋กœ ๊ฐ™๊ณ  ์„œ๋กœ ๊ด€๋ จ ์—†๋Š” unrelated attributes๋ฅผ ์ž˜๋ชป ์ธ์‹ํ•ด์„œ ํ•ฉ์ณ๋ฒ„๋ฆฌ์ง€ ์•Š๊ฒŒ ์กฐ์‹ฌ!
    ์˜ˆ์‹œ

Correct version

select name, title
from student natural join takes, course
where student.course_id = course.course_id;

Incorrect version

select name, title
from student natural join takes natural join course;

takes์— ์žˆ๋Š” course_id์™€ course์— ์žˆ๋Š” course_id๋Š” ๋‹ค๋ฅธ ์•„์ด์ž„!
์ž˜๋ชป๋œ ๋ฒ„์ „์„ ์ด์šฉํ•˜๋ฉด ํ•™์ƒ์ด ์ž์‹ ์˜ ํ•™๊ณผ๊ฐ€ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ํ•™๊ณผ ์ˆ˜์—…์„ ์ˆ˜๊ฐ•ํ•˜๋Š” ๊ฒฝ์šฐ tuple์ด ์‚ญ์ œ๋  ์ˆ˜ ์žˆ๋‹ค.

Join Types

1. Inner join

  • Join ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ ํ•œ ์ชฝ์—๋Š” ์—†๋Š” ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ๋‚ ๋ ค๋ฒ„๋ฆฐ๋‹ค.

2. Outer join

  • loss of information์„ ์ตœ๋Œ€ํ•œ ํ”ผํ•˜๊ณ ์ž ๋งŒ๋“  extension of the join operation
  • match๋˜์ง€ ์•Š๋Š” tuple์„ ๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ join ๊ฒฐ๊ณผ์— ๋„ฃ์–ด์ค€๋‹ค. (๋นˆ ๊ณณ์€ null ๊ฐ’์„ ๋„ฃ์–ด์คŒ)
  1. left outer join
    course natural left outer join prereq
    ๋ผ๊ณ  ํ•˜๋ฉด course relation์—๋งŒ ์žˆ๋Š” ์•„์ด(์ฝ”๋“œ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์— ์ ํžŒ ๋ฆด๋ ˆ์ด์…˜)์€ ์‚ด์•„๋‚จ๊ณ , prereq์—๋งŒ ์žˆ๋Š” ์•„์ด๋Š” ๋ฒ„๋ ค์ง„๋‹ค.
    ์ด ๋•Œ ๋‘˜ ์ค‘ ํ•˜๋‚˜์—๋งŒ ์žˆ์œผ๋ฏ€๋กœ ํ•ฉ์น  ๋•Œ ๊ฐ’์ด ์—†๋Š” attribute (blank field) ๊ฐ€ ์ƒ๊ธฐ๋Š”๋ฐ ์ด ์ž๋ฆฌ์—” null๋กœ ์ฑ„์›Œ์ค€๋‹ค.

    course โŸ• prereq

  2. right outer join
    course natural right outer join prereq
    ๋ผ๊ณ  ํ•˜๋ฉด prereq์— ์žˆ๋Š”, ์ฆ‰ ์ฝ”๋“œ์—์„œ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์• ๋งŒ ์‚ด๊ณ  ์™ผ์ชฝ์— ์žˆ๋Š” course๋Š” ๋ฒ„๋ ค์ง„๋‹ค.

course โŸ– prereq

  1. full outer join
    course natural full outer join prereq
    ์–˜๋Š” ์ด์ œ ์–‘ ์ชฝ ๋‹ค ์‚ด์•„ ๋‚จ์Œ!!

Views

์‹ค์ œ relation์€ ์•„๋‹ˆ์ง€๋งŒ ์‹ค์ œ relation ์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ž๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ virtual relation์„ ์ œ๊ณตํ•˜๋Š”๋ฐ ์ด๊ฒƒ์„ view๋ผ๊ณ  ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ view ๋ฅผ ๋งŒ๋“ ๋‹ค.

create view v as <query expression>;

์ด๋Š” ์‹ค์ œ relation์ด ์•„๋‹ˆ๋ผ ๋ณด๊ธฐ ์œ„ํ•œ ์ž„์‹œ relation !
view๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋ฉด ์šฐ๋ฆฌ๊ฐ€ ๋ถ™์—ฌ์ค€ ์ด๋ฆ„์„ ์ด์šฉํ•ด์„œ ์ด virtual relation์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
view๋Š” ์‹ค์ œ relation์ด ์•„๋‹ˆ๋ผ ๊ทธ์ € query ๋ฌธ ๋ฉ์–ด๋ฆฌ๋ฅผ ์ €์žฅํ–ˆ๋‹ค๊ณ  ๋ด์•ผ ํ•œ๋‹ค.
(relation์„ ๋ณต์‚ฌํ•ด์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฑฐ๋ผ๋ฉด ์›๋ž˜ ๊ธฐ์กด relation์—์„œ ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ƒ๊ธฐ๋ฉด ๋ชจ๋“  ํŒŒ์ƒ relation์„ ๋‹ค ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.
๊ตณ์ด ์ด๋ ‡๊ฒŒ ์•ˆํ•˜๊ณ  query๋ฌธ๋งŒ ์ €์žฅํ•ด๋‘๊ณ  ๊ณ„์† ์“ฐ๋Š” ๊ฒƒ)

์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด์ž

 create view faculty as
     select ID, name, dept_name
    from instructor;

์ด๋ ‡๊ฒŒ view๋ฅผ ๋งŒ๋“ค์–ด ๋’€๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค.

select name
from faculty
where dept_name = 'Biology';

์ด๋ ‡๊ฒŒ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค.

create view departments_total_salary(dept_name, total_salary) as
    select dept_name, sum (salary)
    from instructor
    group by dept_name;

๋˜, view๋ฅผ ๋งŒ๋“ค ๋•Œ ๋‹ค๋ฅธ view๋ฅผ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๋‹ค.

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';
create view physics_fall_2017_watson as
    select course_id, room_number
    from physics_fall_2017
    where building = 'Watson';

Materialized View

  • ํŠน์ • database system์—์„œ๋Š” view relation์„ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ์ €์žฅํ•˜๊ธฐ๋„ ํ•œ๋‹ค.
    • view ๋งŒ๋“ค ๋•Œ physical copy๋ณธ์ด ์ƒ์„ฑ๋จ
      ์ด๋Ÿฌํ•œ view๋ฅผ Materialized view๋ผ๊ณ  ํ•จ
  • ๋งŒ์•ฝ ์›๋ž˜ relation์ด update ๋˜๋ฉด, materialized view result๋Š” ์˜ˆ์ „ ๋ฒ„์ „์— ๋จธ๋ฌด๋ฅด๊ฒŒ ๋จ
    ๋”ฐ๋ผ์„œ ์›๋ž˜ relation์ด update ๋  ๋•Œ ๋งˆ๋‹ค view๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์„œ ์œ ์ง€ํ•ด์•ผ ํ•œ๋‹ค.

Update of a view

์šฐ๋ฆฌ๊ฐ€ ์•„๊นŒ ๋งŒ๋“  faculty๋ผ๋Š” view์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž

insert into faculty
    values ('30765' , 'Green', 'Music');

์ด view๋Š” instructor relation์—์„œ ๋ถ€ํ„ฐ ์™”์œผ๋‚˜, salary attribution์€ ์—†๋Š” relation์ž„
์ด ๋•Œ ์šฐ๋ฆฌ๋Š” ๋‘ ๊ฐ€์ง€ ์„ ํƒ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. Reject the insert
  2. ๋นˆ ๋ถ€๋ถ„์€ null๋กœ ์ฑ„์›Œ์„œ ์›๋ž˜์˜ relation์—๋„ ์ถ”๊ฐ€

๊ทธ๋‚˜๋งˆ ์ด๊ฑด ์–‘๋ฐ˜์ด๋‹ค...

create view instructor_info as
    select ID, name, building
    from instructor, department
    where instructor.dept_name = department.dept_name;

์ด ๊ฒฝ์šฐ insert๋ฅผ ํ•˜๊ณ  ์‹ถ์–ด์„œ ๋ณด๋ฉด ์ผ๋‹จ ๊ฐ€์ ธ์˜จ relation์ด ๋‘ ๊ฐœ๊ณ 
ํ…Œ์ผ๋Ÿฌ์— ์—ฌ๋Ÿฌ department๊ฐ€ ์žˆ๋‹ค๋ฉด ์–ด๋Š department์ธ์ง€, ํ…Œ์ผ๋Ÿฌ์— department๊ฐ€ ์—†๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•  ๊ฑด์ง€ ๊ฒฐ์ •ํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค.

create view history_instructors as
    select *
    from instructor
    where dept_name = 'History';

์—ฌ๊ธฐ์—
insert('25566', 'Brown', 'Biology', 100000)

๋ฅผ ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋ฉด??
์• ์ดˆ์— history ๊ณผ๋ชฉ์ธ๋ฐ biology๋ฅผ ๋„ฃ์œผ๋ฉด ์•ˆ๋œ๋‹ค. ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธใ…‡ ใ…” ๋Œ€๋ถ€๋ถ„์˜ SQL implementation์—์„œ๋Š” ๋‹จ์ˆœํ•œ simple view ์—์„œ๋งŒ update๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค.

  1. from์ ˆ์— ์˜ค์ง ํ•˜๋‚˜์˜ relation์ด ์™”์„ ๋•Œ
  2. select ์ ˆ์—๋Š” ์˜ค์ง attribute ์ด๋ฆ„๋งŒ ์˜ค๊ณ , ์–ด๋–ค expression, aggregates, distinct specification ๋“ฑ์ด ์™€์„  ์•ˆ๋œ๋‹ค.
  3. select ์ ˆ์— ์—†๋Š” attribute๋Š” null๋กœ ์ฑ„์šด๋‹ค.
  4. group by๋‚˜ having clause๋„ ์—†์–ด์•ผ ํ•œ๋‹ค.

์ฆ‰ ๊ฑฐ์˜ view update๋Š” ์•ˆํ•œ๋‹ค.

Transactions

  • "unit" of work. ์ผ๋ จ์˜ query ๋˜๋Š” update statements๋กœ ๊ตฌ์„ฑ๋จ

  • standard SQL์—์„œ๋Š” SQL statement ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ ์•”์‹œ์ ์œผ๋กœ transaction์„ ์‹œ์ž‘ํ•œ๋‹ค.

  • Commit work : transaction์ด ์‹œ์ž‘๋˜๊ณ  ๋‚˜์„œ ์ƒ๊ธด ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ €์žฅํ•œ๋‹ค.

  • Rollback work : ๋ชจ๋“  update์‚ฌํ•ญ์„ roll back(undone)

  • Atomic transaction : ์™„์ „ ์‹คํ–‰๋˜๊ฑฐ๋‚˜ ์™„์ „ ์—†์—ˆ๋˜ ์ผ์ฒ˜๋Ÿผ rolled back๋œ๋‹ค.(???)

  • Isolation from concurrent transactions (???)

begin transaction;
...
commit; or rollback;

Integrity Constraints

  • Integrity constraints๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ authorized change๋กœ ์ธํ•ด data consistency๊ฐ€ ์†์ƒ๋˜์ง€ ์•Š๋„๋ก ํ•จ์œผ๋กœ์จ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์˜ ์šฐ๋ฐœ์ ์ธ ์†์ƒ์„ ๋ง‰๋Š”๋‹ค.

1. not null

```
name varchar(20) not null
budget numeric(12,2) not null
```

2. primary key

3. unique

  • uniqe specification๋Š” Attributes A1, A2 ,..., Am์ด candidate key๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค๊ณ  ๋ช…์‹œ๋˜์–ด์žˆ๋‹ค.
  • candiate keys๋Š” primary key ์™€ ๋‹ค๋ฅด๊ฒŒ null๋„ ํ—ˆ์šฉ๋จ

4. check (P) (P is a predicate)

relaton ์˜ ๋ชจ๋“  tuple์ด ์ฃผ์–ด์ง„ predicate P๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•จ.

create table section
    (course_id varchar(8),
    sec_id varchar(8),
    ...
    primary key (course_id, sec_id, semester, year),
    check (semester in ('Fall', 'Winter', 'Spring', 'Summer')));

semester๋Š” fall, winter, spring, summer ์ค‘์— ๊ฐ’์ด ์žˆ์–ด์•ผ ํ•จ!

Complex Check Conditions

check (time_slot_id in (select time_slot_id from time_slot))

์ด๋ ‡๊ฒŒ check clause์— ์–ด๋–ค subquery๋ฅผ ๋„ฃ์„์ˆ˜๋„ ์žˆ๋‹ค.
์ด ๊ฒฝ์šฐ ๋‹จ์ˆœํžˆ ํŠœํ”Œ์„ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ๋•Œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ time_slot์ด ๋ณ€๊ฒฝ๋  ๋•Œ์—๋„ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

5. Referential Integrity

  • foreign key : ๋‹ค๋ฅธ relatioon์˜ primary ํ‚ค๋ฅผ reference ํ•ด ์˜ดใ…

foreign key๋Š” SQL create table์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

foreign key (dept_neme) references department

๊ธฐ๋ณธ ์ ์œผ๋กœ foreign key๋Š” ๋‹ค๋ฅธ referenced table์˜ primary-key attributes๋ฅผ references ํ•œ๋‹ค.
๋˜๋Š” ์ฐธ์กฐ๋œ relation์˜ attributes๋ฅผ ๋ช…์‹œํ•ด ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

foreign key (dept_name) references department(dept_name)

*๊ธฐ๋ณธ์ ์œผ๋กœ referential-integrity๊ฐ€ ์ง€์ผœ์ง€์ง€ ์•Š์œผ๋ฉด action์€ reject๋œ๋‹ค. *

Cascade : ์›๋ž˜ relation ์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธฐ๋ฉด ๊ทธ relation์„ ์ฐธ์กฐํ•ด ์˜จ relation์˜ ์ •๋ณด๋„ ๋ณ€๊ฒฝํ•ด ์ค€๋‹ค.

create table course(
    (dept_name varchar(20),
    foreign key (dept_name) references department
        on delete cascade
        on update cascade
     );

์ด๋Ÿฌ๋ฉด referenced relation์—์„œ delete๋‚˜ update ํ•  ๋•Œ referencing ํ•œ relation ๋„ ๋ฐ”๋€๋‹ค

cascade ๋Œ€์‹  ์ด๋Ÿฐ ๊ฒƒ๋„ ์žˆ๋‹ค

  • set null : ์‚ญ์ œ๋˜๋ฉด null๋กœ set
  • set default : ์‚ญ์ œ๋˜๋ฉด ๋ฏธ๋ฆฌ ์„ค์ •ํ•ด ๋‘” default ๊ฐ’์œผ๋กœ set

Integrity Constraint Violation During Transactions

create table person(
    ID char(10),
    ...
    spouse char(10),
    primary key (ID),
    foreign key (spouse) references person);

์œ„ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ด๋ผ. ์ž๊ธฐ ์ž์‹ ์„ reference ํ•˜๊ณ  ์žˆ๋‹ค.

์ด ๊ฒฝ์šฐ ๊ฒฐํ˜ผํ•œ ์‚ฌ๋žŒ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ!
์ฐธ์กฐํ•  ์‚ฌ๋žŒ์ด ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ ์–‘ ์ชฝ ๋‹ค ๋™์‹œ์— ์ถ”๊ฐ€๊ฐ€ ์•ˆ๋˜๋‹ˆ๊นŒ ์˜์›ํžˆ ๋นˆํ…Œ์ด๋ธ”๋กœ ๋‚จ์•„์žˆ์„ ๊ฒƒ!

์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋ƒ!

  1. ์ผ๋‹จ null๋กœ ๋„ฃ์–ด๋‘๊ณ  ๋‘ ์‚ฌ๋žŒ์„ insert ํ•œ ๋’ค update ํ•ด ์คŒ
  2. ๋˜๋Š”!!!! defer constraint checking
    • initially deferred : ์ปค๋ฐ‹ ์ผ์–ด๋‚˜๊ธฐ ์ „๊นŒ์ง€ constraint ์•ˆ ๋ณด๊ณ  ์ˆ˜ํ–‰ํ•œ๋‹ค.
    • deferrable : transaction์˜ ์ผ๋ถ€๋กœ deferable์„ ์ง€์ •ํ•œ ํ›„ ์ด๋Ÿฐ query ๋“ค์€ ์„ ํƒ์ ์œผ๋กœ ์ฒดํฌ ์•ˆํ•˜๊ณ  ์ˆ˜ํ–‰ํ•œ๋‹ค.

6. Assertion

์ „์ฒด database ์˜ ๊ทœ์น™ ์ถ”๊ฐ€

์ด๋Ÿฐ ๊ทœ์น™๋“ค์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • student relation์— ์žˆ๋Š” ๊ฐ tuple์—์„œ tot_cred attribution ๊ฐ’์€ ํ•™์ƒ์ด ์ˆ˜๊ฐ• ์™„๋ฃŒํ•œ ์ˆ˜์—…์˜ ํ•™์  ํ•ฉ๊ณ„์™€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
  • ๊ต์ˆ˜๋Š” ํ•œ ํ•™๊ธฐ์— ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๊ต์‹ค์—์„œ ๊ฐ™์€ ์‹œ๊ฐ„๋Œ€์— ๊ฐ•์˜ํ•  ์ˆ˜ ์—†๋‹ค.

create assertion <assertion-name> check (<predicate>);

SQL Data Types and Schemas

Built-in Data Types in sql

  • date
  • time
  • timestamp
  • interval

Large-Object Types

  • blob : binary large object
  • clob : character large object
  • pointer๊ฐ€ return ๋จ

User-Defined Type

create type Dollars as numeric (12, 2) final;
์ด๋Ÿฐ ์‹์œผ๋กœ user๊ฐ€ ๋ง˜๋Œ€๋กœ data type์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

(final

create table department
    (dept_name varchar (20),
     building varchar (15),
     budget Dollars);

์š”๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

Domains

type์ด๋ž‘ ๋น„์Šทํ•˜์ง€๋งŒ ๋„๋ฉ”์ธ์€ constraints๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ  type์€ ์•ˆ๋œ๋‹ค.

create domain person_name char(20) not null;

create domain degree_level varchar(10)
    constraint degree_level_test
        check (value in ('Bachelors', 'Masters', 'Doctorate'));

Index Definition in SQL

database system์ด relation์˜ ๋ชจ๋“  tuple์„ ์Šค์บ”ํ•˜์ง€ ์•Š๊ณ ๋„ ํ•ด๋‹น attribute์— ๋Œ€ํ•ด ์ง€์ •๋œ ๊ฐ’์„ ๊ฐ–๋Š” tuple์„ ํšจ์œจ์ ์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ๋” attribute์— index๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด๋‹ค.

create index <name> on <relation-name> (attribute);

create table student
    (ID varchar (5),
    ...
    primary key(ID));


create index studentID_index on student(ID);

์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๊ณ  ์ฟผ๋ฆฌ ๋‚ ๋ฆด ๋•Œ ๊ตณ์ด TABLE ์ „์ฒด๋ฅผ ์ฝ์ง€ ์•Š๊ณ  studentID_index ์•ˆ์—์„œ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

select *
from student
where ID = '12345'

Authorization

์šฐ๋ฆฌ๋Š” user์—๊ฒŒ database์˜ ์ผ๋ถ€ ๋˜๋Š” ์ „์ฒด ๊ถŒํ•œ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

  • read
  • insert
  • update
  • delete

์ด๋Ÿฌํ•œ autorizations์˜ type๋“ค์„ privilege๋ผ๊ณ  ํ•œ๋‹ค.

Grant

Grant <privilege list> on <relation or view> to <user list>;

๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
์ด๋Š” user list์—๊ฒŒ relation ๋˜๋Š” view๋ฅผ privilege list(RIUD) ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ์ค€๋‹ค๋Š” ๊ฒƒ!

user list ๋Š”

  • user id ์ด๊ฑฐ๋‚˜
  • public ์ด๊ฑฐ๋‚˜ (๋ชจ๋“  valid user)
  • role ์ด ๋œ๋‹ค.

ex

grant select on department to Amit, Satoshi ;

  • view ๊ถŒํ•œ ์ค€๋‹ค๊ณ  ํ•ด์„œ ๊ทธ view์˜ underlying relation์— ๋Œ€ํ•œ ๊ถŒํ•œ๊นŒ์ง€ ๋ถ€์—ฌ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹˜.
  • ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š” grantor๋Š” ์ด๋ฏธ ๊ทธ item๋“ค์— ๋Œ€ํ•ด privilege๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•จ. ์•„๋‹ˆ๋ฉด ๋””๋น„ ๊ด€๋ฆฌ์ž์—ฌ์•ผ ํ•จ.

Privileges in SQL

  • select
  • insert
  • update
  • delete
  • all privileges

Revoking Authorization in SQL

๊ถŒํ•œ ํšŒ์ˆ˜

**revoke <privilege list> on <relation or view> from <user list>;

revoke select on student from U1, U2, U3;

  • <previlege list>๋Š” revokee๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ชจ๋“  privileges๋ฅผ revoke ์‹œํ‚จ๋‹ค.
  • <revokee-list> (public ์ด ๋  ์ˆ˜๋„) ์— ํฌํ•จ๋œ ๋ชจ๋“  user๋Š” ๊ถŒํ•œ์„ ๋บ๊ธด๋‹ค.
  • ๋งŒ์•ฝ ๋™์ผํ•œ ๊ถŒํ•œ์ด ๋‹ค๋ฅธ grantees์— ์˜ํ•ด ๋‘ ๋ฒˆ ๋ถ€์—ฌ๋˜์—ˆ์„ ๊ฒฝ์šฐ, ์ด ์œ ์ €๋Š” ๊ถŒํ•œ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ทจ์†Œ๋˜๋Š” ๊ถŒํ•œ์— ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ๋ชจ๋“  ๊ถŒํ•œ๋“ค๋„ ๋‹ค ์ทจ์†Œ๋œ๋‹ค.

Roles

role์€ ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•! ํŠน์ • role ์ •์˜ํ•ด ๋†“๊ณ  ๊ถŒํ•œ ๋ถ€์—ฌํ•  ๋•Œ ๊ทธ role์˜ ๊ถŒํ•œ์„ ๋ฌผ๋ ค์ค€๋‹ค.

create role <name>;

create role instructor;
grant instructor to <users>;

์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

create role instructor;
grant instructor to Amit;

์ด๋ ‡๊ฒŒ amit์— instructor๋ผ๋Š” ๊ถŒํ•œ ์ฃผ๊ณ 

grant select on takes to instructor;

ํ•˜๋ฉด instructor๋ผ๋Š” role์„ ๊ฐ€์ง„ ๋ชจ๋“  user์—๊ฒŒ ๊ถŒํ•œ์ด ๋ถ€์—ฌ๋œ๋‹ค.

create role teaching_assistant;
grant teaching_assistant to instructor;

์„ ํ•ด์ฃผ๋ฉด instructor๋Š” ๋‹ค์‹œ teaching_assistant์˜ ๊ถŒํ•œ์„ ์ƒ์†๋ฐ›๊ฒŒ ๋œ๋‹ค.
์ฆ‰
teaching_assistant -> instructor -> amit

Chain of roles

create role dean;
grant instructor to dean;
grant dean to Satoshi;