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

๐Ÿ‡ฉ๐Ÿ‡ฆ๐Ÿ‡น๐Ÿ‡ฆ๐Ÿ‡ง๐Ÿ‡ฆ๐Ÿ‡ธ๐Ÿ‡ช

SQL ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด(DML), ์ค‘์ฒฉ ์งˆ์˜, ์™ธ๋ถ€ ์งˆ์˜, IN, EXISTS, ๋ทฐ (view)

์ค‘์ฒฉ ์งˆ์˜(nested query)

- SQL๋ฌธ์„ ๋‹ค๋ฅธ SQL๋ฌธ ์•ˆ์— ์ค‘์ฒฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ์งˆ์˜

- ๋ณต์žกํ•œ ์งˆ์˜๋ฅผ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณต

- ๋‚ด๋ถ€์— ํฌํ•จ๋œ SQL๋ฌธ์„ ๋ถ€-์งˆ์˜ ๋˜๋Š” ๋‚ด๋ถ€์งˆ์˜๋ผ๊ณ  ํ•จ

โ€‹

IN, NOT IN

์˜ˆ) '301ํ˜ธ' ๊ฐ•์˜์‹ค์—์„œ ๊ฐœ์„ค๋œ ๊ฐ•์ขŒ์˜ ๊ณผ๋ชฉ๋ช…์„ ์ถœ๋ ฅ

select title
from   course
where  course_id in
       ( select distinct course_id
         from   class
         where  classroom = '301ํ˜ธ')

 

๋ถ€-์งˆ์˜

โ—ฝ class ํ…Œ์ด๋ธ”์—์„œ ๊ฐ•์˜์‹ค์ด '301ํ˜ธ'์ธ ๊ต๊ณผ๋ชฉ ๋ฒˆํ˜ธ๋ฅผ ๊ฒ€์ƒ‰

์™ธ๋ถ€-์งˆ์˜

โ—ฝ course ํ…Œ์ด๋ธ”์—์„œ course_id ํ•„๋“œ์˜ ๊ฐ’์ด ๋ถ€-์งˆ์˜์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ํฌํ•จ๋˜๋Š” ๋ ˆ์ฝ”๋“œ๋งŒ ์„ ํƒํ•˜์—ฌ ๊ณผ๋ชฉ๋ช… ์ถœ๋ ฅ

โ€‹

์˜ˆ) 2012๋…„ 2ํ•™๊ธฐ์— ๊ฐœ์„ค๋˜์ง€ ์•Š์€ ๊ณผ๋ชฉ๋ช…์„ ๊ฒ€์ƒ‰

select title
from   course
where  course_id not in
       ( select distinct course_id
         from   class
         where  year = 2012 and semester = 2)

 

SOME, ALL

 

=some

- ์ง€์ •๋œ ํ•„๋“œ์˜ ๊ฐ’์ด ๋ถ€-์งˆ์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ์ž„์˜์˜ ๊ฐ’๊ณผ ๊ฐ™์€์ง€๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ ์‚ฌ์šฉ
- in๊ณผ ๊ฐ™์€ ์˜๋ฏธ
<=some

- ๋ถ€-์งˆ์˜์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ์ž„์˜์˜ ๊ฐ’๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€์ง€๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ ์‚ฌ์šฉ
=all

- ์ง€์ •๋œ ํ•„๋“œ์˜ ๊ฐ’์ด ๋ถ€-์งˆ์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ํฌํ•จ๋œ ๋ชจ๋“  ๊ฐ’๊ณผ ๊ฐ™์€์ง€๋ฅผ ํŒ๋‹จ
<=all

- ์ง€์ •๋œ ํ•„๋“œ์˜ ๊ฐ’์ด ๋ถ€-์งˆ์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ํฌํ•จ๋œ ๋ชจ๋“  ๊ฐ’๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€์ง€๋ฅผ ํŒ๋‹จ

 

โ€‹

์˜ˆ) ๊ฐ€์žฅ ๋งŽ์€ ์ˆ˜๊ฐ• ์ธ์›์„ ๊ฐ€์ง„ ๊ฐ•์ขŒ๋ฅผ ๊ฒ€์ƒ‰

select  c1.course_id, title, year, semester, prof_id
from    class c1, course c2
where   c1.course_id = c2.course_id
        and enroll >= all (select enroll from class)

 

EXISTS, NOT EXIST

- ๋ถ€-์งˆ์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ์ตœ์†Œํ•œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€์˜ ์—ฌ๋ถ€๋ฅผ ํ‘œํ˜„

โ—ฝ exists: ์ตœ์†Œํ•œ ํ•œ ๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ฐธ์ด ๋˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฑฐ์ง“

โ—ฝ not exists: ๋ถ€-์งˆ์˜์˜ ๊ฒฐ๊ณผ์— ๋ ˆ์ฝ”๋“œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†์œผ๋ฉด ์ฐธ์ด ๋˜๊ณ  ํ•˜๋‚˜๋ผ๋„ ์กด์žฌํ•˜๋ฉด ๊ฑฐ์ง“

โ€‹

์˜ˆ) '301ํ˜ธ' ๊ฐ•์˜์‹ค์—์„œ ๊ฐœ์„ค๋œ ๊ฐ•์ขŒ์˜ ๊ณผ๋ชฉ๋ช…์„ ์ถœ๋ ฅ

select  title
from    course
where   exists
        (select *
         from   class
         where  classroom = '301ํ˜ธ' and
                course.course_id = class.course_id)

์˜ˆ) 2012๋…„ 2ํ•™๊ธฐ์— ๊ฐœ์„ค๋˜์ง€ ์•Š์€ ๊ณผ๋ชฉ๋ช…์„ ๊ฒ€์ƒ‰

select  title
from    course
where   not exists
        (select *
         from   class
         where  year = 2012 and
         		semester = 2 and
                course.course_id = class.course_id)

 

๋ทฐ (view)

- ๊ธฐ์กด ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋˜๋Š” ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”

- ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ธฐ์กด์˜ ํ…Œ์ด๋ธ”๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š” ํ…Œ์ด๋ธ”์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•จ

- ๊ธฐ๋ฐ˜ ํ…Œ์ด๋ธ”๋“ค์— ๋Œ€ํ•œ ํ•œ์˜ ์งˆ์˜๋ฌธ์œผ๋กœ ์ •์˜๋จ

- ๋ทฐ๋Š” ๊ธฐ๋ฐ˜ ํ…Œ์ด๋ธ”๊ณผ ๋™๋“ฑํ•˜๊ฒŒ ์‚ฌ์šฉ

โ€‹

๊ธฐ๋Šฅ

- ํŠน์ • ์‚ฌ์šฉ์ž์—๊ฒŒ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ ์ค‘ ์ผ๋ถ€๋ฅผ ์ˆจ๊ธธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ ‘๊ทผ ์ œํ•œ ๋ฐ ๋ณด์•ˆ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Œ

- ์งˆ์˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ทฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ์งˆ์˜๋ฅผ ๊ฐ„๋‹จํžˆ ํ‘œํ˜„ ๊ฐ€๋Šฅ

โ€‹

โœ” ๋ทฐ ์ƒ์„ฑ

ํ˜•์‹ : create view <๋ทฐ ์ด๋ฆ„> as <select๋ฌธ> ๋˜๋Š” create or replace view <๋ทฐ ์ด๋ฆ„> as <select๋ฌธ>

 

โ• ๋ทฐ ์ƒ์„ฑ ๊ถŒํ•œ

- ๋Œ€๋ถ€๋ถ„์˜ DBMS์—์„œ ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž ๊ณ„์ •์—๋Š” ๋ทฐ ์ƒ์„ฑ ๊ถŒํ•œ์ด ๋ถ€์—ฌ๋˜์ง€ ์•Š์Œ

- ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋ทฐ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๊ด€๋ฆฌ์ž๊ฐ€ ์‚ฌ์šฉ์ž ๊ณ„์ •์— ๋ทฐ ์ƒ์„ฑ๊ณผ ๊ด€๋ จ๋œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์•ผํ•จ

ํ˜•์‹ : grant create view to <์‚ฌ์šฉ์ž ๊ณ„์ •>

โ€‹

์˜ˆ) takes ํ…Œ์ด๋ธ”์—์„œ grade ํ•„๋“œ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ํ•„๋“œ๋งŒ์œผ๋กœ ๊ตฌ์ •๋œ ๋ทฐ๋ฅผ ์ƒ์„ฑ

create or replace view v_takes as
           select stu_id, class_id from takes

 

โœ” ๋ทฐ ์‚ฌ์šฉ

- ๋ทฐ์— ๋Œ€ํ•ด insert, update, delete ๋ฌธ์„ ์‹คํ–‰

- ๋ทฐ ์ƒ์„ฑ์— ์‚ฌ์šฉ๋œ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์ ์šฉ๋จ

โ€‹

โœ” ๋ทฐ ์‚ญ์ œ

ํ˜•์‹ : drop view <๋ทฐ ์ด๋ฆ„>

โ€‹