ํ˜ผ๊ณตS 2์ฃผ์ฐจ!๐Ÿ˜

์ž„์ฑ„์˜ยท2022๋…„ 7์›” 17์ผ
0

ํ˜ผ๊ณตS

๋ชฉ๋ก ๋ณด๊ธฐ
2/6

๊ฐ•์˜๋‚ด์šฉ

๐Ÿ’ฅ์ฝ”๋”ฉ์ˆœ์„œ๐Ÿ’ฅ

SELECT ์—ด์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”
์ด๋ฆ„
WHERE ์กฐ๊ฑด์‹
ORDER BY ์—ด_์ด๋ฆ„
LIMIT ์ˆซ์ž

๋ฐ›์€ ํŒŒ์ผ ์—ด๊ธฐ : File -> Open SQL script -> ํŒŒ์ผ ์„ ํƒ -> ์—ด๊ธฐ

DROP DATABASE IF EXISTS DB๋ช…; : ๋งŒ์•ฝ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์žˆ์œผ๋ฉด ์‚ญ์ œํ•˜๊ธฐ

์—ด์ด๋ฆ„ : ์˜์–ด๋กœ ์“ฐ๊ธฐ, _๋กœ ๊ตฌ๋ถ„, CHAR์„ ์‚ฌ์šฉํ•  ๋• ๊ธ€์ž ์ˆ˜ ์จ์ฃผ๊ธฐ

USE : ์–ด๋–ค DBMS ์•ˆ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ํ•œ ๊ฐœ๊ฐ€ ์•„๋‹ ๋•Œ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ• ์ง€ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ

SELECT * FROM ํ…Œ์ด๋ธ”๋ช…; ์‹คํ–‰์‹œ id ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌ

๋ณด๊ณ ์‹ถ์€ ํ…Œ์ด๋ธ” ๋ณด๊ธฐ

  1. USE ๋ณด๊ณ ์‹ถ์€ ํ…Œ์ด๋ธ”์ด ์žˆ๋Š” ํŒŒ์ผ๋ช…; ์„ ์„ ํƒ ํ›„ ๋ฒˆ๊ฐœํ•˜๊ณ  SELECT ์‹คํ–‰ํ•˜๊ธฐ
  2. SELECT * FROM ๋ณด๊ณ ์‹ถ์€ ํ…Œ์ด๋ธ”์ด ์žˆ๋Š” ํŒŒ์ผ๋ช….ํ…Œ์ด๋ธ”๋ช… WHERE ์—ด ์ด๋ฆ„ = '';

SELECT ์—ด1 , ์—ด2, ์—ด3 FROM ํ…Œ์ด๋ธ”๋ช…; : ์›ํ•˜๋Š” ์—ด๋งŒ ๋ณด๊ธฐ
SELECT ์—ด1 ๋ณ„๋ช…1, ์—ด2, ์—ด3 FROM ํ…Œ์ด๋ธ”๋ช…; : ์›ํ•˜๋Š” ๋ณ„๋ช…์œผ๋กœ ๋ณด๊ธฐ(๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•  ๋•Œ๋Š” "" ๋กœ ๋ฌถ์–ด์ฃผ๊ธฐ)

SELECT ์กฐ๊ฑด์„ ๋„ฃ์„ ๋•Œ <, <=, =, >, >= ๊ณผ AND, OR, BETWEEN ์‚ฌ์šฉ ๊ฐ€๋Šฅ
WHERE addr = '๊ฒฝ๊ธฐ' OR addr = '์ „๋‚จ' OR addr = '๊ฒฝ๋‚จ';
&
WHERE addr IN('๊ฒฝ๊ธฐ', '์ „๋‚จ', '๊ฒฝ๋‚จ');
= ๊ฐ™๋‹ค

LIKE : ๋ฌธ์ž ๋น„๊ต
ex1)WHERE mem_name = '์šฐ%'; : ๋ช‡๊ธ€์ž์ธ์ง€๋Š” ๋ชจ๋ฅด์ง€๋งŒ ๋งจ์•ž์— ์šฐ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ๋งŒ ์•Œ ๋•Œ
ex2)WHERE mem_name = '__ํ•‘ํฌ': ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„์— ๋ช‡๊ธ€์ž๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ ๋•Œ(underbar๋‹น 1๊ธ€์ž)

ORDER BY ์—ด๋ช… (ASC); : ์—ด๋ช…์—์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ฆฌ
ORDER BY ์—ด๋ช… DESC; : ์—ด๋ช…์—์„œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ฆฌ
์—ฌ๋Ÿฌ๊ฐœ์˜ ์กฐ๊ฑด
ORDER BY ์—ด๋ช…1, ์—ด๋ช…2; : ์—ด๋ช…1์—์„œ ๊ฐ™์„ ๋•Œ ๊ทธ ์•ˆ์—์„œ ์—ด๋ช…2๋กœ ์ •๋ ฌ

LIMIT ์ˆซ์ž : ์ •๋ ฌ ํ›„ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋งŒํผ๋งŒ ๋ณด์—ฌ์ค€๋‹ค.
LIMIT ์ˆซ์ž1, ์ˆซ์ž2 : ์ˆซ์ž1๋ฒˆํ–‰๋ถ€ํ„ฐ ์ˆซ์ž2์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋ณด์—ฌ์ค€๋‹ค.

SELECT DISTINCT ์—ด๋ช… FROM ํ…Œ์ด๋ธ”๋ช… : ์ค‘๋ณต์ผ ๋•Œ๋Š” ํ•œ ๋ฒˆ๋งŒ ๋ณด์—ฌ์ค€๋‹ค.

GROUP BY : ์„œ๋กœ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์ค€๋‹ค.
โ–ชSUM() : ํ•ฉ๊ณ„
โ–ชAVG() : ํ‰๊ท 
โ–ชMIN() : ์ตœ์†Œ๊ฐ’
โ–ชMAX() : ์ตœ๋Œ€๊ฐ’
โ–ชCOUNT() : ํ–‰์˜ ๊ฐœ์ˆ˜
โ–ชCOUNT(DISTINCT) : ํ–‰์˜ ๊ฐœ์ˆ˜(์ค‘๋ณต ์ œ์™ธ)

SELECT ์—ด๋ช…1, SUM(์—ด๋ช…2) FROM ํ…Œ์ด๋ธ”๋ช… GROUP BY ์—ด๋ช…1;
: ์—ด๋ช…1 ๋ณ„๋กœ ๋ฌถ์–ด์„œ ์—ด๋ช…2์˜ ํ•ฉ๊ณ„๋ฅผ ๋‚ธ๋‹ค.
()์•ˆ์— ์›ํ•˜๋Š” ๊ณ„์‚ฐ์‹์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. EX) price*amout

SELECT AVG(์—ด๋ช…) FROM ํ…Œ์ด๋ธ”๋ช…;
: ํ…Œ์ด๋ธ”์—์„œ ์›ํ•˜๋Š” ์—ด์— ํ‰๊ท  ๊ตฌํ•˜๊ธฐ

COUNT(*) : ์ „์ฒด๋ฅผ ์„ผ๋‹ค.
COUNT(์—ด์ด๋ฆ„) : ์—ด์ด๋ฆ„์ด NULL์ด ์•„๋‹Œ ๊ฒƒ๋งŒ ์„ผ๋‹ค.

HAVING : GROUP BY ์•ˆ์—์„œ whereํ•จ์ˆ˜๋ฅผ ์“ฐ๊ณ ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

2์ฃผ์ฐจ ๊ธฐ๋ณธ๋ฏธ์…˜๐Ÿฆ

p. 138 - ํ™•์ธ๋ฌธ์ œ 2๋ฒˆ ํ’€๊ณ  ์ธ์ฆ์ƒท

  1. ๋‹ค์Œ ์ค‘ ๋ณด๊ธฐ ์ค‘์—์„œ ๊ฐ ๋ฌธํ•ญ์˜ ๋นˆ์นธ์— ๋“ค์–ด๊ฐˆ ๊ฒƒ์„ ๊ณ ๋ฅด์„ธ์š”

    LIKE, DESC, ORDER BY, DISTINCT, ASC, AND, OR, >=, LIMIT

    โ‘  SELECT FROM member ORDER BY height;
    โ‘ก SELECT
    FROM member LIMIT 5.2;
    โ‘ข SELECT DISTINCT phone 1 FROM member

2์ฃผ์ฐจ ์„ ํƒ๋ฏธ์…˜๐Ÿฆ–

๋ฐ์ดํ„ฐ ์ž…๋ ฅ, ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋ณธ ํ˜•์‹ ์ž‘์„ฑํ•˜๊ธฐ

  1. ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
    INSERT INTO ํ…Œ์ด๋ธ”๋ช… VALUES ('');

  2. ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋ณธ ํ˜•์‹ ์ž‘์„ฑํ•˜๊ธฐ

    delete from ํ…Œ์ด๋ธ”๋ช… where ์—ด_๋ช… = '';

0๊ฐœ์˜ ๋Œ“๊ธ€