2021-04-27(Tue)

ํ•ญ๋ชฉ

๋‚ด์šฉ

ํ•™์Šต ๋‚ ์งœ

2021-04-27(ํ™”)

ํ•™์Šต ์‹œ๊ฐ„

09:00~23:00

ํ•™์Šต ๋ฒ”์œ„ ๋ฐ ์ฃผ์ œ

AWS EC2

ํ•™์Šต ๋ชฉํ‘œ

Rails ์„œ๋น„์Šค ๋ฐฐํฌํ•˜์ž.

๋™๋ฃŒ ํ•™์Šต ๋ฐฉ๋ฒ•

-

์ƒ์„ธ ํ•™์Šต ๋‚ด์šฉ

๊ฐ„๋‹จํ•˜๊ฒŒ ํŠธ๋ Œ์„ผ๋˜์Šค ํ”„๋กœ์ ํŠธ๋ฅผ AWS EC2๋กœ ๋ฐฐํฌํ•˜์˜€๋‹ค!

๋ฐฐํฌ ๋งํฌ: https://bit.ly/3sU04CL

Nginx ์™€ Capistrano ์กฐํ•ฉ์„ ์ด์šฉํ•ด์„œ ์ƒ์šฉ ์„œ๋น„์Šค์ฒ˜๋Ÿผ ๋ฐฐํฌํ•˜๋Š” ๊ฒƒ๋„ ๊ณ ๋ คํ•ด๋ณด์•˜์œผ๋‚˜, ์ง€๊ธˆ ํ•ด์•ผํ•  ํ”„๋กœ์ ํŠธ๊ฐ€ ๋งŽ์•„์„œ ์šฐ์„  ์ˆœ์œ„์—์„œ ๋ฏธ๋ค˜๋‹ค.

์ฐธ๊ณ 

https://kbs4674.tistory.com/126

๊ทธ๋ฆฌ๊ณ  SQL์˜ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„์„ ์žก๊ธฐ ์œ„ํ•ด ์ฑ…์„ ์ฝ์—ˆ๋‹ค.

SQL ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„์„ ์žก์ž

RDBMS๋กœ ๊ฐ์ฒด ๋‹ค๋ฃจ๋“ฏ ํ•˜๋‹ค๋ณด๋‹ˆ SQL ๋ฌธ๋ฒ•์ด ํ—ท๊ฐˆ๋ฆฐ๋‹ค.

์กฐ์ธ(Join)

์กฐ์ธ ๊ธฐ๋ฒ•์˜ ์ข…๋ฅ˜

  • ๊ณฑ์ง‘ํ•ฉ(cartesian product)

    • ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธ

  • ๋™๋“ฑ ์กฐ์ธ(equi join or inner join)

    • ์กฐ์ธ ์กฐ๊ฑด์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์— ๊ฒฐ๊ณผ ์ถœ๋ ฅ

  • ๋น„๋™๋“ฑ ์กฐ์ธ(non equi join)

    • ์กฐ์ธ ์กฐ๊ฑด์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ

  • ์™ธ๋ถ€ ์กฐ์ธ(outer join)

    • ์กฐ์ธ ์กฐ๊ฑด์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š์•„๋„ ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ

  • ์ž์ฒด ์กฐ์ธ(self join)

    • ์ž์ฒด ํ…Œ์ด๋ธ”์—์„œ ์กฐ์ธํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ

๋™๋“ฑ ์กฐ์ธ ์˜ˆ์‹œ

-- employees ํ…Œ์ด๋ธ”๊ณผ departments ํ…Œ์ด๋ธ”๊ณผ locations ํ…Œ์ด๋ธ”์„ ์กฐ์ธ ์˜ˆ์‹œ
โ€‹
SELECT  A.employee_id, A.department_id, B.department_name, C.location_id, C.city
FROM employees A, departments B, locations C
WHERE A.department_id = B.department_id
AND B.location_id = C.location_id;

์™ธ๋ถ€ ์กฐ์ธ

์™ธ๋ถ€ ์กฐ์ธ์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์€ ํ–‰๋„ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์กฐ์ธ ๊ธฐ๋ฒ•.

-- employees ํ…Œ์ด๋ธ”๊ณผ departments ํ…Œ์ด๋ธ”์„ department_id๋กœ ์™ธ๋ถ€ ์กฐ์ธํ•˜์—ฌ department_id๊ฐ€ null ๊ฐ’์ธ Kimberely Grant๋„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•ด๋ผ.
โ€‹
SELECT A.employee_id, A.first_name, A.last_name, B.department_id, B.department_name
FROM employees A, departemnts B
WHERE A.department_id = B.department_id(+)
ORDER BY A.employee_id;

์•„๋ž˜์ฒ˜๋Ÿผ ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•ด๋ณด์ž.

  1. ์–‘์ชฝ ํ…Œ์ด๋ธ” ์ค‘ ์ „๋ถ€ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ด๋ธ” ์ชฝ์„ ๋จผ์ € ์ƒ๊ฐํ•œ๋‹ค.

  2. (+)๋Š” ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ” ์ชฝ ์กฐ์ธ ์กฐ๊ฑด์— ๋ถ™์ธ๋‹ค.

์ž์ฒด ์กฐ์ธ

์ž์ฒด ์กฐ์ธ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ณ„์นญ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

-- employees ํ…Œ์ด๋ธ”์„ ์ž์ฒด ์กฐ์ธํ•˜์—ฌ ์ง์›๋ณ„ ๋‹ด๋‹น ๋งค๋‹ˆ์ €๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ์กฐํšŒํ•˜์ž.
SELECT A.employee_id, A.first_name, A.last_name, A.manager_id, B.first_name || ' ' B.last_name manger_name
FROM employees A, employees B
WHERE A.manager_id = B.employee_id
ORDER BY A.employee_id;

์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž

์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด๋„ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

์ข…๋ฅ˜

  • UNION

    • SELECT ๋ฌธ์˜ ์กฐํšŒ ๊ฒฐ๊ณผ์˜ ํ•ฉ์ง‘ํ•ฉ. ์ค‘๋ณต๋˜๋Š” ํ–‰์€ ํ•œ ๋ฒˆ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค. (ํ•ฉ์ง‘ํ•ฉ)

  • UNION ALL

    • SELECT ๋ฌธ์˜ ์กฐํšŒ ๊ฒฐ๊ณผ์˜ ํ•ฉ์ง‘ํ•ฉ. ์ค‘๋ณต๋˜๋Š” ํ–‰๋„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค. (ํ•ฉ์ง‘ํ•ฉ)

  • INTERSET

    • SELECT ๋ฌธ์˜ ์กฐํšŒ ๊ฒฐ๊ณผ์˜ ๊ต์ง‘ํ•ฉ. ์ค‘๋ณต๋˜๋Š” ํ–‰๋งŒ ์ถœ๋ ฅํ•œ๋‹ค. (๊ต์ง‘ํ•ฉ)

  • MINUS

    • ์ฒซ ๋ฒˆ์งธ SELECT ๋ฌธ์˜ ์กฐํšŒ ๊ฒฐ๊ณผ์—์„œ ๋‘ ๋ฒˆ์งธ ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ๋บ€๋‹ค. (์ฐจ์ง‘ํ•ฉ)

-- employees ํ…Œ์ด๋ธ”์˜ department_id ์ง‘ํ•ฉ๊ณผ departments ํ…Œ์ด๋ธ”์˜ department_id ์ง‘ํ•ฉ์„ UNION ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด ํ•ฉ์ณ ๋ณด์„ธ์š”.
โ€‹
SELECT department_id
FROM employees
UNION
SELECT department_id
FROM departments;

์„œ๋ธŒ์ฟผ๋ฆฌ

์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋Š” ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

๋ฉ”์ธ ์ฟผ๋ฆฌ์™€ ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ์—ฐ๊ฒฐ ํ˜•ํƒœ๋Š” ์—ฐ์‚ฐ์ž์— ๋”ฐ๋ผ ์˜๋ฏธ๊ฐ€ ๋‹ค๋ฅด๋‹ค.

์—ฐ์‚ฐ์ž ๊ตฌ๋ถ„

์ข…๋ฅ˜

์‚ฌ์šฉ์ฒ˜

๋‹จ์ผ ํ–‰ ์—ฐ์‚ฐ์ž

=, >, >=, <, <=, <>, !=

๋‹จ์ผ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ, ๋‹ค์ค‘ ์—ด ์„œ๋ธŒ์ฟผ๋ฆฌ

๋‹ค์ค‘ ํ–‰ ์—ฐ์‚ฐ์ž

IN, NOT IN, EXISTS, ANY, ALL

๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ, ๋‹ค์ค‘ ์—ด ์„œ๋ธŒ์ฟผ๋ฆฌ

๋‹จ์ผ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ

-- employees ํ…Œ์ด๋ธ”์˜ last_name์ด 'De Haan'์ธ ์ง์›๊ณผ salary๊ฐ€ ๋™์ผํ•œ ์ง์›์—๋Š” ๋ˆ„๊ฐ€ ์žˆ๋Š”์ง€ ๋‹จ์ผ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ์ถœ๋ ฅํ•ด๋ณด์ž.
โ€‹
SELECT *
FROM employees A
WHERE A.salary = (
                  SELECT salary
                  FROM employees
                  WHERE last_name = 'De Haan'
                  )

๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ

-- employees ํ…Œ์ด๋ธ”์—์„œ department_id๋ณ„๋กœ ๊ฐ€์žฅ ๋‚ฎ์€ salary๊ฐ€ ์–ผ๋งˆ์ธ์ง€ ์ฐพ์•„๋ณด๊ณ , ์ฐพ์•„๋‚ธ salary์— ํ•ด๋‹นํ•˜๋Š” ์ง์›์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ฐพ์•„๋ณด์ž.
โ€‹
SELECT *
FROM employees A
WHERE A.salary = IN (
                    SELECT MIN(salary) ์ตœ์ €๊ธ‰์—ฌ
                    FROM employees
                    GROUP BY department_id
                    )
ORDER BY A.salary DESC;

๋‹ค์ค‘ ์—ด ์„œ๋ธŒ์ฟผ๋ฆฌ

-- employees ํ…Œ์ด๋ธ”์—์„œ job_id๋ณ„๋กœ ๊ฐ€์žฅ ๋‚ฎ์€ salary๊ฐ€ ์–ผ๋งˆ์ธ์ง€ ์ฐพ์•„๋ณด๊ณ , ์ฐพ์•„๋‚ธ job_id๋ณ„ salary์— ํ•ด๋‹นํ•˜๋Š” ์ง์›์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋‹ค์ค‘ ์—ด ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ฐพ์•„๋ณด์ž.
โ€‹
SELECT *
FROM employees A
WHERE (A.job_id, A.salary) IN  (
                                SELECT job_id, MIN(salary) ๊ทธ๋ฃน๋ณ„๊ธ‰์—ฌ
                                FROM employees
                                GROUP BY job_id
                                )
ORDER BY A.salary DESC;

FROM ์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ: ์ธ๋ผ์ธ ๋ทฐ

FROM ์ ˆ์—์„œ๋„ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!

-- ์ง์› ์ค‘์—์„œ department_name์ด IT์ธ ์ง์›์˜ ์ •๋ณด๋ฅผ ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ์ด์šฉํ•ด์„œ ์ถœ๋ ฅํ•ด ๋ณด๋ผ.
โ€‹
SELECT *
FROM employees AS A,
                  ( 
                  SELECT department_id
                  FROM departments
                  WHERE department_name = 'IT'
                  ) AS B
WHERE A.departemnt_id = B.department_id;

ํ•™์Šต ๋‚ด์šฉ์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ์ดํ‰

๋‹ค์Œ ํ•™์Šต ๊ณ„ํš

Last updated