DB/All-DB

[DB] Insert ์œ ์šฉํ•œ Options

Codit Develop 2023. 5. 18. 16:19
๋ฐ˜์‘ํ˜•

๐Ÿ”Ž Insert Query

Insert ์ค‘ ์ค‘๋ณต ์˜ค๋ฅ˜๋กœ ์ธํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ฐพ์•„๋ณด๋‹ˆ ์—ฌ๋Ÿฌ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜์—ฌ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค.

์ฟผ๋ฆฌ๋Š” ์—ญ์‹œ ์–ด๋ ค์›Œ

๐Ÿ“‘ Insert ์ค‘๋ณต ๋ฐฉ์ง€

๐Ÿ“Œ Ignore

PK/UK์— ์˜ํ•ด ์ค‘๋ณต ์ฒ˜๋ฆฌ๊ฐ€ ๋‚  ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ๋ฌด์‹œ ์ฒ˜๋ฆฌ

-- ์˜ค๋ฅ˜ ๋ฐœ์ƒ
Insert Into `user-env` (user_idx , env_code) 
values ('001','ENV_TEST');

-- ์˜ค๋ฅ˜ ๋ฌด์‹œ
Insert Ignore Into `user-env` (user_idx , env_code)
values ('001','ENV_TEST');

๐Ÿ“Œ Filter (Where not exists)

Select๋กœ ์กฐํšŒ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ Insert


Insert Into `user-env` (user_idx , env_code) 
values ('001','ENV_TEST')
where not exists
(select * from `user-env` where user_idx = '001' and env_code = 'ENV_TEST');

๐Ÿ“‘ Insert ์ค‘๋ณต ์‹œ ํ›„์ฒ˜๋ฆฌ

๐Ÿ“Œ Duplicate key [QUERY]

Insert ์ค‘๋ณต ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ Update ์ฒ˜๋ฆฌ

Insert Into `user_env` (user_idx , env_code) 
duplicate key
update user_idx = '001' and env_code = 'ENV_TEST';

ํ•ด๋‹น ๊ตฌ๋ฌธ์€ ์•„์ง ์™„๋ฒฝํ•˜๊ฒŒ ์ดํ•ด๋˜์ง€ ์•Š์•„ ์˜ˆ์‹œ๊ฐ€ ํ‹€๋ฆด ์ˆ˜ ์žˆ์Œ.

๐Ÿ“Œ ์ค‘๋ณต ์‹œ ๋ฎ์–ด์“ฐ๊ธฐ

Insert ์ค‘๋ณต ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ํ•ด๋‹น ์ค‘๋ณต๊ฐ’์„ ์‚ญ์ œํ•˜๊ณ  ์ƒˆ๋กœ ์ƒ์„ฑ

Replace Into `user_env` (user_idx, env_code)
values ('001','ENV_TEST');

๐Ÿ“‹ REF

https://zionh.tistory.com/43
https://tez.kr/161

๋ฐ˜์‘ํ˜•