DataBase๐Ÿ’พ

๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋ž€?

hae02y 2022. 8. 24. 14:14
๋ฐ˜์‘ํ˜•

๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋ž€ ํ…Œ์ด๋ธ”์— ๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ์ด๋‹ค. SQL-D ์‹œํ—˜ ๊ณต๋ถ€๋ฅผ ํ•  ๋•Œ, ์ •๋ง ์ฝ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ์ดํ•ด๊ฐ€ ์•ˆ๋˜์—ˆ๋˜ ๋ถ€๋ถ„์ด ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์˜€๋‹ค. ๋”ฐ๋ผ์„œ ๊ธ€์„ ์“ฐ๋ฉด์„œ ๋‹ค์‹œ ์ •๋ฆฌํ•ด๋ณธ๋‹ค.

1. ๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ

๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ๋ž€ ๋™์ผ ํ…Œ์ด๋ธ”์— ๊ณ„์ธต์ ์œผ๋กœ ์ƒ์œ„์™€ ํ•˜์œ„ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•œ๋‹ค.
์˜ˆ์‹œ๋กœ ๋“ค์–ด๋ณผ ํ•˜๋“œ์›จ์–ด์˜ ๊ณ„์ธต์  ๊ตฌ์กฐ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์œ„์™€ ํ•˜์œ„ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ค‘์•™์ฒ˜๋ฆฌ์žฅ์น˜์™€ ๊ธฐ์–ต์žฅ์น˜, ์ž…๋ ฅ์žฅ์น˜์˜ ์ƒ์œ„ ๋ฐ์ดํ„ฐ๋Š” ํ•˜๋“œ์›จ์–ด์ด๋ฉฐ, ํ•˜๋“œ์›จ์–ด์˜ ํ•˜์œ„ ๋ฐ์ดํ„ฐ๋Š” ์œ„์˜ ์„ธ ๊ฐœ์ด๋‹ค.

์ด๋ฅผ ํ…Œ์ด๋ธ”๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

2. ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์˜ ํ˜•ํƒœ

1
2
3
START WITH ์กฐ๊ฑด
CONNECT BY [NOCYCLE] ์กฐ๊ฑด
[ORDER SIBLINGS BY ์ปฌ๋Ÿผ];

๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์“ฐ์ธ๋‹ค. [] ์•ˆ์˜ ๋‚ด์šฉ์€ ํ•„์ˆ˜์ ์œผ๋กœ ๋“ค์–ด๊ฐ€์ง€ ์•Š์•„๋„ ๋˜๋Š” ๋‚ด์šฉ์ด๋‹ค. ์ด ๊ตฌ๋ฌธ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…์„ ํ•ด๋ณด์ž๋ฉด

START WITH ์ ˆ์€ ๊ทธ ์ด๋ฆ„์—์„œ๋„ ์ถ”์ธกํ•  ์ˆ˜๋„ ์žˆ๋“ฏ์ด ๊ณ„์ธต ๊ตฌ์กฐ ์ „๊ฐœ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ํ…Œ์ด๋ธ”์—์„œ START WITH ์ƒ์œ„์žฅ์น˜ IS NULL์„ ํ•˜๋ฉด 1ํ–‰์„ ์‹œ์ž‘์œ„์น˜๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
CONNECT BY ์ ˆ์€ ๋‹ค์Œ์— ์ „๊ฐœ๋  ์ž์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•˜๋Š”๋ฐ, PRIOR๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•ด์„œ ์ง€์ •ํ•œ๋‹ค. ์ด๋•Œ PRIOR ์ž์‹ = ๋ถ€๋ชจ ํ˜•ํƒœ๋ฅผ ์“ธ ๊ฒฝ์šฐ ์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ, PRIOR ๋ถ€๋ชจ = ์ž์‹ ํ˜•ํƒœ๋ฅผ ์“ธ ๊ฒฝ์šฐ ์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ๋ฅผ ํ•œ๋‹ค. ์œ„ ํ…Œ์ด๋ธ”์„ ์˜ˆ๋กœ ๋“ค์ž๋ฉด PRIOR ์žฅ์น˜์ด๋ฆ„ = ์ƒ์œ„์žฅ์น˜์˜ ๊ฒฝ์šฐ ์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ, PRIOR ์ƒ์œ„์žฅ์น˜ = ์žฅ์น˜์ด๋ฆ„์˜ ๊ฒฝ์šฐ ์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ์ด๋‹ค.
CONNECT BY ์ ˆ์—์„œ []์นœ NOCYCLE์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๊ฐœํ•˜๋ฉด์„œ ์ด๋ฏธ ๋‚˜ํƒ€๋‚œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋˜ ๋‚˜ํƒ€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ด์ค€๋‹ค.
ORDER SIBLINGS BY ์ ˆ์€ ํ˜•์ œ ๋…ธ๋“œ(๋™์ผ LEVEL) ์‚ฌ์ด์—์„œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•ด์ค€๋‹ค.

3. ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์˜ ์˜ˆ์‹œ

์œ„์˜ ์„ค๋ช…๋งŒ์œผ๋กœ๋Š” ์ด๊ฒŒ ๋ญ”์†Œ๋ฆฐ๊ฐ€…? ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“ค ๊ฒƒ์ด๋‹ค. ์ง์ ‘ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋ฅผ ์จ๋ณด๋ฉฐ ์ดํ•ด๋ฅผ ํ•ด๋ณด์ž.

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE TAB1 (
    ์žฅ์น˜์ด๋ฆ„ VARCHAR2(20),
    ์ƒ์œ„์žฅ์น˜ VARCHAR2(20) );
     
INSERT INTO TAB1 VALUES ('ํ•˜๋“œ์›จ์–ด', NULL);
INSERT INTO TAB1 VALUES ('์ค‘์•™์ฒ˜๋ฆฌ์žฅ์น˜', 'ํ•˜๋“œ์›จ์–ด');
INSERT INTO TAB1 VALUES ('๊ธฐ์–ต์žฅ์น˜', 'ํ•˜๋“œ์›จ์–ด');
INSERT INTO TAB1 VALUES ('์ž…์ถœ๋ ฅ์žฅ์น˜', 'ํ•˜๋“œ์›จ์–ด');
INSERT INTO TAB1 VALUES ('์ž…๋ ฅ์žฅ์น˜', '์ž…์ถœ๋ ฅ์žฅ์น˜');
INSERT INTO TAB1 VALUES ('์ถœ๋ ฅ์žฅ์น˜', '์ž…์ถœ๋ ฅ์žฅ์น˜');
 
SELECT * FROM TAB1;

์ปฌ๋Ÿผ ์ด๋ฆ„์€ ๋ณด๊ธฐ ํŽธํ•˜๋„๋ก ๊ทธ๋ƒฅ ํ•œ๊ธ€์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์ด sql๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ์œ„์—์„œ ๋ณด์•˜๋˜ ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๊ฐ€ ๋œ๋‹ค. ๋จผ์ € ์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ๋ฌธ์„ ๋จผ์ € ์‹คํ–‰์‹œ์ผœ๋ณด์ž.

1
2
3
SELECT ์žฅ์น˜์ด๋ฆ„, ์ƒ์œ„์žฅ์น˜, LEVEL FROM TAB1
START WITH ์ƒ์œ„์žฅ์น˜ IS NULL
CONNECT BY PRIOR ์žฅ์น˜์ด๋ฆ„ = ์ƒ์œ„์žฅ์น˜;

๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ์ „๊ฐœ๋˜๋Š”์ง€ ๋ณด๊ธฐ ํŽธํ•˜๋„๋ก LEVEL์ด๋ผ๋Š” ๊ฐ€์ƒ ์ปฌ๋Ÿผ์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. LEVEL์€ ๋ฃจํŠธ ๋ฐ์ดํ„ฐ๋Š” 1, ๊ทธ ํ•˜์œ„ ๋ฐ์ดํ„ฐ๋Š” 2, ๊ทธ ํ•˜์œ„ ๋ฐ์ดํ„ฐ์˜ ํ•˜์œ„ ๋ฐ์ดํ„ฐ๋Š” 3… ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฆฌํ”„ ๋ฐ์ดํ„ฐ๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•œ๋‹ค. ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๋‹ค๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ์„ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ์ด๋‹ค.

์œ„ SQL๋ฌธ์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ณด์ž๋ฉด START WITH ์ƒ์œ„์žฅ์น˜ IS NULL์„ ํ†ตํ•ด ํ•˜๋“œ์›จ์–ด๋ฅผ ๊ณ„์ธตํ˜• ์ „๊ฐœ์˜ ์‹œ์ž‘์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์ด๋•Œ ์‹œ์ž‘์ ์€ ๋ฃจํŠธ ๋ฐ์ดํ„ฐ๋กœ LEVEL์€ 1์ด๋‹ค. ์ด์ œ CONNECT BY PRIOR ์žฅ์น˜์ด๋ฆ„ = ์ƒ์œ„์žฅ์น˜ ๋ฅผ ํ†ตํ•ด ์ž์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๊ฐœํ•œ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด์ „ ๊ณ„์ธต์˜ ์žฅ์น˜์ด๋ฆ„(ํ•˜๋“œ์›จ์–ด)๋ฅผ ์ƒ์œ„์žฅ์น˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ธฐ์–ต์žฅ์น˜, ์ž…์ถœ๋ ฅ์žฅ์น˜, ์ค‘์•™์ฒ˜๋ฆฌ์žฅ์น˜๋Š” LEVEL 2๊ฐ€ ๋œ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ์ด์ „ ๊ณ„์ธต์˜ ์žฅ์น˜์ด๋ฆ„(์ž…์ถœ๋ ฅ์žฅ์น˜)๋ฅผ ์ƒ์œ„์žฅ์น˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ž…๋ ฅ์žฅ์น˜, ์ถœ๋ ฅ์žฅ์น˜๊ฐ€ LEVEL 3์ด ๋œ๋‹ค.

1
2
3
SELECT ์žฅ์น˜์ด๋ฆ„, ์ƒ์œ„์žฅ์น˜, LEVEL FROM TAB1
START WITH ์ƒ์œ„์žฅ์น˜ = '์ž…์ถœ๋ ฅ์žฅ์น˜'
CONNECT BY PRIOR ์ƒ์œ„์žฅ์น˜ = ์žฅ์น˜์ด๋ฆ„;

๋ฐ˜๋Œ€๋กœ PRIOR ์ƒ์œ„์žฅ์น˜ = ์žฅ์น˜์ด๋ฆ„์„ ์จ์„œ ์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ๋ฅผ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ด๋‚ผ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ

1
2
3
4
SELECT ์žฅ์น˜์ด๋ฆ„, ์ƒ์œ„์žฅ์น˜, LEVEL FROM TAB1
START WITH ์ƒ์œ„์žฅ์น˜ IS NULL
CONNECT BY PRIOR ์žฅ์น˜์ด๋ฆ„ = ์ƒ์œ„์žฅ์น˜
ORDER SIBLINGS BY ์žฅ์น˜์ด๋ฆ„ DESC;

ORDER SIBLINGS BY ์ ˆ์„ ์ด์šฉํ•ด ๊ฐ™์€ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ ๊ฐ„์— ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•