๐Ÿ“š๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด

๐Ÿ“„๊ณ ๊ธ‰ SELECT ๋ฌธ

  • ์ง‘๊ณ„ ํ•จ์ˆ˜
    ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ์ „์ฒด ๋˜๋Š” ์ผ๋ถ€ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ†ต๊ณ„์ ์ธ ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
ํ•จ์ˆ˜๋ช… ๊ธฐ๋Šฅ
AVG ์ปฌ๋Ÿผ์— ์žˆ๋Š” ๊ฐ’๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•จ
MIN ์ปฌ๋Ÿผ์— ์žˆ๋Š” ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ตฌํ•จ
MAX ์ปฌ๋Ÿผ์— ์žˆ๋Š” ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•จ
SUM ์ปฌ๋Ÿผ์— ์žˆ๋Š” ๊ฐ’๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•จ
COUNT ์ปฌ๋Ÿผ์— ์žˆ๋Š” ๊ฐ’๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•จ


๐Ÿ“๋‹จ๊ณผ๋Œ€ํ•™์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

1
2
SELECT COUNT(DISTINCT ๋‹จ๊ณผ๋Œ€ํ•™) AS ๋‹จ๊ณผ๋Œ€ํ•™์ˆ˜
  FROM ํ•™๊ณผ



  • ๊ทธ๋ฃน ์งˆ์˜
    ์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ์ „์ฒด ๋ ˆ์ฝ”๋“œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ˆ˜ํ–‰๋˜์–ด ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ๋‹ค์ˆ˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ๊ฐ ๊ทธ๋ฃน๋ณ„๋กœ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ทธ๋ฃน ์งˆ์˜๋ฅผ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฃน ์งˆ์˜๋ฅผ ์œ„ํ•ด GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•˜๊ณ  ๊ทธ๋ฃน ๋‚ด์— ์กฐ๊ฑด ์ง€์ •์„ ์›ํ•˜๋Š” ๊ฒฝ์šฐ HAVING ์ ˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
1
2
3
4
/* SELECT ์ ˆ์— ๊ทธ๋ฃน์˜ ๊ธฐ์ค€๊ณผ ์ง‘๊ณ„ํ•จ์ˆ˜ 
์ด์™ธ์˜ ์ปฌ๋Ÿผ์€ ํฌํ•จ๋  ์ˆ˜ ์—†์Œ */
SELECT ์งˆ์˜
  GROUP BY ์ปฌ๋Ÿผ


๐Ÿ“์†Œ์†ํ•™๊ณผ๋ณ„ ๊ต์ˆ˜์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

1
2
3
4
5
6
/* SELECT ์ ˆ์— ๊ต์ˆ˜์ด๋ฆ„ ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•˜๋ฉด
๊ทธ๋ฃน์˜ ๊ธฐ์ค€๊ณผ ์ง‘๊ณ„ํ•จ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์ปฌ๋Ÿผ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— 
ํฌํ•จ๋  ์ˆ˜ ์—†์Œ */
SELECT ์†Œ์†ํ•™๊ณผ, COUNT(*) AS ๊ต์ˆ˜์ˆ˜, ๊ต์ˆ˜์ด๋ฆ„(X)
  FROM ๊ต์ˆ˜
  GROUP BY ์†Œ์†ํ•™๊ณผ



  • HAVING ์ ˆ
    GROUP BY ์—ฐ์‚ฐ๊ณผ ์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  ๊ทธ๋ฃน์— ๋Œ€ํ•œ ์ง‘๊ณ„ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๋Š” ์ •๋ณด๋งŒ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด HAVING ์ ˆ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
1
2
3
SELECT ์งˆ์˜
  GROUP BY ์ปฌ๋Ÿผ
  HAVING ์กฐ๊ฑด
  • WHERE: ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•œ ์กฐ๊ฑด
  • HAVING ์ ˆ: ์ง‘๊ณ„ ๊ฒฐ๊ณผ ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•œ ์กฐ๊ฑด


๐Ÿ“2๊ฐœ ์ด์ƒ์˜ ์ „๊ณต์„ ์‹ ์ฒญํ•œ ํ•™์ƒ์˜ ํ•™์ƒ๋ฒˆํ˜ธ์™€ ์‹ ์ฒญ ์ „๊ณต ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

1
2
3
4
SELECT ํ•™์ƒ๋ฒˆํ˜ธ, COUNT(*) AS ์‹ ์ฒญ_์ „๊ณต์ˆ˜
  FROM ์ „๊ณต
  GROUP BY ํ•™์ƒ๋ฒˆํ˜ธ
  HAVING ์‹ ์ฒญ_์ „๊ณต์ˆ˜ >= 2



  • ์ค‘์ฒฉ ์งˆ์˜
    ์ค‘์ฒฉ ์งˆ์˜๋Š” SELECT ๋ฌธ ๋‚ด๋ถ€์— ๋‹ค๋ฅธ SELECT ๋ฌธ์ด ๋“ค์–ด์žˆ๋Š” ํ˜•ํƒœ์˜ ์งˆ์˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋‚ด๋ถ€ ์งˆ์˜์˜ ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ์™ธ๋ถ€ ์งˆ์˜์—์„œ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ค‘์ฒฉ ์งˆ์˜๋Š” ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

-FROM ์ ˆ์—์„œ์˜ ์ค‘์ฒฉ ์งˆ์˜ ํ™œ์šฉ: FROM ์ ˆ์—์„œ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ SELECT์—์„œ ์žฌ๊ฒ€์ƒ‰

1
2
3
4
5
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
  FROM(SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผm
          FROM ํ…Œ์ด๋ธ”
          WHERE ์กฐ๊ฑด)
  WHERE ์กฐ๊ฑด


-WHERE ์ ˆ์—์„œ์˜ ์ค‘์ฒฉ ์งˆ์˜ ํ™œ์šฉ: WHERE ์ ˆ์—์„œ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ํ™œ์šฉํ•˜์—ฌ ์™ธ๋ถ€ ์งˆ์˜์—์„œ ๋ ˆ์ฝ”๋“œ ์ถœ๋ ฅ ์—ฌ๋ถ€ ๊ฒฐ์ •. IN, NOT IN, EXISTS, NOT EXISTS ์‚ฌ์šฉ.

1
2
3
4
5
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
  FROM ํ…Œ์ด๋ธ”
  WHERE ์ปฌ๋Ÿผi์—ฐ์‚ฐ์ž(SELECT ์ปฌ๋Ÿผj
                        FROM ํ…Œ์ด๋ธ”2
                        WHERE ์กฐ๊ฑด)



  • ๊ต์ง‘ํ•ฉ๊ณผ ์ฐจ์ง‘ํ•ฉ ์—ฐ์‚ฐ
    ์—ฌ๋Ÿฌ ๋ ˆ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋œ ๋ฆฌ์ŠคํŠธ ์ค‘ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด IN ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฐจ์ง‘ํ•ฉ ์—ฐ์‚ฐ์— ํ•ด๋‹นํ•˜๋Š” EXCEPT ์—ฐ์‚ฐ์€ ํ•œ ํ…Œ์ด๋ธ”์— ํฌํ•จ๋˜์ง€๋งŒ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์—๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋ ˆ์ฝ”๋“œ ์ง‘ํ•ฉ์„ ์ถ”์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“โ€™์ปดํ“จํ„ฐ๊ณผํ•™๊ณผโ€™๋ฅผ ์ „๊ณตํ•˜๋Š” ํ•™์ƒ ์ค‘ โ€˜์ˆ˜ํ•™๊ณผโ€™๋„ ์ „๊ณตํ•˜๋Š” ํ•™์ƒ์˜ ํ•™์ƒ๋ฒˆํ˜ธ, ์ด์ˆ˜ํ•™์ ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

1
2
3
4
5
SELECT ํ•™์ƒ๋ฒˆํ˜ธ, ์ด์ˆ˜ํ•™์ 
  FROM ์ „๊ณต
  WHERE ํ•™๊ณผ์ด๋ฆ„ = '์ˆ˜ํ•™๊ณผ' AND
        ํ•™์ƒ๋ฒˆํ˜ธ IN (SELECT ํ•™์ƒ๋ฒˆํ˜ธ FROM ์ „๊ณต
                      WHERE ํ•™๊ณผ์ด๋ฆ„ = '์ปดํ“จํ„ฐ๊ณผํ•™๊ณผ')



  • EXISTS์™€ NOT EXISTS
    EXISTS๋‚˜ NOT EXISTS์‚ฌ ์‚ฌ์šฉ๋˜๋ฉด ์™ธ๋ถ€ ์งˆ์˜์˜ ๊ฐ ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด ๋‚ด๋ถ€ ์งˆ์˜์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. EXISTS๋Š” ์™ธ๋ถ€ ์งˆ์˜์˜ ๊ฐ ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด ๋‚ด๋ถ€ ์งˆ์˜์˜ ๊ฒฐ๊ณผ์— ๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. NOT EXISTS๋Š” ์ด์™€ ๋ฐ˜๋Œ€๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“โ€™์ปดํ“จํ„ฐ๊ณผํ•™๊ณผโ€™์†Œ์† ํ•™์ƒ ์ค‘ ์ˆ˜๊ฐ•์‹ ์ฒญ์„ ํ•˜์ง€ ์•Š์€ ํ•™์ƒ์˜ ํ•™์ƒ๋ฒˆํ˜ธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์‹œ์˜ค.

1
2
3
4
5
6
SELECT A.ํ•™์ƒ๋ฒˆํ˜ธ
  FROM ์ „๊ณต AS A
  WHERE ํ•™๊ณผ์ด๋ฆ„ = '์ปดํ“จํ„ฐ๊ณผํ•™๊ณผ' AND
        NOT EXISTS (SELECT ํ•™์ƒ๋ฒˆํ˜ธ 
                    FROM ์ˆ˜๊ฐ• AS B
                    WHERE A.ํ•™์ƒ๋ฒˆํ˜ธ = B.ํ•™์ƒ๋ฒˆํ˜ธ)



๐Ÿ“„์กฐ์ธ ์—ฐ์‚ฐ์„ ์ด์šฉํ•œ SELECT ๋ฌธ

์กฐ์ธ ์งˆ์˜๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๋ จ์„ฑ์„ ์ด์š”ํ•˜์—ฌ ์ž„์‹œ์ ์œผ๋กœ ๋ ˆ์ฝ”๋“œ๋ฅผ ํ†ตํ•ฉํ•œ ํ›„ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ์งˆ์˜๊ธฐ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

  • ํฌ๋กœ์Šค ์กฐ์ธ
    ํฌ๋กœ์Šค ์กฐ์ธ์€ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ์กฐ๊ฑด ์—†์ด ๋ชจ๋“  ์กฐํ•ฉ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ๊ด€๊ณ„ ๋Œ€์ˆ˜์˜ ์นดํ‹ฐ์…˜ ํ”„๋กœ๋•ํŠธ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
1
2
3
4
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
  FROM ํ…Œ์ด๋ธ”1, CROSS JOIN ํ…Œ์ด๋ธ”2
    [... CROSS JOIN ํ…Œ์ด๋ธ”m]
  WHERE ์กฐ๊ฑด


๐Ÿ“30์„ธ ์ด์ƒ์ธ ํ•™์ƒ๊ณผ ๋ชจ๋“  ๊ณ„์ขŒ์— ๋Œ€ํ•ด ํฌ๋กœ์Šค ์กฐ์ธํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

1
2
3
SELECT ํ•™์ƒ.*, ๊ณ„์ขŒ.*
  FROM ํ•™์ƒ CROSS JOIN ๊ณ„์ขŒ
  WHERE ํ•™์ƒ.๋‚˜์ด >= 30



  • ๋‚ด๋ถ€ ์กฐ์ธ
    ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ์กฐ์ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ ˆ์ฝ”๋“œ ๋งŒ ๊ฒฐํ•ฉํ•˜์—ฌ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์— ํฌํ•จ์‹œํ‚ค๋Š” ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ์กฐ์ธ ์กฐ๊ฑด์€ WHERE ์ ˆ์ด ์•„๋‹Œ ON ์ ˆ์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
1
2
3
4
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
  FROM ํ…Œ์ด๋ธ”1, INNER JOIN ํ…Œ์ด๋ธ”2
  ON ์กฐ์ธ์กฐ๊ฑด1
  [WHERE ์กฐ๊ฑด]


๐Ÿ“30์„ธ ์ด์ƒ์ธ ํ•™์ƒ์˜ ํ•™์ƒ์ด๋ฆ„๊ณผ ๋‚˜์ด, ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ•™์ƒ์ด ์†Œ์œ ํ•œ ๊ณ„์ขŒ์˜ ๊ณ„์ขŒ๋ฒˆํ˜ธ, ์ž”์•ก์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

1
2
3
4
5
SELECT ํ•™์ƒ.ํ•™์ƒ์ด๋ฆ„, ํ•™์ƒ.๋‚˜์ด
       ๊ณ„์ขŒ.๊ณ„์ขŒ๋ฒˆํ˜ธ, ๊ณ„์ขŒ.์ž”์•ก
  FROM ํ•™์ƒ INNER JOIN ๊ณ„์ขŒ
  ON ํ•™์ƒ.ํ•™์ƒ๋ฒˆํ˜ธ = ๊ณ„์ขŒ.ํ•™์ƒ๋ฒˆํ˜ธ
  WHERE ํ•™์ƒ.๋‚˜์ด >= 30


๐Ÿ’กOracle ์‚ฌ์˜ ๊ตฌ๋ฌธ ํ˜•์‹

1
2
3
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
  FROM ํ…Œ์ด๋ธ”1, ํ…Œ์ด๋ธ”2, [... ํ…Œ์ด๋ธ”์ด๋ฆ„m]
  WHERE ์กฐ๊ฑด AND ์กฐ์ธ์กฐ๊ฑด1 [... AND ์กฐ์ธ์กฐ๊ฑดp]



  • ์ž์—ฐ ์กฐ์ธ
    ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๋‚ด๋ถ€ ์กฐ์ธ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํ…Œ์ด๋ธ”์— ๋™์ผํ•œ ์ด๋ฆ„์˜ ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ ๊ฐ’์ด ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋‚ด๋ถ€ ์กฐ์ธ์ž…๋‹ˆ๋‹ค.
1
2
3
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
  FROM ํ…Œ์ด๋ธ”1 NATURAL JOIN ํ…Œ์ด๋ธ”2
  [WHERE ์กฐ๊ฑด]



  • ์™ธ๋ถ€ ์กฐ์ธ
    ๋‚ด๋ถ€ ์กฐ์ธ์€ ์กฐ์ธ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋งŒ ๊ฒฐํ•ฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์กฐ์ธ ๊ฒฐ๊ณผ์— ์ •๋ณด์˜ ์†์‹ค์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์™ธ๋ถ€ ์กฐ์ธ์€ ์กฐ์ธ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š” ๋ ˆ์ฝ”๋“œ๋„ ์งˆ์˜์˜ ๊ฒฐ๊ณผ์— ํฌํ•จ์‹œํ‚ค๋Š” ์งˆ์˜์ž…๋‹ˆ๋‹ค.

    • ์™ผ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ
    • ์˜ค๋ฅธ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ
    • ์™„์ „ ์™ธ๋ถ€ ์กฐ์ธ
1
2
3
4
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
  FROM ํ…Œ์ด๋ธ”1 LEFT|RIGHT[OUTER] JOIN ํ…Œ์ด๋ธ”2
  ON ํ…Œ์ด๋ธ”์ด๋ฆ„1.์ปฌ๋Ÿผ = ํ…Œ์ด๋ธ”์ด๋ฆ„.์ปฌ๋Ÿผ
  [WHERE ์กฐ๊ฑด]


๐Ÿ“ํ•™์ƒ์˜ ํ•™์ƒ๋ฒˆํ˜ธ, ํ•™์ƒ์ด๋ฆ„๊ณผ ๊ทธ ํ•™์ƒ์ด ์ˆ˜๊ฐ•์‹ ์ฒญํ•œ ๊ณผ๋ชฉ์˜ ๊ณผ๋ชฉ์ฝ”๋“œ, ์‹ ์ฒญ์‹œ๊ฐ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.(๋‹จ, ์ˆ˜๊ฐ•์‹ ์ฒญ์„ ํ•˜์ง€ ์•Š์€ ํ•™์ƒ๋„ ๊ฒฐ๊ณผ์— ํฌํ•จ์‹œํ‚ค๊ณ  ๊ณผ๋ชฉ์ฝ”๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.)

1
2
3
4
SELECT A.ํ•™์ƒ๋ฒˆํ˜ธ, A.ํ•™์ƒ์ด๋ฆ„, B.๊ณผ๋ชฉ์ฝ”๋“œ, B.์‹ ์ฒญ์‹œ๊ฐ
  FROM ํ•™์ƒ AS A LEFT OUTER JOIN ๊ณ„์ขŒ AS B
  ON A.ํ•™์ƒ๋ฒˆํ˜ธ = B.ํ•™์ƒ๋ฒˆํ˜ธ
  ORDER BY ๊ณผ๋ชฉ ์ฝ”๋“œ ASC



  • ์…€ํ”„ ์กฐ์ธ
    ์…€ํ”„ ์กฐ์ธ์€ ํ•œ ํ…Œ์ด๋ธ”์ด ์ž๊ธฐ ์ž์‹ ๊ณผ ์กฐ์ธ๋˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์ด๋ฆ„์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ์กฐ์ธ์ด๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ํ…Œ์ด๋ธ” ์ด๋ฆ„์— ๋Œ€ํ•œ ๋ณ„์นญ์ด ์˜๋ฌด์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
1
2
3
4
5
SELECT ๋ณ„์นญ1.์ปฌ๋Ÿผ1, ๋ณ„์นญ1.์ปฌ๋Ÿผ2, ..., ๋ณ„์นญ1.์ปฌ๋Ÿผn,
       ๋ณ„์นญ2.์ปฌ๋Ÿผ1, ๋ณ„์นญ2.์ปฌ๋Ÿผ2, ..., ๋ณ„์นญ2.์ปฌ๋Ÿผm
  FROM ํ…Œ์ด๋ธ”1 AS ๋ณ„์นญ1 INNER|OUTER JOIN ํ…Œ์ด๋ธ”2 AS ๋ณ„์นญ2
  ON ์กฐ์ธ์กฐ๊ฑด
  [WHERE ์ ˆ]




๐Ÿ“š๋ทฐ์˜ ์‚ฌ์šฉ

๋ทฐ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ์›๋ณธ ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ์œ ๋„๋˜์–ด ์ผ๋ฐ˜ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์ƒ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ฆฌ์ ์œผ๋กœ ์ €์žฅ๋˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ ์‚ฌ์ „์— ๋ทฐ์—๋Œ€ํ•œ ์ •์˜๋งŒ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

  • ๋ฐ์ดํ„ฐ ๋…๋ฆฝ์„ฑ: ์›๋ณธ ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ๋ทฐ๋ฅผ ์ด์šฉํ•œ ์ž‘์—…์€ ์ •์˜๋งŒ ๋ณ€๊ฒฝ๋˜์–ด ์‘์šฉํ”„๋กœ๊ทธ๋žจ์— ์˜ํ–ฅ์ด ์—†์Œ

  • ๋ฐ์ดํ„ฐ ๋ณด์•ˆ: ์‚ฌ์šฉ์ž์—๊ฒŒ ์›๋ณธ ํ…Œ์ด๋ธ”์˜ ์ผ๋ถ€ ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜์—ฌ ๋ณด์•ˆ ํšจ๊ณผ ํ–ฅ์ƒ

  • ๋‹ค์–‘ํ•œ ๊ตฌ์กฐ์˜ ํ…Œ์ด๋ธ” ์‚ฌ์šฉ: ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๋Š” ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ ์ œ๊ณต

  • ์ž‘์—…์˜ ๋‹จ์ˆœํ™”: ๋ณต์žกํ•œ ์งˆ์˜๋ฌธ์„ ๋ทฐ๋กœ ๋‹จ์ˆœํ™”

  • ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ: WITH CHECK OPTION์„ ์ด์šฉํ•˜์—ฌ ๋ทฐ ์ƒ์„ฑ์— ์œ„๋ฐฐ๋˜๋Š” ์ˆ˜์ •์ž‘์—…์„ ๊ฑฐ๋ถ€


๐Ÿ“„๋ทฐ์˜ ์ƒ์„ฑ

SELECT ๋ฌธ์œผ๋กœ ๊ฒ€์ƒ‰๋  ์ˆ˜ ์žˆ๋Š” ์–ด๋–ค ํ…Œ์ด๋ธ”๋„ ๋ทฐ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ƒ์„ฑ๋˜๋Š” ๋ทฐ์˜ ๊ตฌ์กฐ๋Š” SELECT ๋ฌธ์˜ ๊ฒฐ๊ณผ๋กœ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.

1
2
3
4
5
CREATE VIEW ๋ทฐ์ด๋ฆ„ AS
  (SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
      FROM ํ…Œ์ด๋ธ”
      [WHERE ์กฐ๊ฑด])
[WITH CHECK OPTION]



๐Ÿ“„๋ทฐ์˜ ์ˆ˜์ •

ALTER VIEW๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑ๋œ ๋ทฐ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1
2
3
4
ALTER VIEW ๊ธฐ์กด๋ทฐ์ด๋ฆ„(์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ... ์ปฌ๋Ÿผn) AS
  ( SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ..., ์ปฌ๋Ÿผn
    [FROM ํ…Œ์ด๋ธ”]
    [WHERE ์กฐ๊ฑด] )



๐Ÿ“„๋ทฐ์˜ ์‚ญ์ œ

DROP VIEW ๋ฌธ์„ ์‚ฌ์šฉํ•ด ํ•„์š”์—†๋Š” ๋ทฐ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ญ์ œ๋œ ๋ทฐ๋ฅผ ๊ทผ๊ฑฐ๋กœ ์ƒ์„ฑ๋œ ๋‹ค๋ฅธ ๋ทฐ ๋˜ํ•œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ทฐ๋ฅผ ์‚ญ์ œํ•ด๋„ ๋ทฐ์˜ ๊ทผ๊ฑฐ๊ฐ€ ๋˜๋Š” ์‹ค์ œ ๋ฌผ๋ฆฌ์ ์ธ ํ…Œ์ด๋ธ”์€ ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

1
DROP VIEW ๋ทฐ์ด๋ฆ„



๐Ÿ“„๋ทฐ์™€ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ ์กฐ์ž‘

๋ทฐ๋Š” ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ทฐ์™€ ๊ด€๋ จ๋˜ ๋†Ž์ž‘์€ ํ…Œ์ด๋ธ” ์กฐ์ž‘๊ณผ ๋ถ€๋ถ„์ ์„ ๋™์ผํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ทฐ์— ๋Œ€ํ•œ INSERT ๋ฌธ์€ ํ…Œ์ด๋ธ”์—์„œ์™€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋ทฐ๊ฐ€ ์›๋ณธ ํ…Œ์ด๋ธ”์—์„œ NOT NULL๋กœ ์ •์˜๋œ ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ ์ œ์•ฝ์กฐ๊ฑด์„ ๊ฐ์ถ”๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.


  • ๋ทฐ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰
    ๋ทฐ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰์€ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ ๋ทฐ ์ •์˜์—์„œ ์ œ์™ธ๋œ ์ปฌ๋Ÿผ์˜ ์ •๋ณด๋Š” ๊ฒ€์ƒ‰์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
1
2
SELECT * FROM ๋ทฐ์ด๋ฆ„
  WHERE ์กฐ๊ฑด


  • ๋ทฐ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
    ๋ทฐ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…์€ INSERT ๋ฌธ์ด ๋ทฐ๊ฐ€ ์•„๋‹Œ ์›๋ณธ ํ…Œ์ด๋ธ”์—์„œ ์‹คํ–‰๋˜์–ด ์‚ฝ์ž…์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

๋ทฐ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์œ ์˜ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค.

  1. PRIMARY KEY, NOT NULL ๋“ฑ์˜ ์ œ์•ฝ์‚ฌํ•ญ์ด ์œ„๋ฐฐ๋˜๋Š” ๊ฒฝ์šฐ ์‚ฝ์ž…์ด ๋ถˆ๊ฐ€๋Šฅ

  2. ์›๋ณธ ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ์ปฌ๋Ÿผ์ด์ง€๋งŒ ๋ทฐ์—๋Š” ์—†๋Š” ์ปฌ๋Ÿผ์— ์‚ฝ์ž…ํ•˜๋Š” ๊ฒฝ์šฐ ์‹คํ–‰ ๋ถˆ๊ฐ€๋Šฅ

  3. ์กฐ์ธ ์งˆ์˜ ๋˜๋Š” ๊ทธ๋ฃน ์งˆ์˜๊ฐ€ ์ ์šฉ๋œ ๋ทฐ๋Š” ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ๋ฐ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅ

  4. WITH CHECK OPTION์ด ์ ์šฉ๋œ ๋ทฐ๋Š” ์œ„๋ฐฐ๋˜๋Š” ์‚ฌํ•ญ์€ ์—†์ง€๋งŒ ๋ทฐ์— ๋งž์ง€ ์•Š๋Š” ์กฐ๊ฑด์ผ ๊ฒฝ์šฐ ์‹คํ–‰ ๋ถˆ๊ฐ€๋Šฅ



Leave a comment