Bibi's DevLog ๐Ÿค“๐ŸŽ

[MySQL] CRUD - INSERT, SELECT, UPDATE, DELETE / JOIN ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Database

[MySQL] CRUD - INSERT, SELECT, UPDATE, DELETE / JOIN

๋น„๋น„ bibi 2021. 1. 28. 16:01

์ด ๊ธ€์€ ์ธํ”„๋Ÿฐ ์ƒํ™œ์ฝ”๋”ฉ-DATABASE 1&2๋ฅผ ๋ณด๊ณ  ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

MySQL CRUD

CRUD = ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ฃผ์š” 4๊ฐ€์ง€ ๊ธฐ๋Šฅ.

โ†’
Create ๋ฐ์ดํ„ฐ ์ƒ์„ฑ,
Read ๋ฐ์ดํ„ฐ ์กฐํšŒ,
Update ๋ฐ์ดํ„ฐ ์ˆ˜์ •,
Delete ๋ฐ์ดํ„ฐ ์‚ญ์ œ

์ด ์ค‘์—์„œ๋„ DB๋ผ๋ฉด ๋ฐ˜๋“œ์‹œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ธฐ๋Šฅ์€? Create์™€ Read.
์–ด๋–ค ๋ถ„์•ผ(ํšŒ๊ณ„, ์—ญ์‚ฌ ๋“ฑ)์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜์ •๊ณผ ์‚ญ์ œ ๊ธฐ๋Šฅ์„ ์ „ํ˜€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ๋„ ํ•œ๋‹ค.

โ€ป DESC ํ…Œ์ด๋ธ”์ด๋ฆ„; : ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ช…๋ น์–ด.

Create : SQL์˜ INSERT ๊ตฌ๋ฌธ

ํ‘œ์— ํ–‰์„ '์‚ฝ์ž…'ํ•œ๋‹ค๋Š” ์ ์—์„œ INSERT๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
auto_increment ์ธ ๊ฐ’๋งŒ ์ œ์™ธํ•˜๊ณ  ํ•ญ๋ชฉ๋“ค์„ ์ ์–ด๋„ฃ์œผ๋ฉด ๋œ๋‹ค.
โ†’ INSERT INTO ํ…Œ์ด๋ธ”์ด๋ฆ„ (์—ด1์ด๋ฆ„,์—ด2์ด๋ฆ„...) VALUES('์—ด1๊ฐ’','์—ด2๊ฐ’',...);

Read : SQL์˜ SELECT๊ตฌ๋ฌธ

๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” SQL๋ช…๋ น์–ด.
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค= ์ตœ๋Œ€ ์–ต ๋‹จ์œ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ.
๋”ฐ๋ผ์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๊ณจ๋ผ์„œ(select) ์กฐํšŒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”.
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ž˜ ๋‹ค๋ฃฌ๋‹ค = select๋ฌธ์„ ์ž˜ ๋‹ค๋ฃฌ๋‹ค.
SELECT๋ฌธ์€ ์กฐ๊ฑด์ด ๋ณต์žกํ•ด์งˆ์ˆ˜๋ก ์‹ฌ์˜คํ•ด์ง€๋ฏ€๋กœ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ๊ทธ๋•Œ๊ทธ๋•Œ ํ•„์š”ํ•œ ๋ช…๋ น์–ด&๋ช…๋ น์–ด ์ˆœ์„œ๋ฅผ ์ฐพ์•„ ์จ์•ผ ํ•œ๋‹ค.

SELECT~ FROM~ ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
FROM ๋’ค์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ํ‘œ ์ด๋ฆ„์ด ์˜จ๋‹ค.
SELECT ์™€ FROM ์‚ฌ์ด = Projection = ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์€ column๋ชฉ๋ก์ด ์˜จ๋‹ค. (* = ์ „๋ถ€)

  1. ํŠน์ • ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ.
    โ†’ SELECT * FROM ํ…Œ์ด๋ธ”์ด๋ฆ„;
  2. ํŠน์ • ํ…Œ์ด๋ธ”์˜ ํŠน์ • column๋“ค๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ.
    โ†’ SELECT column1์ด๋ฆ„, column2์ด๋ฆ„,.. FROM ํ…Œ์ด๋ธ”์ด๋ฆ„;
  3. ํŠน์ • ํ…Œ์ด๋ธ”์˜ ํŠน์ • column์˜ ํŠน์ • ์กฐ๊ฑด ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ.
    โ†’ SELECT * FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ํŠน์ •column๋ช…=์กฐ๊ฑด;
  4. ํŠน์ • ํ…Œ์ด๋ธ” ํŠน์ • column ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ.
    โ€ปascending order = ์˜ค๋ฆ„์ฐจ์ˆœ = ์ •๋ฐฉํ–ฅ ์ •๋ ฌ. 123, ๊ฐ€๋‚˜๋‹ค, ABC..
    descending order = ๋‚ด๋ฆผ์ฐจ์ˆœ = ์—ญ๋ฐฉํ–ฅ ์ •๋ ฌ. 987, ํ•˜ํŒŒํƒ€, ZYX...
    ascending โ†’ SELECT * FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ ORDER BY ํŠน์ •column๋ช… ASC;
    (ASC ๋Š” ์ƒ๋žต๊ฐ€๋Šฅ-๊ธฐ๋ณธ์ •๋ ฌ)
    descending โ†’ SELECT * FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ ORDER BY ํŠน์ •column๋ช… DESC;

Update : SQL์˜ UPDATE ๊ตฌ๋ฌธ

โ†’ UPDATE ํ…Œ์ด๋ธ”์ด๋ฆ„ SET column๋ช…=์ˆ˜์ •ํ• ๊ฐ’, column๋ช…=์ˆ˜์ •ํ• ๊ฐ’.. WHERE ์ˆ˜์ •๋Œ€์ƒ;

์ˆ˜์ •๋Œ€์ƒ์€ ์ฃผ๋กœ id=__ ์™€ ๊ฐ™์ด ์‹๋ณ„์ž๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. (์ˆ˜์ •ํ•  ๋ฐ์ดํ„ฐ์˜ uniqueํ•œ ๊ฐ’)
โš ๏ธUPDATE ๊ตฌ๋ฌธ์—์„œ WHERE ~ ๋ฅผ ๋น ๋œจ๋ฆฌ๋ฉด ์žฌ์•™์ด ์˜จ๋‹ค.. ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์ด ๋ฐ”๋€” ์ˆ˜๋„ ์žˆ๋‹ค.

Delete : SQL์˜ DELETE ๊ตฌ๋ฌธ

โ†’ DELETE FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์‚ญ์ œ๋Œ€์ƒ;
์‚ญ์ œ๋Œ€์ƒ์€ ์ฃผ๋กœ id=__ ์™€ ๊ฐ™์ด ์‹๋ณ„์ž๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. (์‚ญ์ œํ•  ๋ฐ์ดํ„ฐ์˜ uniqueํ•œ ๊ฐ’)
โš ๏ธDELETE ๊ตฌ๋ฌธ์—์„œ WHERE ~ ๋ฅผ ๋น ๋œจ๋ฆฌ๋ฉด ์žฌ์•™์ด ์˜จ๋‹ค.. ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ์‚ญ์ œ๋  ์ˆ˜๋„ ์žˆ๋‹ค.


๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ•„์š”์„ฑ

๊ด€๊ณ„ํ˜•๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์™œ ํ•„์š”ํ•œ๊ฐ€?

๋ฐ์ดํ„ฐ๋“ค์€ ์ค‘๋ณต์ด ๋งŽ์„์ˆ˜๋ก ์ข‹์ง€ ์•Š๋‹ค. (์ค‘๋ณต์˜ ์•…์ทจ)
selectํ•˜๊ธฐ ์–ด๋ ค์›Œ์ ธ์„œ ๋ฐ์ดํ„ฐ ์กฐํšŒ, ๊ด€๋ฆฌ(์œ ์ง€๋ณด์ˆ˜)๊ฐ€ ์–ด๋ ค์›Œ์ง€๊ธฐ ๋–„๋ฌธ.

๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐœ์„ ํ•œ๋‹ค.

ํ‘œ ์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ํ‘œtopic์€ author, profileํ•ญ๋ชฉ์˜ ์ค‘๋ณต์ด ๋„ˆ๋ฌด ๋งŽ๋‹ค.
๊ทธ๋ž˜์„œ ์•„๋ž˜์ฒ˜๋Ÿผ author, topic ์˜ ๋‘ ๊ฐœ์˜ ํ‘œ๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด..
โ†’ egoing ์˜ ์ด๋ฆ„์ด ์ด๊ณ ์ž‰์œผ๋กœ ๋ฐ”๋€Œ์–ด๋„ authorํ‘œ์—์„œ๋งŒ ๋ฐ”๊พธ๋ฉด ๋˜์–ด ํŽธ๋ฆฌ.
โ†’ ๊ธฐ์กด ์ €์ž์™€ ์ด๋ฆ„, ํ”„๋กœํ•„์ด ๊ฐ™์€ ๋‹ค๋ฅธ ์ž‘๊ฐ€๊ฐ€ ์ถ”๊ฐ€๋˜์–ด๋„ ์‹๋ณ„ ๊ฐ€๋Šฅ.
(์ด๋ฆ„์ด egoing, ํ”„๋กœํ•„์ด developer์ธ ๋™๋ช…์ด์ธ์ด ์ถ”๊ฐ€๋˜์–ด๋„ author id๋Š” ๋‹ค๋ฅผ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ)

ํ•˜์ง€๋งŒ ๋‹จ์ ๋„ ์žˆ๋‹ค.
์œ„์˜ ํ‘œ๋Š” ๋” ์ง๊ด€์ ์ด๋‹ค (ํ•˜๋‚˜์˜ ํ‘œ๋งŒ ๋ณด๋ฉด ๋œ๋‹ค)
์•„๋ž˜์˜ ๋‘ ํ‘œ๋Š” ์ฐธ์กฐํ‘œ๊ฐ€ ํ•„์š”ํ•˜๊ณ , ๋งค๋ฒˆ ๋น„๊ตํ•ด์„œ ๋ด์•ผ ํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ๋‹ค.

โ‡’ ์ด ๋ถˆํŽธํ•จ์„ ํ•ด๊ฒฐํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋‹ค.
" ์ €์žฅ์€ ๋ถ„์‚ฐํ•ด์„œ, ๋ณด์—ฌ์ค„ ๋• ํ•ฉ์ณ์„œ ๋ณด์—ฌ์ค€๋‹ค ".

ํ…Œ์ด๋ธ” ๋ถ„๋ฆฌํ•˜๊ธฐ

์œ„์˜ ์˜ˆ์ œ์ฒ˜๋Ÿผ ํฐ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์„ ๋‘˜๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ์—ฐ์Šต์„ ๋จผ์ € ํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค.

ํฐ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์—์„œ ์„œ๋กœ ๊ด€๊ณ„์žˆ๋Š” ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด..
ํฐ ํ…Œ์ด๋ธ” : id, title, description, created, author, profile ์˜ ์—ด์ด ์žˆ์Œ

โ†’๊ฐ€์žฅ ๊ด€๊ณ„์„ฑ ๋†’์•„์„œ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ข‹์€ author, profile ์„ ๋ถ„๋ฆฌํ•œ๋‹ค.

topic : id, title, description, created, authorโ†’author_id
author : id, author, profile
(์‹ค์Šต ๋ฐ์ดํ„ฐ ์ฝ”๋“œ๋Š” ์ธ๊ฐ• ์ˆ˜์—…์ž๋ฃŒ์— ์žˆ์Œ!)

JOIN - ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ฝƒ

JOIN = ๊ฒฐํ•ฉ.
๋‘ ํ…Œ์ด๋ธ”์„ joinํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฒฐํ•ฉํ•  '๊ธฐ์ค€'์ด ํ•„์š”ํ•˜๋‹ค.
์ด '๊ธฐ์ค€'์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ฒฐํ•ฉ๊ณ ๋ฆฌ(์—ฐ๊ด€์ ) ์— ํ•ด๋‹นํ•œ๋‹ค.
์˜ˆ๋ฅผ๋“ค์–ด, ์œ„ ์˜ˆ์ œ์˜ topic-author_id ์™€ author-id ๊ฐ€ ์„œ๋กœ ์—ฐ๊ด€๋˜์–ด ์žˆ์œผ๋ฉฐ, JOIN ๋ช…๋ น์–ด๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ด๋ฅผ ์ฐธ์กฐํ•ด ๋ช…๋ นํ•  ์ˆ˜ ์žˆ๋‹ค.
โ†’ SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.id;
์ฆ‰ SELECT * FROM ํ…Œ์ด๋ธ”1 LEFT JOIN ํ…Œ์ด๋ธ”2 ON ๊ฒฐํ•ฉ๊ธฐ์ค€;.

๋‹จ์ˆœํžˆ ๋‘ ํ…Œ์ด๋ธ”๋งŒ ์—ฐ๊ด€์ง“๊ณ  ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ด€์ง“๊ณ  ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด๊ฒƒ์ด ๋ฌด๊ถ๋ฌด์ง„ํ•˜๊ฒŒ ์‘์šฉ ๊ฐ€๋Šฅํ•œ '๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค'์˜ ํž˜!
JOIN ์€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฐ€์žฅ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‹ต๊ฒŒ ํ•˜๋Š” ๊ฝƒ์ด๋‹ค.

JOIN ์„ ์ดํ•ดํ•˜๋ฉด ๋น„๋กœ์†Œ '๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค'์˜ ์˜๋ฏธ๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

โ€ป** AS ## โ†’ ** ๋ฅผ ## ๋กœ ๋ฐ”๊พผ๋‹ค.