2021. 10. 15. 11:18ใ๋ฐ์ดํฐ๋ฒ ์ด์ค
1. update
update ํ ์ด๋ธ๋ช set ํ๋๋ช 1 = ๊ฐ, ํ๋๋ช 2 = ๊ฐ, ... where ์กฐ๊ฑด์;
2. insert
insert into ํ ์ด๋ธ๋ช values (๊ฐ1, ๊ฐ2, ๊ฐ3, ...);
or
insert into ํ ์ด๋ธ๋ช (ํ๋๋ช 1, ํ๋๋ช 2...) valeus ( ๊ฐ1, ๊ฐ2,...);
3. merge
merge ๋ช ๋ น์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ง์ ํ ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๊ฑฐ๋ ์์ ํ๋ค.
์ผ๋ฐ์ ์ผ๋ก ์กฐ๊ฑด์์ ๋ฐ๋ผ ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด update ๊ตฌ๋ฌธ์ ์ฒ๋ฆฌํ๊ณ , ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด insert ๊ตฌ๋ฌธ์ ์ฒ๋ฆฌํ๋ค.
merge into ์ ๋ ฅ/ ์์ ํ ํ ์ด๋ธ๋ช
using (ํ ์ด๋ธ๋ช | ๋ทฐ| ์๋ธ์ฟผ๋ฆฌ)
on ์กฐ๊ฑด์
when matched then
update set ํ๋๋ช 1 = ๊ฐ1, ...
when not matched then
insert .. values ๊ตฌ๋ฌธ
4. delete
delete from ํ ์ด๋ธ๋ช where ์กฐ๊ฑด์;
5. commit
commit ๋ช ๋ น์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ๋ ๋ด์ฉ์ ๋ฐ์ํ๋ค.
insert, update, delete ๋ช ๋ น์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊พธ๋ฉด ๋ฐ๋ก db์ ๋ฐ์ํ๋ ๊ฒ์ด ์๋๋ผ ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ๋ ๋ด์ฉ์ ์์๋ก ์ ์ฅํ๋ค. ๋ฐ๋ผ์ ๋ฐ๋ ๋ด์ฉ์ ๋ฐ์ํ๊ธฐ ์ํด์ commit ๋ช ๋ น์ ์คํํด db์ ์ค์ ๋ก ๋ฐ์ํ๋ค.
6. rollback
rollback ๋ช ๋ น์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ฐ๋ ๋ด์ฉ์ ์ทจ์ํ๊ณ ์ต์ข commit ์ํ๋ก ๋์๊ฐ๋ค.
7. ํธ๋์ญ์
ํธ๋์ญ์ transaction ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ์์ ํ๋๋ก ๋ฌถ์ ์ ์๋ ์ ๋ฌด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
ํธ๋์ญ์ ์์ ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๋ ฅ, ์์ , ์ญ์ ๋ฑ ์ฌ๋ฌ ์์ ์ ํฌํจํ๋ฉฐ, ๋ฐ์ดํฐ์ ๋ฌด๊ฒฐ์ฑ์ ๋ณด์ฅํ๊ธฐ ์ํด ์์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์์ ์ ์ฒด๋ฅผ ์ทจ์ํ๊ณ ์์ ์ ์ํ๋ก ๋์๊ฐ๋ค.
https://hyejin.tistory.com/119?category=975063
[Database] Transaction๊ณผ Trigger
1. Transaction ํธ๋์ญ์ ์ด๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ํ๋ฅผ ๋ณํ์ํค๋ ํ๋์ ๋ ผ๋ฆฌ์ ๊ธฐ๋ฅ์ ์ํํ๊ธฐ ์ํ ์์ ์ ๋จ์ ๋๋ ํ๊บผ๋ฒ์ ์ํ๋์ด์ผ ํ ์ผ๋ จ์ ์ฐ์ฐ๋ค์ ์๋ฏธํ๋ค. ํธ๋์ญ์ ์ ๋ฐ์ดํฐ๋ฒ ์ด
hyejin.tistory.com
ํธ๋์ญ์ ๊ด๋ จ ์์ธํ ๊ธ์ ์ฌ๊ธฐ์ ํ์ธ ๊ฐ๋ฅํ๋ค.
8. ์๋ธ์ฟผ๋ฆฌ (SUB QUERY)
์๋ธ์ฟผ๋ฆฌ๋ select ๊ตฌ๋ฌธ ์์ ์๋ก์ด select ๊ตฌ๋ฌธ์ผ๋ก sql์ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด๋ ๊ฒ ๋ณต์กํ๊ฒ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์งค ์๋ ์๋๋ฐ
๋๋ ์ ์๊ฐํด๋ด์ผํ๋ค.
1๏ธโฃ
2๏ธโฃ
3๏ธโฃ
4๏ธโฃ
9. exists
exists ๊ตฌ๋ฌธ์ ์๋ธ์ฟผ๋ฆฌ์ ๊ฒฐ๊ณผ๊ฐ ์์ผ๋ฉด ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๋ค.
select ๊ฒ์ ํ๋ from ํ ์ด๋ธ where exists (select ~);
10. ๊ฒ์ ์กฐ๊ฑด์ ์ ๋ ฅํด์ sql ์คํ
ํ๋ = :๊ฒ์์กฐ๊ฑด๋ช
* ์ฃผ์ ์ฒ๋ฆฌํ๋๋ฒ
/* */
--