๐Ÿ’พ DML - INSERT๋ฌธ, UPDATE๋ฌธ, DELETE๋ฌธ

suRanยท2022๋…„ 7์›” 14์ผ
1

๐Ÿ’พ MySQL

๋ชฉ๋ก ๋ณด๊ธฐ
3/9
post-thumbnail

๋ณธ ํฌ์ŠคํŒ…์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฏธ๋‹ˆ ๋ฐ๋ธŒ ์ฝ”์Šค๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ
ํ•™์Šต์„ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์ž‘์„ฑ๋œ ๊ธ€์ž…๋‹ˆ๋‹ค.

1. ์‹ค์Šต์— ํ•„์š”ํ•œ ํ…Œ์ด๋ธ” ์ •์˜

๋จผ์ €, CREATE๋ฌธ์œผ๋กœ
์‹ค์Šต์— ํ•„์š”ํ•œ prod.vitalํ…Œ์ด๋ธ”๊ณผ prod.alertํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค.


CREATE TABLE prod.vital(
	user_id int NOT NULL,
    vital_id int,
    date timestamp NOT NULL,
    weight int NOT NULL,
    primary key(vital_id)
);
  • PK๋Š” vital_id
CREATE TABLE prod.alert (
	alert_id int,
    vital_id int,
    alert_type varchar(32),
    date timestamp,
    user_id int,
    primary key(alert_id)
);
  • PK๋Š” alert_id



2. INSERT

INSERT๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ช…๋ น์–ด์ด๋‹ค.

INSERT๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์ฒด์ ์ธ ์˜ˆ๋กœ ์‹ ๊ทœํšŒ์› ์ •๋ณด ์ €์žฅ์ด ์žˆ๋‹ค.

์‚ฌ์ดํŠธ์— ์‹ ๊ทœ ํšŒ์›๊ฐ€์ž…์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ,
๊ฐœ๋ฐœ์ž๋Š” ์‹ ๊ทœ ํšŒ์›์ •๋ณด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด
INSERT ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.


INSERT INTO prod.vital(user_id, vital_id, date, weight) VALUES(100, 1, '2020-01-01', 75);
INSERT INTO prod.vital(user_id, vital_id, date, weight) VALUES(100, 1, '2020-01-02', 78);
INSERT INTO prod.vital(user_id, vital_id, date, weight) VALUES(100, 1, '2020-01-01', 90);
INSERT INTO prod.vital(user_id, vital_id, date, weight) VALUES(100, 1, '2020-01-02', 95);
INSERT INTO prod.vital(user_id, vital_id, date, weight) VALUES(999, 5, '2020-01-02', -1);  -- weight ํ•„๋“œ์— ์Œ์ˆ˜๊ฐ’์ด ๋“ค์–ด๊ฐ”๋‹ค
INSERT INTO prod.vital(user_id, vital_id, date, weight) VALUES(999, 5, '2020-01-02', 10);  -- primery key๋กœ ์ง€์ •๋œ ํ•„๋“œ์˜ ๊ฐ’์ด ์ค‘๋ณต๋œ๋‹ค.
  • INTSERT์˜ ํ˜•์‹์€ ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค. ์ž…๋ ฅํ•œ ํ•„๋“œ ๊ฐ’์„ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€๋œ๋‹ค.

  • ๋‹จ, 6๋ฒˆ ์งธ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. RDBMS๊ฐ€ PK์˜ ์œ ์ผ์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.



INSERT INTO prod.vital(user_id, vital_id, date, wight) VALUES(1, 4, '2020-01-02', 101);
INSERT INTO prod.vital(user_id, vital_id, date, wight) VALUES(2, NULL, '2020-01-04', 100);
INSERT INTO prod.vital(user_id, vital_id, date, wight) VALUES(3, NULL, '2020-01-04', 101);   
  • ์ถ”๊ฐ€ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ์˜ ์ˆœ์„œ๊ฐ€ ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•  ๋•Œ์˜ ํ•„๋“œ ์ˆœ์„œ์™€ ๋™์ผํ•˜๋‹ค๋ฉด
    INSERT๋ฌธ์—์„œ ํ•„๋“œ๋ช… ๋ช…์‹œ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜๋„ ์žˆ๋‹ค.



๐Ÿ‘‰ IS NULL

  • ํŠน์ • ํ•„๋“œ์˜ ๊ฐ’์ด NULL์ธ ๋ ˆ์ฝ”๋“œ๋งŒ ์ฐพ๊ณ ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ„ ์งˆ์˜๋กœ ์ถ”๊ฐ€ํ•œ 2, 3๋ฒˆ์งธ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์งˆ์˜๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๋Š” ์ด๊ฒƒ์ด๋‹ค.

SELECT * FROM prod.alert WHERE vital_id = NULL;


  • ๋ถ„๋ช… INSERT๋ฌธ์œผ๋กœ ์ถ”๊ฐ€ํ•œ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๊ฒ€์ƒ‰๋˜์ง€ ์•Š๋Š” ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ?
    ์ •๋‹ต์€ NULL ์—ฐ์‚ฐ ์‹œ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ํ‚ค์›Œ๋“œ์— ์žˆ๋‹ค.
    NULL์˜ ์—ฐ์‚ฐ์€ '=' ์—ฐ์‚ฐ์ž๊ฐ€ ์•„๋‹Œ IS NULL ์—ฐ์‚ฐ์œผ๋กœ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.



๐Ÿ‘‰ IS NOT NULL

  • NULL์ด ์•„๋‹Œ ๊ฐ’์„ ์ฐพ๊ณ ์‹ถ์„ ๋•Œ๋„ '!=' ์—ฐ์‚ฐ์ž๊ฐ€ ์•„๋‹ˆ๋ผ IS NOT NULL ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

SELECT * FROM prod.alert WHERE vital_id IS NOT NULL;






3. DELETE

DELETE๋Š” ํ…Œ์ด๋ธ”์—์„œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด์ด๋‹ค.
๋‹จ, ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•˜๋”๋ผ๋„ ํ…Œ์ด๋ธ”์€ ๊ณ„์† ์กด์žฌํ•œ๋‹ค.



๐Ÿ’ฟ DELETE FROM vs TRUNCATE


์ฐจ์ด์ 

  • DELETE FROM์€ ์กฐ๊ฑด์„ ์ฃผ๊ณ  ์œตํ†ต์„ฑ์žˆ๊ฒŒ ํŠน์ • ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

  • TRUNCATE๋Š” ์กฐ๊ฑด ์—†์ด ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค. ์†๋„๊ฐ€ ๋น ๋ฅด์ง€๋งŒ
    ํŠธ๋žœ์žญ์…˜ ์‚ฌ์šฉ์‹œ ๋กค๋ฐฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.


๊ณตํ†ต์ 

  • ๋‘ ๋ช…๋ น์–ด๋กœ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•ด๋„ ํ…Œ์ด๋ธ”์€ ๊ณ„์† ์กด์žฌํ•œ๋‹ค.



์˜ˆ์ œ

DELETE FROM prod.vital WHERE weight <= 0;
  • ์œ„ ์˜ˆ์ œ์—์„œ๋Š”
    DELETE ์กฐ๊ฑด์œผ๋กœ weightํ•„๋“œ๊ฐ€ 0๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋งŒ ์‚ญ์ œ ๋ฅผ ๊ฑธ์—ˆ๋‹ค.
    ๊ทธ๋Ÿผ INSERT ์˜ˆ์ œ์—์„œ prod.vital ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€ํ–ˆ๋˜ 5๋ฒˆ์งธ ๋ ˆ์ฝ”๋“œ์ธ
    weight๊ฐ’์œผ๋กœ -1์ด ๋“ค์–ด๊ฐ„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค. (์•„๋ž˜ ์ฝ”๋“œ ์ฐธ์กฐ)

INSERT INTO prod.vital(user_id, vital_id, date, wight) VALUES(999, 5, '2020-01-02', -1); 
-- ์ด ๋ ˆ์ฝ”๋“œ๋Š” ์‚ญ์ œ๋œ๋‹ค.


๐Ÿ‘‰ Safe update mode ์™€ PK

์ถ”๊ฐ€๋กœ, ์‚ฌ์‹ค ์œ„ ์˜ˆ์ œ์˜
DELETE FROM prod.vital WHERE weight <= 0; ์ฝ”๋“œ๋Š” MySQL์—์„œ ์ง์ ‘ ์‹คํ–‰์‹œํ‚ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

MySQL์˜ ๊ธฐ๋ณธ ๋™์ž‘ ๋ชจ๋“œ์ธ safe update mode์—์„œ๋Š”
DELETE, UPDATE๋ฅผ ํ•  ๋•Œ WHERE์ ˆ(์กฐ๊ฑด์ ˆ)์— ๋ฐ˜๋“œ์‹œ PK๊ฐ’(ํ‚ค๊ฐ’)์„ ์ด์šฉํ•ด์•ผํ•œ๋‹ค.
๊ฒŒ๋‹ค๊ฐ€ ํ•˜๋‚˜์˜ ๋ ˆ์ฝ”๋“œ๋งŒ DELETE, UPDATEํ•˜๋„๋ก ์„ค์ •์ด ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์—
๋‹ค์ˆ˜์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ DELETE, UPDATEํ•˜๋Š” sql์งˆ์˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค๋ฉด
์•„์˜ˆ ํ•ด๋‹น ์ฝ”๋“œ์˜ ์‹คํ–‰์„ ๋ง‰์•„๋ฒ„๋ฆฐ๋‹ค.


๋”ฐ๋ผ์„œ ์งˆ์˜๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” vital_id(PK)๋ฅผ ์‚ฌ์šฉํ•œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

DELETE FROM prod.vital WHERE vital_id = 5;


๋งŒ์•ฝ safe mode๋ฅผ ํ•ด์ œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค.


SET SQL_SAFE_UPDATES = 0; 		-- 0 : sefe update mode ํ•ด์ œ , 1: safe update mode ์„ค์ •

๐Ÿ‘‰ ์›Œํฌ๋ฒค์น˜๋กœ ํ•ด์ œํ•˜๋Š” ๋ฐฉ๋ฒ•




4 UPDATE

UPDATE๋Š” ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํŠน์ • ๋ ˆ์ฝ”๋“œ์˜ ํ•„๋“œ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š” ๋ช…๋ น์–ด์ด๋‹ค.


์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž.
๋‹ค์Œ์€ vital_id (PK)๊ฐ€ 4์ธ ๋ ˆ์ฝ”๋“œ์˜ weight๋ฅผ 92๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์งˆ์˜์ด๋‹ค.


UPDATE prod.vital
SET weight = 92
WHERE vital_id = 4;
  • ๋งŒ์•ฝ WHERE๋กœ ์กฐ๊ฑด์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด prod.vitalํ…Œ์ด๋ธ” ์˜ ๋ชจ๋“  weightํ•„๋“œ์˜ ๊ฐ’์ด 92๋กœ ๋ฐ”๋€๋‹ค.

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


profile
๊ฐœ๋ฐœ ๊ณต๋ถ€๋ฅผ ํ•ด๋ผ

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