CodeGym /Java Course /All lectures for KO purposes /ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ

ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ

All lectures for KO purposes
๋ ˆ๋ฒจ 1 , ๋ ˆ์Šจ 805
์‚ฌ์šฉ ๊ฐ€๋Šฅ

4.1 UPDATE SET ๋ฌธ

ํ…Œ์ด๋ธ”์˜ ๊ธฐ์กด ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค UPDATE.

ํ•œํŽธ์œผ๋กœ ์—ฐ์‚ฐ์ž๋Š” ์—ด์— ๋Œ€ํ•œ ์ƒˆ ๊ฐ’์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— UPDATE์—ฐ์‚ฐ์ž์™€ ์œ ์‚ฌ ํ•ฉ๋‹ˆ๋‹ค. INSERT๋ฐ˜๋ฉด ์—ฐ์‚ฐ์ž์—์„œ๋Š” SELECT๋ฐ์ดํ„ฐ ๊ทธ๋ฃน์œผ๋กœ ์ฆ‰์‹œ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ค‘์š”ํ•œ! ์—ฐ์‚ฐ์ž์˜ ๋ชฉ์ ์€ UPDATEํ…Œ์ด๋ธ”์˜ ํ•œ ํ–‰์ด ์•„๋‹Œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ํ–‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž‘์€ ์‹ค์ˆ˜๋ฅผ ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์žƒ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์š”์ฒญ์— ๋Œ€ํ•œ ์ผ๋ฐ˜ ๋ณด๊ธฐ:

UPDATE table SET
  	column 1 = expression 1,
  	column 2 = expression 2,
  	column 3 = expression 3
WHERE condition;

๋ชจ๋“  ์ง์›์˜ ๊ธ‰์—ฌ๋ฅผ 100๋งŒํผ ์ธ์ƒํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

UPDATE employee SET
  	salary = salary+100;

๊ทธ๋ฆฌ๊ณ  ์ด์ œ ๊ธ‰์—ฌ๊ฐ€ 50,000 ๋ฏธ๋งŒ์ธ ๋ชจ๋“  ์‚ฌ๋žŒ์— ๋Œ€ํ•ด์„œ๋„ ๊ธ‰์—ฌ๋ฅผ 20% ์ธ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์š”์ฒญ ์˜ˆ์‹œ:

UPDATE employee SET
  	salary = salary*1.2
WHERE salary<50000;

๋ฌผ๋ก  ๊ฐ๋…์˜ ๊ธ‰์—ฌ๋ฅผ 2 ๋ฐฐ๋กœ ๋Š˜๋ฆด ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ ์—†์ด๋Š” ์–ด๋–ป๊ฒŒ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

UPDATE employee SET
  	salary = salary*2
WHERE id=4;

4.2 ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์‹œ๋‚˜๋ฆฌ์˜ค

๋™์‹œ์— ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๊ฑฐ๋‚˜ ํ•œ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋กœ ์ฑ„์›Œ์•ผ ํ•˜๊ฑฐ๋‚˜ ๋‹จ์ˆœํžˆ ์ฒซ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ณผ์ •์—์„œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ๋“  ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๊ทธ๋Ÿฌ๋ฉด ๋†€๋ผ์›€์ด ์šฐ๋ฆฌ๋ฅผ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.

์ง์› ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ์‚ฌ์šฉ์ž ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์‚ฌ์šฉ์ž์˜ ๋ ˆ๋ฒจ์„ ๋ณ€๊ฒฝํ•ด ๋ด…์‹œ๋‹ค. ๋‹จ์ˆœํ™”๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ์ž์˜ ์ผ์น˜๋ฅผ ์ด๋ฆ„์œผ๋กœ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ด ์ฟผ๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๊นŒ?

UPDATE user SET
  	level = 80,
WHERE user . name IN ( list of names from employee table );

์ด์ œ ์ง์› ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„ ๋ชฉ๋ก์„ ์ด ์ฟผ๋ฆฌ์— ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

UPDATE user SET
  	level = 80,
WHERE user.name IN (select name from employee);

ํฅ๋ฏธ๋กœ์šด! ์ด๋ฆ„์„ ์–ป๊ธฐ ์œ„ํ•œ SQL ์ฟผ๋ฆฌ ์ž์ฒด๋Š” ์ž‘์—… ์„ค๋ช…๋ณด๋‹ค ํ›จ์”ฌ ์งง์€ ๊ฒƒ์œผ๋กœ ๋ฐํ˜€์กŒ์Šต๋‹ˆ๋‹ค. SQL์„ ๋งŒ๋“  ์‚ฌ๋žŒ๋“ค์€ ๋นต์„ ํ—›๋˜์ด ๋จน์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

๋‹น์‹ ์„ ํ™”๋‚˜๊ฒŒ ๊ฐ•์š”-์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ด๋Ÿฌํ•œ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

UPDATE table 1, table 2 SET
  	column 1 = expression 1,
  	column 2 = expression 2,
  	column 3 = expression 3
WHERE table 1.id = table 2.ref_id;

์กฐ๊ธˆ ๋” ์–ด๋ ต๊ฒŒ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค. ๊ธ‰์—ฌ๊ฐ€ 50,000 ์ด์ƒ์ธ ์šฐ์ˆ˜ํ•œ ์ง์›์—๊ฒŒ๋งŒ ๋ ˆ๋ฒจ์„ 80์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์™„์ „ํ•œ ์†”๋ฃจ์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

UPDATE user, good_employees SET
  	user.level = 80
WHERE user.name = good_employees.name;

์ด๋Ÿฌํ•œ ํ…Œ์ด๋ธ” ์กฐ์ธ์€ ์‹ค์ œ๋กœ ์ด๋ฏ€๋กœ INNER JOIN๊ฒฐ๊ตญ์—๋Š” ๋‘ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”์—์„œ ์ด๋ฆ„์ด ์ผ์น˜ํ•˜๋Š” ์‚ฌ์šฉ์ž๋งŒ ๋‚จ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ๋“  ์ข‹์€ ์ง์›์„ ์„ ํƒํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ฝ”๋“œ๋ฅผ ๋” ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

SELECT * FROM employee WHERE salary >= 50000

์ด์ œ ์ตœ์ข… ์ฟผ๋ฆฌ์—์„œ ์ด ํ…Œ์ด๋ธ”์„ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.

UPDATE user,
    (SELECT * FROM employee WHERE salary >= 50000) good_emps
SET
    user.level = 80
WHERE user.name = good_emps.name;

์ง€๊ธˆ ๊ทธ๊ฒƒ๊ณผ ํ•จ๊ป˜ ์‚ด์•„๋ผ.

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION