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

์ธ์†Œ๋ฆฌยท2022๋…„ 7์›” 17์ผ
1

ํ˜ผ๊ณตS

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

2์ฃผ์ฐจ์ž…๋‹ˆ๋‹ค...๐ŸคŸ
๊ฐ‘์ž๊ธฐ ๋ชฐ์•„์น˜๋Š” ๋ฌธ๋ฒ•์˜ ํŒŒ๋„...๐ŸŒŠ
ํ•˜์ง€๋งŒ ์กฐ๋งŒ๊ฐ„ ์ •๋ณตํ•  ๋“ฏ


1. USE

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

2. AUTO_INCREMENT

์ž๋™์œผ๋กœ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

3. SELECT ๋ฌธ - ํŠน์ • ์กฐ๊ฑด๋งŒ ์กฐํšŒํ•˜๊ธฐ

๐Ÿ”ธ ์ˆœ์„œ

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

๐Ÿ”ธ ๊ธฐ๋ณธ์ ์ธ WHERE ์ ˆ

์—ด ์ด๋ฆ„ = ๊ฐ’

์—ด์˜ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฐ๊ณผ๋งŒ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

๐Ÿ”ธ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์˜ ์‚ฌ์šฉ

SELECT mem_name, height, mem_number
โ €โ €โ €โ €โ € FROM member
โ €โ €โ €โ €โ € WHERE height >= 165 AND mem_number > 6;

๊ด€๊ณ„ ์—ฐ์‚ฐ์ž >=, >์™€ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž AND๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‰๊ท  ํ‚ค๊ฐ€ 165 ์ด์ƒ์ด๋ฉด์„œ ์ธ์›๋„ 6๋ช… ์ดˆ๊ณผ์ธ ํšŒ์›์˜ ์ด๋ฆ„๊ณผ ๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž๋Š” >, <, >=, <=, = ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” AND, OR์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ”ธ BETWEEN ~ AND

SELECT mem_name, height
โ €โ €โ €โ €โ € FROM member
โ €โ €โ €โ €โ € WHERE height BETWEEN 163 AND 165;

ํ‰๊ท  ํ‚ค(height)๊ฐ€ 163 ~ 165์ธ ํšŒ์›์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ IN()

SELECT mem_name, addr
โ €โ €โ €โ €โ € FROM member
โ €โ €โ €โ €โ € WHERE addr = '๊ฒฝ๊ธฐ' OR addr = '์ „๋‚จ' OR addr = '๊ฒฝ๋‚จ';

์œ„์— ๋ฌธ์žฅ์„ IN()์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT mem_name, addr
โ €โ €โ €โ €โ € FROM member
โ €โ €โ €โ €โ € WHERE addr IN('๊ฒฝ๊ธฐ', '์ „๋‚จ', '๊ฒฝ๋‚จ');

๊ฒฝ๊ธฐ/์ „๋‚จ/๊ฒฝ๋‚จ ์ค‘ ํ•œ ๊ณณ์— ์‚ฌ๋Š” ํšŒ์›์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ LIKE

๋ฌธ์ž์—ด์˜ ์ผ๋ถ€ ๊ธ€์ž๋ฅด ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

SELECT * FROM member WHERE mem_name LIKE '์šฐ%';

์ฒซ ๊ธ€์ž๊ฐ€ '์šฐ'๋กœ ์‹œ์ž‘ํ•˜๋Š” ํšŒ์›์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. '์šฐ' ๋’ค๋Š” ๋ฌด์—‡์ด๋“ (%) ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

SELECT * FROM WHERE mem_name LIKE '__ํ•‘ํฌ';

์•ž ๋‘ ๊ธ€์ž๋Š” ์ƒ๊ด€์—†๊ณ  ๋’ค๋Š” 'ํ•‘ํฌ'์ธ ํšŒ์›์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ ORDER BY

๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

  • ASC: ์˜ค๋ฆ„์ฐจ์ˆœ
  • DESC: ๋‚ด๋ฆผ์ฐจ์ˆœ

๐Ÿ”ธ LIMIT

์ถœ๋ ฅ ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค.

LIMIT ์‹œ์ž‘, ๊ฐœ์ˆ˜

โ• LIMIT 3๋งŒ ์“ฐ๋ฉด LIMIT 0, 3๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ DISTINCT

์ค‘๋ณต๋œ ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

SELECT DISTINCT addr FROM member;

์œ„์— ๋ฌธ์žฅ์€ ์ค‘๋ณต๋œ ์ง€์—ญ์„ 1๊ฐœ๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•œ ํ›„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ GROUP BY

๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์ค๋‹ˆ๋‹ค.

๐Ÿ”ธ HAVING

์ง‘๊ณ„ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์กฐ๊ฑด์„ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค.
โ• HAVING ์ ˆ์€ ๊ผญ GROUP BY ์ ˆ ๋‹ค์Œ์— ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

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

SELECT mem_name, height FROM member
โ €โ €โ €โ €โ € WHERE height > (SELECT height FROM member WHERE mem_name = '์—์ดํ•‘ํฌ');

๊ด„ํ˜ธ ์•ˆ์˜ SELECT ๋ฌธ ๊ฒฐ๊ณผ๊ฐ€ 164์ด๋ฏ€๋กœ ํ‰๊ท  ํ‚ค๊ฐ€ 164๋ณด๋‹ค ํฐ ํšŒ์›์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

5. ์ง‘๊ณ„ ํ•จ์ˆ˜

ํ•จ์ˆ˜๋ช…์„ค๋ช…
SUM()ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
AVG()ํ‰๊ท ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
MIN()์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
MAX()์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
COUNT()ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์…‰๋‹ˆ๋‹ค.
COUNT(DISTINCT)ํ–‰์˜ ๊ฐœ๋ฃจ๋ฅผ ์…‰๋‹ˆ๋‹ค(์ค‘๋ณต์€ 1๊ฐœ๋งŒ ์ธ์ •).

6. INSERT

๐Ÿ”ธ ๊ธฐ๋ณธ์ ์ธ ํ˜•์‹

INSERT INTO ํ…Œ์ด๋ธ” [(์—ด1, ์—ด2, ...)] VALUES (๊ฐ’1, ๊ฐ’2, ...)


๐Ÿ”ธ AUTO_INCREMENT

์—ด์„ ์ •์˜ํ•  ๋•Œ 1๋ถ€ํ„ฐ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ’์„ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค.
โ• AUTO_INCREMENT๋กœ ์ง€์ •ํ•˜๋Š” ์—ด์€ ๊ผญ PRIMARY KEY๋กœ ์ง€์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ INSERT INTO ~ SELECT

๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์ด๋ฏธ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋˜์–ด ์žˆ๋‹ค๋ฉด ๊ทธ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ํ•œ ๋ฒˆ์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

INSERT INTO ํ…Œ์ด๋ธ” ์ด๋ฆ„ (์—ด ์ด๋ฆ„1, ์—ด ์ด๋ฆ„2, ...)
โ €โ €โ €โ €โ €SELECT ๋ฌธโ €โ €;

โ• SELECT ๋ฌธ์˜ ์—ด ๊ฐœ์ˆ˜๋Š” INSERTํ•  ํ…Œ์ด๋ธ”์˜ ์—ด ๊ฐœ์ˆ˜์™€ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

7. UPDATE

๊ธฐ์กด์— ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ๊ฐ’์„ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ ๊ธฐ๋ณธ์ ์ธ ํ˜•์‹

UPDATE ํ…Œ์ด๋ธ” ์ด๋ฆ„
โ €โ €โ €โ €โ €SET ์—ด1 = ๊ฐ’1, ์—ด2 = ๊ฐ’2, ...
โ €โ €โ €โ €โ €WHERE ์กฐ๊ฑดโ €โ €;

โ• WHERE๊ฐ€ ์—†์œผ๋ฉด ๋ชจ๋“  ํ–‰์˜ ๋ฐ์ดํ„ฐ ๊ฐ’์ด ์ˆ˜์ •๋ฉ๋‹ˆ๋‹ค.

8. DELETE

ํ–‰ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ ๊ธฐ๋ณธ์ ์ธ ํ˜•์‹

DELETE FROM ํ…Œ์ด๋ธ” ์ด๋ฆ„ WHERE ์กฐ๊ฑด;

โ• UPDATE์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ WHERE ์ ˆ์ด ์ƒ๋žต๋˜๋ฉด ์ „์ฒด ํ–‰ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
โ• ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์‚ญ์ œํ•  ๋•Œ๋Š” DELETE๋ณด๋‹ค TRUNCATE๊ฐ€ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

๋ฏธ์…˜๐Ÿ”โœจ

ํ™•์ธ๋ฌธ์ œ 2๋ฒˆ ํ’€๊ณ  ์ธ์ฆ์ƒท!

profile
์ฝ”๋”ฉ์ด๋ผ๋Š” ๊ฐ์˜ฅ์— ๊ฐ‡ํ˜€ ์‚ฌ๋Š” ์‚ถ

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