[STARTERS 4๊ธฐ TIL] #8์ผ์ฐจ(23.02.15)


๐Ÿ“š ์˜ค๋Š˜ ๋‚ด๊ฐ€ ๋ฐฐ์šด ๊ฒƒ

๐Ÿ“ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐœ์š” 

 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ž€?

- ๋ชฉ์ ์— ๋งž๋„๋ก ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„๋‘๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•

- ๋ฐ์ดํ„ฐ ๋ชจ์Œ์„ ๊ด€๋ฆฌ ๋ฐ ์ด์šฉํ•˜๋„๋ก ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์„ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ๊ณ ๋„ ๋ถ€๋ฆ„

๐Ÿ“ DBMS : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ด€๋ฆฌ/์šด์˜ํ•˜๋Š” ์—ญํ• 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ข…๋ฅ˜
๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
๊ฐ์ฒด์ง€ํ–ฅ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
๊ฐ์ฒด๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
NoSQL

 

๐Ÿ“ DB์™€ Schema 

Schema
๋ฌผ๋ฆฌ์  ๊ณต๊ฐ„ DBMS์— ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„
๋…ผ๋ฆฌ์  ๊ณต๊ฐ„ ์ €์žฅ ๊ณต๊ฐ„์˜ ์˜์—ญ

 

๐Ÿ“SELECT ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ 

  • SELECT ๋ฌธ์˜ ๋…ผ๋ฆฌ์  ์ฒ˜๋ฆฌ ์ˆœ์„œ
SELECT a.job                     ------------------------------- 5
		 , COUNT(a.empno)     AS cnt_emp
     , SUM(a.sal)         AS sum_salary
     , MAX(a.hiredate)    AS latest_hiredate
  FROM emp a                     ------------------------------- 1
 WHERE a.sal > 1000              ------------------------------- 2
GROUP BY a.job                   ------------------------------- 3
  HAVING COUNT(a.empno) >= 3     ------------------------------- 4
ORDER BY a.job;                  ------------------------------- 6

 

๐Ÿ“ SELECT ์ ˆ๊ณผ FROM ์ ˆ

  • SELECT ๋ฌธ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ SELECT ์ ˆ๊ณผ FROM ์ ˆ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
SELECT ์นผ๋Ÿผ, ์นผ๋Ÿผ, ...
  FROM ํ…Œ์ด๋ธ”;
  • SELECT ์ ˆ์— ์• ์Šคํ„ฐ๋ฆฌ์Šคํฌ(*)๋ฅผ ๊ธฐ์ˆ ํ•˜๋ฉด ํ…Œ์ด๋ธ”์˜ ์ „์ฒด ์นผ๋Ÿผ์ด ์กฐํšŒ๋œ๋‹ค.
SELECT *
  FROM ํ…Œ์ด๋ธ”;
  • SELECT ์ ˆ์— DISTINCT ํ‚ค์›Œ๋“œ๋ฅผ ๊ธฐ์ˆ ํ•˜๋ฉด ์ค‘๋ณต ๊ฐ’์ด ์ œ๊ฑฐ๋œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

      - ๊ฐ’์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ !

SELECT DISRINCT job
  FROM emp;
  • SELECT ์ ˆ์˜ ์นผ๋Ÿผ์— ๋ณ„์นญ(Column Alias)์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

      - ์นผ๋Ÿผ ๋ณ„์นญ์€ AS ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ (์ƒ๋žต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ ์‚ฌ์šฉ)

      - (”)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ„์นญ์— ๊ณต๋ฐฑ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž ํฌํ•จ, ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ๊ฐ€๋Šฅ

SELECT deptno AS dno, dname AS "๋ถ€์„œ ๋ช…", loc "LOC ation!@"
  FROM dept ;
  • FROM ์ ˆ์˜ ํ…Œ์ด๋ธ”์—๋„ ๋ณ„์นญ(Column Alias)์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

      - ํ…Œ์ด๋ธ”๋ช… ๋’ค์— ํ•œ ์นธ ๋„์šฐ๊ณ  ๋ฐ”๋กœ ๊ธฐ์ˆ 

      - ์นผ๋Ÿผ ์•ž์— ํ…Œ์ด๋ธ” ๋ณ„์นญ์„ ์‚ฌ์šฉํ•˜๋ฉด, ํ•ด๋‹น ์นผ๋Ÿผ์ด ์–ด๋–ค ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ์ธ์ง€ ๋ช…์‹œ ๊ฐ€๋Šฅ

SELECT a.deptno, a.dname
  FROM dept a ;
  • SELECT ์ ˆ์— ์นผ๋Ÿผ๊ณผ ํ•จ๊ป˜ ๋ฆฌํ„ฐ๋Ÿด(Literal)์„ ๊ธฐ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค.

      - ๋ฆฌํ„ฐ๋Ÿด : ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฐ’

      - (‘)์œผ๋กœ ๊ฐ์‹ธ์„œ ๊ธฐ์ˆ , ๋‚ ์งœ ๋ฆฌํ„ฐ๋Ÿด์€ DATE ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ ๊ธฐ์ˆ 

SELECT a.deptno
     , 'data' AS char_literal, 99 AS num_literal, DATE '2018-06-05' AS date_literal
  FROM dept a ;

[๊ฒฐ๊ณผ]

DEPTNO CHAR_LITERAL NUM_LITERAL DATE_LITERAL
10 data 99 2018/06/05 00:00:00
20 data 99 2018/06/05 00:00:00
  • SELECT ์ ˆ์˜ ์นผ๋Ÿผ์ด๋‚˜ ๋ฆฌํ„ฐ๋Ÿด์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

      1. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž : ์ˆซ์ž ๊ฐ’, ๋‚ ์งœ ๊ฐ’

        - ์ˆซ์ž & ์ˆซ์ž : ์‚ฌ์น™์—ฐ์‚ฐ(*, /, +, -)

        - ๋‚ ์งœ & ์ˆซ์ž : ๋”ํ•˜๊ธฐ(+), ๋นผ๊ธฐ(-)

        - ๋‚ ์งœ & ๋‚ ์งœ : ๋นผ๊ธฐ(-)

      2. ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž(||) : ๋ฌธ์ž ๊ฐ’

SELECT a.sal
     , (a.sal + 1000) / 2                AS new_sal
     , a.hiredate
     , a.hiredate + 1                    AS hdate_plus_1day
     , a.hiredate - 1/24/60/60           AS hdate_minus_1sec
     , a.hiredate - DATE '1981-02-01'    AS diff_days
  FROM emp a ;

[๊ฒฐ๊ณผ]

SAL NEW_SAL HIREDATE DATE_PLUS_1DAY HDATE_MINUS_1SEC DIFF_DAYS
800 900 1980/12/17 00:00:00 1980/12/18 00:00:00 1980/12/16 23:59:59 -46
  • NULL

      - NULL์„ ํฌํ•จํ•œ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋Š” ํ•ญ์ƒ NULL !

๐Ÿ“ WHERE ์ ˆ 

  • ์กฐํšŒ๋˜๋Š” ๋ฐ์ดํ„ฐ(ํ–‰)๋ฅผ ์ œํ•œ
  • ์กฐ๊ฑด์ด TRUE์ธ ๊ฐ’๋งŒ ๊ฒฐ๊ณผ์— ํฌํ•จ
  • ๋น„๊ต ์กฐ๊ฑด
A = B A๊ฐ€ B์™€ ๊ฐ™๋‹ค A <> B A๊ฐ€ B์™€ ๊ฐ™์ง€ ์•Š๋‹ค
A > B A๊ฐ€ B๋ณด๋‹ค ํฌ๋‹ค A < B A๊ฐ€ B๋ณด๋‹ค ์ž‘๋‹ค
A >= B A๊ฐ€ B๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค A <= B A๊ฐ€ B๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค
  • ๋…ผ๋ฆฌ ์กฐ๊ฑด
(์กฐ๊ฑด1) AND (์กฐ๊ฑด2) ๋‘ ์กฐ๊ฑด ๋ชจ๋‘ TRUE → TRUE
(์กฐ๊ฑด1) OR (์กฐ๊ฑด2) ๋‘ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ผ๋„ TRUE → TRUE

AND ๋…ผ๋ฆฌ ์กฐ๊ฑด์ด OR ๋…ผ๋ฆฌ ์กฐ๊ฑด๋ณด๋‹ค ๋จผ์ € ํ‰๊ฐ€๋จ !!

๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…์‹œ์ ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ์ง€์ • ํ•„์š”

NOT (์กฐ๊ฑด) ๋ฐ˜๋Œ€๋กœ !
  • IN ์กฐ๊ฑด
A IN (b1, b2, …) A๊ฐ€ IN List(b1, b2, …) ์ค‘ ์–ด๋–ค ๊ฐ’๊ณผ ๊ฐ™๋‹ค
A NOT IN (b1, b2, …) A๊ฐ€ IN List(b1, b2, …) ์˜ ๋ชจ๋“  ๊ฐ’๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค

NOT IN ์กฐ๊ฑด ์‚ฌ์šฉ ์‹œ, IN List์— NULL์ด ํฌํ•จ๋˜์ง€ ์•Š์•„์•ผ ํ•จ

 

  • BETWEEN ์กฐ๊ฑด
A BETWEEN b1 AND b2 b1, b2 ๊ฐ’ ํฌํ•จ
A NOT BETWEEN b1 AND b2 b1, b2 ๊ฐ’ ๋ฏธํฌํ•จ
  • LIKE ์กฐ๊ฑด
A LIKE (๋ฌธ์ž์—ด ํŒจํ„ด) A๊ฐ€ (๋ฌธ์ž์—ด ํŒจํ„ด)๊ณผ ์ผ์น˜ํ•œ๋‹ค
A NOT LIKE (๋ฌธ์ž์—ด ํŒจํ„ด) A๊ฐ€ (๋ฌธ์ž์—ด ํŒจํ„ด)๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค

์–‘์ชฝ ๋ชจ๋‘ ๋ฌธ์ž ๊ฐ’์ด์–ด์•ผ ํ•จ

% 0๊ฐœ ์ด์ƒ์˜ ๋ชจ๋“  ๋ฌธ์ž์™€ ์ผ์น˜
_ 1๊ฐœ์˜ ๋ชจ๋“  ๋ฌธ์ž์™€ ์ผ์น˜

 


๐Ÿ’ญ ์˜ค๋Š˜ ๊ธฐ์–ตํ•ด์•ผ ํ•  ๋‚ด์šฉ

โ‘  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์Šคํ‚ค๋งˆ, ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ด๋ธ” ๊ฐ„ ๊ด€๊ณ„

 ์ด์ „์—๋Š” ๊ทธ๋ƒฅ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ sql์„ ์ด์šฉํ•˜์—ฌ ๋ถ„์„ํ•˜๋Š” ๊ณผ์ •๋งŒ ์ง„ํ–‰ํ•ด๋ณด์•„์„œ,

 ์ •ํ™•ํžˆ sql์ด ์–ด๋–ค ๊ตฌ์กฐ์ธ์ง€ ๊ทธ๋ฆฌ๊ณ  ์Šคํ‚ค๋งˆ์™€ ํ…Œ์ด๋ธ”์ด ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š”์ง€์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ์ „ํ˜€ ์—†์—ˆ๋‹ค.

 ์˜ค๋Š˜ ๊ฐ•์˜๋ฅผ ํ†ตํ•ด sql์— ๋Œ€ํ•œ ๊ทผ๋ณธ์  ์ดํ•ด๊ฐ€ ์ด๋ฃจ์–ด์กŒ๋‹ค !

 

 

โœ๐Ÿป ํšŒ๊ณ 

๋“œ๋””์–ด ํŒŒ์ด์ฌ์ด ๋๋‚˜๊ณ  sql์— ๋Œ€ํ•œ ๊ฐ•์˜๊ฐ€ ์‹œ์ž‘๋˜์—ˆ๋‹ค !

๊ฐ•์˜ ์ค‘ ์˜ค๋ฅ˜๋กœ ์ธํ•ด ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜๋Š” ๋ถ€๋ถ„๋„ ์žˆ์—ˆ์ง€๋งŒ ํŒ€์›๋ถ„๋“ค์˜ ๋„์›€์œผ๋กœ ์ž˜ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค :)

๋‚จ์€ sql ๊ฐ•์˜๋„ ํ™”์ดํŒ…!!!!