๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ž๊ฒฉ์ฆ ์žˆ์œผ๋ฉด ์ข‹์ง€/์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ

[์‹ค๊ธฐ] ์ดˆ์ดˆ์ดˆ์š”์•ฝ - 8์žฅ SQL ์‘์šฉ

by ์ž„๋ฆฌ๋‘ฅ์ ˆ 2024. 10. 17.
๋ฐ˜์‘ํ˜•

์ธ๋„ค์ผ ์ง์ ‘ ๋งŒ๋“ค์–ด๋”ฐ ใ…‹ใ…‹

 


 

๋”๋ณด๊ธฐ
๋”๋ณด๊ธฐ

์ฑ… ์ž์ฒด์—์„œ A, B ์ด๋Ÿฐ ์‹์œผ๋กœ ์ถœ์ œ ์ •๋„๋ฅผ ํ‘œ์‹œํ•˜๊ณ , ์ถœ์ œ๋œ ๋…„๋„ ์ˆ˜๋„ ๋‚˜์˜จ๋‹ค. ํ‘œ์‹œ๋œ ๋Œ€๋กœ ํ•œ ๋ฒˆ ์ด์ƒ ์ถœ์ œ๋Š” ํšŒ์ƒ‰ ๋ฐฐ๊ฒฝ,  ๋‘ ๋ฒˆ์ด์ƒ ๋ฐ ์ง์ ‘ ์ถœ์ œ๋ฅผ ํ™•์ธํ•œ ๊ฒฝ์šฐ, ๋…ธ๋ž‘ ๋ฐฐ๊ฒฝ์œผ๋กœ ํ‘œ์‹œํ•  ์˜ˆ์ •์ด๋‹ค. ๋˜ํ•œ ํ‚ค์›Œ๋“œ ์œ„์ฃผ๋กœ ์ตœ๋Œ€ํ•œ ๊ธฐ์–ตํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๊ธฐ๋กํ•  ์˜ˆ์ •์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ดํ•ดํ•˜๊ธฐ ํž˜๋“ค ์ˆ˜๋„์žˆ์ง€๋งŒ ์ตœ๋Œ€ํ•œ ์‰ฝ๊ฒŒ ์จ๋ณผ ์˜ˆ์ •์ด๋‹ค...


DDL ; Data Define Language

DB๋ฅผ ๊ตฌ์ถ•ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ๋ชฉ์  

  • CREATE ; SCHEMA, DOMAIN, TABLE, VIEW, INDEX ์ •์˜
    CREATE [UNIQUE] INDEX ์ธ๋ฑ์Šค๋ช… 
    ON ํ…Œ์ด๋ธ”๋ช… (์†์„ฑ๋ช… [ASC | DESC]);
  • ALTER ; TABLE์— ๋Œ€ํ•œ ์ •์˜ ๋ณ€๊ฒฝ
    ALTER TABLE ํ…Œ์ด๋ธ”๋ช… ADD | DROP COLUMN ์†์„ฑ๋ช… ๋ฐ์ดํ„ฐํƒ€์ž… [DEFAULT ' ๊ธฐ๋ณธ๊ฐ’'];
  • DROP ; SCHEMA, DOMAIN, TABLE, VIEW, INDEX ์‚ญ์ œ 
    DROP ์ข…๋ฅ˜ ์ข…๋ฅ˜๋ช… [CASCADE | RESTRICT];
# CREATE --------
CREATE [UNIQUE] INDEX ์ธ๋ฑ์Šค๋ช… 
# UNIQUE ์‚ฌ์šฉ - ์ค‘๋ณต ์—†. (์ƒ๋žต : ์ค‘๋ณตํ—ˆ์šฉ)
ON ํ…Œ์ด๋ธ”๋ช…(์†์„ฑ๋ช…[ASC|DESC][,์†์„ฑ๋ช…[ASC|DESC]]) 
# ASC | DESC ์˜ค๋ฆ„์ฐจ์ˆœ | ๋‚ด๋ฆผ์ฐจ์ˆœ (์ƒ๋žต : ์˜ค๋ฆ„)
[CLUSTER]; 
# CLUSTER ์‚ฌ์šฉํ•˜๋ฉด ํด๋Ÿฌ์Šคํ„ฐ๋“œ ์ธ๋ฑ์Šค ์„ค์ • 

# <๊ณ ๊ฐ> ํ…Œ์ด๋ธ”, UNIQUE ํŠน์„ฑ ๊ฐ–๋Š” '๊ณ ๊ฐ๋ฒˆํ˜ธ' ์†์„ฑ์— ๋Œ€ํ•ด ๋‚ด๋ฆผ์ฐจ์ˆœ, '๊ณ ๊ฐ๋ฒˆํ˜ธ_idx'์ด๋ฆ„์œผ๋กœ ์ธ๋ฑ์Šค ์ •์˜
CREATE UNIQUE INDEX ๊ณ ๊ฐ๋ฒˆํ˜ธ_idx
ON ๊ณ ๊ฐ(๊ณ ๊ฐ๋ฒˆํ˜ธ DESC);


#ALTER --------
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… ADD ์†์„ฑ๋ช… ๋ฐ์ดํ„ฐ_ํƒ€์ž… [DEFAULT '๊ธฐ๋ณธ๊ฐ’'];
# ADD ์—ด ์ถ”๊ฐ€
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… ADD ์†์„ฑ๋ช… [SET DEFAULT '๊ธฐ๋ณธ๊ฐ’'];
# ALTER ํŠน์ • ์†์„ฑ ๊ธฐ๋ณธ๊ฐ’ ๋ณ€๊ฒฝ
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… DROP COLUMN ์†์„ฑ๋ช… [CASCADE];
# DROP COLUMN ํŠน์ • ์†์„ฑ ์‚ญ์ œ 

# <ํ•™์ƒ> ํ…Œ์ด๋ธ”. ์ตœ๋Œ€ 3๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜๋Š” 'ํ•™๋…„'์†์„ฑ ์ถ”๊ฐ€,
# 'ํ•™๋ฒˆ'ํ•„๋“œ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ํฌ๊ธฐ VARCHAR(10)์œผ๋กœ ํ•˜๊ณ  NULL ์ž…๋ ฅ๋˜์ง€ ์•Š๋„๋ก. 
ALTER TABLE ํ•™์ƒ ADD ํ•™๋…„ VARCHAR(3);
ALTER TABLE ํ•™์ƒ ALTER ํ•™๋ฒˆ VARCHAR(10) NOT NULL;


#DROP --------
DROP SCHEMA ์Šคํ‚ค๋งˆ๋ช… [CASCADE | RESTRICT];
DROP DOMAIN, TABLE, VIEW, INDEX ๊ฐ๋ช… [CASCADE | RESTRICT];
DROP CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด๋ช…;
# CASCADE ์ฐธ์กฐํ•˜๋Š” ๋ชจ๋“  ~~~ ๋‹ค
# RESTRICT ์ฐธ์กฐ์ค‘์ด๋ฉด ์ œ๊ฑฐ ์ทจ์†Œ 

# <ํ•™์ƒ> ํ…Œ์ด๋ธ” ์ œ๊ฑฐ, ์ฐธ์กฐํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ
DROP TABLE ํ•™์ƒ CASCADE;

DCL ; Data Control Language

Data์˜ ๋ณด์•ˆ, ๋ฌด๊ฒฐ์„ฑ, ํšŒ๋ณต, ๋ณ‘ํ–‰ ์ œ์–ด ๋“ฑ์„ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด. ๊ด€๋ฆฌ์ž๊ฐ€ ์‚ฌ์šฉ 

  • COMMIT ; ์™„๋ฃŒ, ์•Œ๋ฆผ
  • ROLLBACK ; ๋น„์ •์ƒ ์ข…๋ฃŒ, ์›๋ž˜ ๋ณต๊ตฌ. ๋ณ€๊ฒฝ๋˜์—ˆ์œผ๋‚˜ COMMIT ๋˜์ง€ ์•Š์€ ๋ชจ๋“  ๋‚ด์šฉ ์ทจ์†Œ, ๋˜๋Œ๋ฆฌ๋Š”. 
    Inconsistency ์ƒํƒœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ถ€ ์™„๋ฃŒ๋œ ํŠธ๋žœ์žญ์…˜์€ ๋กค๋ฐฑ ๋˜์–ด์•ผ ํ•จ. 
  • GRANT / REVOKE ; ๊ถŒํ•œ ๋ถ€์—ฌ, ์ทจ์†Œ
    GRANT (ALL) ON ํ…Œ์ด๋ธ”๋ช… TO ์‚ฌ์šฉ์ž๋ช… WITH GRANT OPTION
    REVOKE ๊ถŒํ•œ(GRANT OPTION) FOR ๋ญํ•˜๋Š”๊ถŒํ•œ? (UPDATE) ON ํ…Œ์ด๋ธ”๋ช… FROM ์‚ฌ์šฉ์ž๋ช… 
# GRANT / REVOKE --------
GRANT ๊ถŒํ•œ๋ฆฌ์ŠคํŠธ ON ๊ฐœ์ฒด TO ์‚ฌ์šฉ์ž [WITH GRANT OPTION];
REVOKE [GRANT OPTION FOR] ๊ถŒํ•œ๋ฆฌ์ŠคํŠธ ON ๊ฐœ์ฒด 
FROM ์‚ฌ์šฉ์ž [CASCADE];

# ์‚ฌ์šฉ์ž ID 'NABI'์—๊ฒŒ <๊ณ ๊ฐ> ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๋ชจ๋“  ๊ถŒํ•œ, ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ๊ถŒํ•œ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ
GRANT ALL ON ๊ณ ๊ฐ TO NABI WITH GRANT OPTION;
# ์‚ฌ์šฉ์ž ID 'STAR'์—๊ฒŒ ๋ถ€์—ฌํ•œ <๊ณ ๊ฐ> ํ…Œ์ด๋ธ” ๊ถŒํ•œ ์ค‘, UPDATE ๊ถŒํ•œ์„ ๋‹ค๋ฅธ์‚ฌ๋žŒ์—๊ฒŒ ๋ถ€์—ฌํ•˜๋Š” ๊ถŒํ•œ๋งŒ ์ทจ์†Œ
REVOKE GRANT OPTION FOR UPDATE ON ๊ณ ๊ฐ FROM STAR;

DML ; Data Manipulation Language

์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์งˆ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด

  • SELECT
    SELECT FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด GROUP BY ์†์„ฑ๋ช… HAVING ORDER BY
  • INSERT
  • DELETE
    DELETE FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด
  • UPDATE
    UPDATE SET ์ˆ˜์ •์†์„ฑ๋ช… = ์ˆ˜์ •๋ฐ์ดํ„ฐ WHERE ์กฐ๊ฑด
# SELECT -----
SELECT 
[PREDICATE] [ํ…Œ์ด๋ธ”๋ช…] ์†์„ฑ๋ช… [AS ๋ณ„์นญ(๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ํ‘œ์‹œ)] ....
# ๊ฒ€์ƒ‰ํ•  ํŠœํ”Œ ์ˆ˜๋ฅผ ์ œํ•œํ•˜๋Š”, DISTINCT - ์ค‘๋ณต์žˆ์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ๋งŒ ํ‘œ์‹œ
FROM ํ…Œ์ด๋ธ”๋ช…
[WHERE] ์กฐ๊ฑด
[GROUP BY] ์†์„ฑ๋ช…
# ํŠน์ • ์†์„ฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ™” ๊ฒ€์ƒ‰
[HAVING] ์กฐ๊ฑด
# GROUP BY ์™€ ๊ฐ™์ด. ๊ทธ๋ฃน์— ๋Œ€ํ•œ ์กฐ๊ฑด
[ORDER BY] ์†์„ฑ๋ช… [ASC | DESC];
# ์ •๋ ฌ

# <์‚ฌ์›> ๋ชจ๋“  ํŠœํ”Œ
# <์‚ฌ์›> ํ…Œ์ด๋ธ” '์ฃผ์†Œ'๊ฒ€์ƒ‰, ๊ฐ™์€ '์ฃผ์†Œ'ํ•œ๋ฒˆ๋งŒ
SELECT *FROM ์‚ฌ์›;
SELECT DISTINCT ์ฃผ์†Œ FROM ์‚ฌ์›;


# DELETE -----
DELETE FROM ํ…Œ์ด๋ธ”๋ช… [WHERE ์กฐ๊ฑด];
# ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ๋Š” WHERE ์—†์ด

# <์‚ฌ์›> ํ…Œ์ด๋ธ” '์ž„๊บฝ์ •' ํŠœํ”Œ ์‚ญ์ œ
# <์‚ฌ์›> ํ…Œ์ด๋ธ” '์ธํ„ฐ๋„ท' ๋ถ€์„œ ๋ชจ๋“  ํŠœํ”Œ ์‚ญ์ œ
# <์‚ฌ์›> ํ…Œ์ด๋ธ” ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ
DELETE FROM ์‚ฌ์› WHERE ์ด๋ฆ„='์ž„๊บฝ์ •';
DELETE FROM ์‚ฌ์› WHERE ๋ถ€์„œ='์ธํ„ฐ๋„ท';
DELETE FROM ์‚ฌ์›;


# UPDATE -----
UPDATE ํ…Œ์ด๋ธ”๋ช…
SET ์†์„ฑ๋ช… = ๋ฐ์ดํ„ฐ[, ์†์„ฑ๋ช… = ๋ฐ์ดํ„ฐ, ...]
[WHERE ์กฐ๊ฑด];

# <์‚ฌ์›> ํ…Œ์ด๋ธ”, "ํ™๊ธธ๋™" '์ฃผ์†Œ'๋ฅผ "์ˆ˜์ƒ‰๋™" ์ˆ˜์ •
# <์‚ฌ์›> ํ…Œ์ด๋ธ”, "ํ™ฉ์ง„์ด" '๋ถ€์„œ'๋ฅผ "๊ธฐํš๋ถ€" ๋ณ€๊ฒฝ, '๊ธฐ๋ณธ๊ธ‰' 5๋งŒ์› ์ธ์ƒ
UPDATE ์‚ฌ์› SET ์ฃผ์†Œ='์ˆ˜์ƒ‰๋™' WHERE ์ด๋ฆ„='ํ™๊ธธ๋™';
UPDATE ์‚ฌ์›
SET ๋ถ€์„œ='๊ธฐํš๋ถ€', ๊ธฐ๋ณธ๊ธ‰=๊ธฐ๋ณธ๊ธ‰+5
WHERE ์ด๋ฆ„='ํ™ฉ์ง„์ด';

์กฐ๊ฑด ์ง€์ • ๊ฒ€์ƒ‰

  • ๋น„๊ต ์—ฐ์‚ฐ์ž
    =, <>(๊ฐ™์ง€์•Š๋‹ค), >, <, >=, <=
  • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
    NOT, AND, OR
  • LIKE ์—ฐ์‚ฐ์ž
    %(๋ชจ๋“  ๋ฌธ์ž ๋Œ€ํ‘œ), _(๋ฌธ์ž ํ•˜๋‚˜ ๋Œ€ํ‘œ), #(์ˆซ์ž ํ•˜๋‚˜ ๋Œ€ํ‘œ)
  • IN ์—ฐ์‚ฐ์ž 
# 1) <์‚ฌ์›> ํ…Œ์ด๋ธ”์—์„œ '๊ธฐํš'๋ถ€ ๋ชจ๋“  ํŠœํ”Œ ๊ฒ€์ƒ‰
SELECT * 
FROM ์‚ฌ์› 
WHERE ๋ถ€์„œ = '๊ธฐํš';

# 2) <์‚ฌ์›> ํ…Œ์ด๋ธ”์—์„œ '๊ธฐํš'๊ทผ๋ฌด, '๋Œ€ํฅ๋™'์‚ฌ๋Š” ์‚ฌ๋žŒ 
SELECT *
FROM ์‚ฌ์›
WHERE ๋ถ€์„œ = '๊ธฐํš' AND ์ฃผ์†Œ = '๋Œ€ํฅ๋™';

# 3) <์‚ฌ์›> ํ…Œ์ด๋ธ”์—์„œ ์„ฑ์ด '๊น€'์‚ฌ๋žŒ
SELECT *
FROM ์‚ฌ์›
WHERE ์ด๋ฆ„ LIKE "๊น€%"; 
# %๋ชจ๋“  ๋ฌธ์ž

# 4) <์‚ฌ์›> ํ…Œ์ด๋ธ”์—์„œ '์ƒ์ผ'์ด '01/01/69'์—์„œ '12/31/73' ์‚ฌ์ด์ธ ํŠœํ”Œ
SELECT *
FROM ์‚ฌ์›
WHERE ์ƒ์ผ BETWEEN #01/01/69# AND #12/31/73#; 
# #์€ ์ˆซ์ž

# 5) <์‚ฌ์›> ํ…Œ์ด๋ธ”์—์„œ '์ฃผ์†Œ'๊ฐ€ NULL ์ธ ํŠœํ”Œ
SELECT *
FROM ์‚ฌ์›
WHERE ์ฃผ์†Œ IS NULL;

์ •๋ ฌ ๊ฒ€์ƒ‰ 

ORDER BY ํŠน์ • ์†์„ฑ ์ง€์ •

# <์‚ฌ์›> ํ…Œ์ด๋ธ” '๋ถ€์„œ' ๊ธฐ์ค€, ์˜ค๋ฆ„์ฐจ์ˆœ. ๊ฐ™์€ '๋ถ€์„œ'์— ๋Œ€ํ•ด์„œ '์ด๋ฆ„' ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ
SELECT *
FROM ์‚ฌ์›
ORDER BY ๋ถ€์„œ ASC, ์ด๋ฆ„ DESC;

 

ํ•˜์œ„ ์งˆ์˜ 

์กฐ๊ฑด์ ˆ์— ์ฃผ์–ด์ง„ ์งˆ์˜ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์กฐ๊ฑด์ ˆ์˜ ํ”ผ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉ

# '์ทจ๋ฏธ'๊ฐ€ '๋‚˜์ดํŠธ๋Œ„์Šค'์ธ ์‚ฌ์›์˜ '์ด๋ฆ„'๊ณผ '์ฃผ์†Œ'๋ฅผ ๊ฒ€์ƒ‰
SELECT ์ด๋ฆ„, ์ฃผ์†Œ
FROM ์‚ฌ์›
WHERE ์ด๋ฆ„ = (
	SELECT ์ด๋ฆ„ FROM ์—ฌ๊ฐ€ํ™œ๋™ WHERE ์ทจ๋ฏธ = '๋‚˜์ดํŠธ๋Œ„์Šค'
);

# ์ทจ๋ฏธํ™œ๋™ ํ•˜์ง€ ์•Š๋Š” ์‚ฌ์›๋“ค์„ ๊ฒ€์ƒ‰
SELECT *
FROM ์‚ฌ์›
WHERE ์ด๋ฆ„ NOT IN (SELECT ์ด๋ฆ„ FROM ์—ฌ๊ฐ€ํ™œ๋™);

# '๋ง์›๋™'์— ๊ฑฐ์ฃผํ•˜๋Š” ์‚ฌ์›๋“ค์˜ '๊ธฐ๋ณธ๊ธ‰'๋ณด๋‹ค ์ ์€ '๊ธฐ๋ณธ๊ธ‰'์„ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰
SELECT ์ด๋ฆ„, ๊ธฐ๋ณธ๊ธ‰, ์ฃผ์†Œ
FROM ์‚ฌ์›
WHERE ๊ธฐ๋ณธ๊ธ‰ < ALL (SELECT ๊ธฐ๋ณธ๊ธ‰ FROM ์‚ฌ์› WHERE ์ฃผ์†Œ = '๋ง์›๋™');

 

๊ทธ๋ฃน ์ง€์ • ๊ฒ€์ƒ‰

GROUP BY ์ง€์ • ์†์„ฑ ๊ธฐ์ค€(์—ด๊ธฐ์ค€)์œผ๋กœ ๊ฒ€์ƒ‰

# <์ƒ์—ฌ๊ธˆ> ํ…Œ์ด๋ธ”์—์„œ '๋ถ€์„œ'๋ณ„ '์ƒ์—ฌ๊ธˆ'์˜ ํ‰๊ท 
SELECT ๋ถ€์„œ, AVG(์ƒ์—ฌ๊ธˆ) AS ํ‰๊ท 
FROM ์ƒ์—ฌ๊ธˆ
GROUP BY ๋ถ€์„œ; # ์—ด ์ง€์ •

# <์ƒ์—ฌ๊ธˆ> ํ…Œ์ด๋ธ”์—์„œ /'์ƒ์—ฌ๊ธˆ'์ด 100 ์ด์ƒ์ธ /์‚ฌ์›์ด 2๋ช… ์ด์ƒ์ธ /'๋ถ€์„œ'์˜ ํŠœํ”Œ ์ˆ˜
SELECT ๋ถ€์„œ, COUNT(*) AS ์‚ฌ์›์ˆ˜
FROM ์ƒ์—ฌ๊ธˆ
WHERE ์ƒ์—ฌ๊ธˆ >= 100
GROUP BY ๋ถ€์„œ 
HAVING COUNT(*) >= 2;
# ์ง€์ • ์—ด์—์„œ ์กฐ๊ฑด

์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•œ ํ†ตํ•ฉ ์งˆ์˜

2๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜๋กœ ํ†ตํ•ฉ 

# ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž --------
SELECT ์†์„ฑ๋ช…1, ์†์„ฑ๋ช…2...
FROM ํ…Œ์ด๋ธ”๋ช…

UNION | UNION ALL | INTERSECT | EXCEPT
# UNION ํ•ฉ์ง‘ํ•ฉ, ์ค‘๋ณต ํ–‰ ํ•œ ๋ฒˆ๋งŒ 
# UNION ALL ํ•ฉ์ง‘ํ•ฉ, ์ค‘๋ณต ํ–‰ ๋‹ค
# INTERSECT ๊ต์ง‘ํ•ฉ. ๊ณตํ†ต ํ–‰ ์ถœ๋ ฅ
# EXCEPT ์ฐจ์ง‘ํ•ฉ. ์ฒซ - ๋‘๋ฒˆ์งธ ํ–‰ ์ œ์™ธ ์ถœ๋ ฅ

SELECT ์†์„ฑ๋ช…1, ์†์„ฑ๋ช…2...
FROM ํ…Œ์ด๋ธ”๋ช…
[ORDER BY ์†์„ฑ๋ช… [ASC | DESC]];

# LEFT OUTER JOIN -------
# INNER JOIN ๊ฒฐ๊ณผ ๊ตฌํ•ด ์šฐ์ธก ํ•ญ ๋ฆด๋ ˆ์ด์…˜ ์ขŒ์ธก์— NULL ๋ถ™์—ฌ ์ถ”๊ฐ€
SELECT [ํ…Œ์ด๋ธ”๋ช…1]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2]์†์„ฑ๋ช…..
FROM ํ…Œ์ด๋ธ”๋ช…1 LEFT OUTER JOIN ํ…Œ์ด๋ธ”๋ช…2
ON ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…;

# RIGHT OUTER JOIN -------
# INNER JOIN ๊ฒฐ๊ณผ ๊ตฌํ•ด ์ขŒ์ธก ํ•ญ ๋ฆด๋ ˆ์ด์…˜ ์ขŒ์ธก์— NULL ๋ถ™์—ฌ ์ถ”๊ฐ€
SELECT [ํ…Œ์ด๋ธ”๋ช…1]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2]์†์„ฑ๋ช…..
FROM ํ…Œ์ด๋ธ”๋ช…1 RIGHT OUTER JOIN ํ…Œ์ด๋ธ”๋ช…2
ON ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…;

# <ํ•™์ƒ> ํ…Œ์ด๋ธ”, <ํ•™๊ณผ> ํ…Œ์ด๋ธ” ์—์„œ 'ํ•™๊ณผ์ฝ”๋“œ' ๊ฐ’์ด ๊ฐ™์€ ํŠœํ”Œ JOIN, 'ํ•™๋ฒˆ', '์ด๋ฆ„', 'ํ•™๊ณผ์ฝ”๋“œ', 'ํ•™๊ณผ๋ช…' ์ถœ๋ ฅ. 
# ์ด๋•Œ 'ํ•™๊ณผ์ฝ”๋“œ'์ž…๋ ฅ๋˜์ง€ ์•Š์€ ํ•™์ƒ๋„ ์ถœ๋ ฅ
SELECT ํ•™๋ฒˆ, ์ด๋ฆ„, ํ•™์ƒ.ํ•™๊ณผ์ฝ”๋“œ, ํ•™๊ณผ๋ช…
FROM ํ•™์ƒ LEFT OUTER JOIN ํ•™๊ณผ
ON ํ•™์ƒ.ํ•™๊ณผ์ฝ”๋“œ = ํ•™๊ณผ.ํ•™๊ณผ์ฝ”๋“œ;

 

ํŠธ๋ฆฌ๊ฑฐ Trigger

Event๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ๊ด€๋ จ ์ž‘์—…์ด ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰๋˜๊ฒŒ ํ•˜๋Š” ์ ˆ์ฐจํ˜• SQL์ด๋‹ค. 

๋ฐ๋ฒ  ์ €์žฅ, ๋ณ€๊ฒฝ ๋ฐ ๋ฌด๊ฒฐ์„ฑ ์œ ์ง€, ๋กœ๊ทธ ์ถœ๋ ฅ ๋“ฑ์˜ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ.


์ค‘์š” ํ‚ค์›Œ๋“œ ์ˆœ ์žฌ์ •๋ฆฌ 

์ค‘์š”ํ•˜๋‹ˆ๊นŒ ๋‹ค ๋ณด๊ณ  ์ดํ•ดํ•˜์ž............
๋ฐ˜์‘ํ˜•

์ตœ๊ทผ๋Œ“๊ธ€

์ตœ๊ทผ๊ธ€

skin by ยฉ 2024 ttutta