[Database] MySQL Update & Delete

MySQL Update & Delete

MySQL๋กœ ์ž๋ฃŒ๋ฅผ ์ˆ˜์ •(Update)ํ•˜๊ณ  ์‚ญ์ œ(Delte)ํ•ด๋ณด์ž.

Update

show tables;

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ use ํ•œ ๋’ค ์‚ฌ์šฉํ•  ํ…Œ์ด๋ธ”์„ ํ™•์ธํ•œ๋‹ค.

1

SELECT * from testTable;

testTable์ด ๊ฐ–๊ณ  ์žˆ๋Š” ์ž๋ฃŒ๋“ค์„ ์กฐํšŒํ•œ๋‹ค.

2

Update ๋ช…๋ น์–ด

UPDATE โ€œํ…Œ์ด๋ธ” ์ด๋ฆ„โ€ SET โ€œcolumn1 ์ด๋ฆ„โ€ = โ€œcolumn1 ๊ฐ’โ€, โ€œcolumn2 ์ด๋ฆ„โ€ = โ€œcolumn2 ๊ฐ’โ€ โ€ฆ where โ€œcolumn ์ด๋ฆ„โ€ = โ€œcolumn ๊ฐ’โ€;

์œ„์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋กœ ๋ฐ์ดํ„ฐ์˜ ์ž๋ฃŒ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด๋•Œ, where๋กœ ํŠน์ • ๊ฐ’์„ ์ง€์ •ํ•ด ์ฃผ์ง€ ์•Š์œผ๋ฉด ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ๋ณ€ํ•œ๋‹ค.

UPDATE testTable SET content=โ€™hello world!โ€™, title=โ€™titleโ€™ where id = 1;

์ด๋ ‡๊ฒŒ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆฌ๋ฉด,

3

์œ„์™€ ๊ฐ™์ด ์ž๋ฃŒ๊ฐ€ ๋ณ€ํ•œ๋‹ค.

Delete

์‚ญ์ œ๋Š” ์ˆ˜์ •๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋”ฐ๋กœ column์— ๋Œ€ํ•œ value๋ฅผ ์ •ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๋‹จํ•˜๋‹ค.
์‚ญ์ œ๋„ ๋ฐ˜๋“œ์‹œ where๋กœ ํŠน์ • ๊ฐ’์„ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

Delete from โ€œํ…Œ์ด๋ธ” ์ด๋ฆ„โ€ where โ€œcolumn ์ด๋ฆ„โ€ = โ€œcolumn ๊ฐ’โ€;

์œ„์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋กœ ๋ฐ์ดํ„ฐ์˜ ์ž๋ฃŒ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

Delete testTable where id=4;

testTable์—์„œ id=4์ธ ์ž๋ฃŒ๋ฅผ ์ง€์šฐ๋Š” ๋ช…๋ น์–ด์ด๋‹ค.

4

์œ„์™€ ๊ฐ™์ด 4๋ฒˆ์ด ์ง€์›Œ์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

CRUD

์•ž์„  ๊ฒŒ์‹œ๊ธ€๊นŒ์ง€ ํ•ด์„œ MySQL์„ ์ด์šฉํ•˜์—ฌ CRUD ์กฐ์ž‘ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜๋‹ค.
CRUD๋Š” C(Create) R(Read) U(Update) D(Delete)์˜ ์•ฝ์ž์ด๋ฉฐ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ธฐ๋ณธ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.