Search

๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ CONNECT BY

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

๊ณ„์ธต์ ์ธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์—์„œ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•˜๋Š” ์ฟผ๋ฆฌ
์ฃผ๋กœ ์กฐ์ง๋„, ํŠธ๋ฆฌ ๊ตฌ์กฐ ๋ฐ์ดํ„ฐ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•

SELECT [ํ‘œํ˜„์‹๋“ค] FROM [ํ…Œ์ด๋ธ”๋ช…] START WITH [์‹œ์ž‘ ์กฐ๊ฑด] CONNECT BY [์žฌ๊ท€ ์กฐ๊ฑด];
SQL
๋ณต์‚ฌ
โ€ข
๊ธฐ๋ณธ ๊ตฌ์„ฑ ์š”์†Œ
๊ตฌ๋ฌธ
์„ค๋ช…
SELECT [ํ‘œํ˜„์‹๋“ค]
์กฐํšŒํ•  ์—ด์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์—ด๊ณผ ์ถ”๊ฐ€์ ์ธ ์—ด์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
FROM [ํ…Œ์ด๋ธ”๋ช…]
๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋Œ€์ƒ ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
START WITH [์‹œ์ž‘ ์กฐ๊ฑด]
๊ณ„์ธต ๊ตฌ์กฐ์˜ ์‹œ์ž‘์ ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ง€์ •๋œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์ด ์‹œ์ž‘์ ์ด ๋ฉ๋‹ˆ๋‹ค.
CONNECT BY [์žฌ๊ท€ ์กฐ๊ฑด]
์žฌ๊ท€์ ์ธ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ง€์ •๋œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์ด ์ด์ „ ํ–‰๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ˜•์„ฑํ•ฉ๋‹ˆ๋‹ค.
PRIOR
CONNECT BY ์ ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ด์ „ ํ–‰์„ ์˜๋ฏธํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค. PRIOR ์ž์‹ = ๋ถ€๋ชจ (์ •๋ฐฉํ–ฅ) : ๋ถ€๋ชจ ์ž์‹ PRIOR ๋ถ€๋ชจ = ์ž์‹ (์—ญ๋ฐฉํ–ฅ) : ์ž์‹ ๋ถ€๋ชจ
โ€ข
CONNECT BY ํ‚ค์›Œ๋“œ
ํ‚ค์›Œ๋“œ ๋ฐ ํ•จ์ˆ˜
์„ค๋ช…
CONNECT BY
๊ณ„์ธต์  ์ฟผ๋ฆฌ๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ํ‚ค์›Œ๋“œ๋กœ, ๋ถ€๋ชจ-์ž์‹ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
LEVEL
ํ˜„์žฌ ํ–‰์˜ ๊ณ„์ธต ๋ ˆ๋ฒจ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜์‚ฌ ์—ด์ž…๋‹ˆ๋‹ค.
CONNECT_BY_ROOT
๊ณ„์ธต ๊ตฌ์กฐ์—์„œ ์ตœ์ƒ์œ„ ๋ถ€๋ชจ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์˜์‚ฌ ์—ด์ž…๋‹ˆ๋‹ค.
CONNECT_BY_ISLEAF
ํ˜„์žฌ ํ–‰์ด ๋ฆฌํ”„(๋ง๋‹จ) ๋…ธ๋“œ์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ์˜์‚ฌ ์—ด์ž…๋‹ˆ๋‹ค.
SYS_CONNECT_BY_PATH
๊ณ„์ธต์  ์ฟผ๋ฆฌ์—์„œ ํ–‰์˜ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
NOCYCLE
์ˆœํ™˜(cycle)์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์˜ต์…˜์ž…๋‹ˆ๋‹ค.
CONNECT_BY_ISCYCLE
ํ˜„์žฌ ํ–‰์ด ์ˆœํ™˜ ๊ตฌ์กฐ์— ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ์˜์‚ฌ ์—ด์ž…๋‹ˆ๋‹ค.
ORDER BY SIBLINGS BY ์ปฌ๋Ÿผ
๊ฐ™์€ LEVEL ๋…ธ๋“œ ์‚ฌ์ด์—์„œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋กœ ์กฐ์ง๋„๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ

SELECT LEVEL , employee_id , first_name , last_name , job_id , manager_id FROM employees START WITH manager_id IS NULL CONNECT BY PRIOR employee_id = manager_id;
SQL
๋ณต์‚ฌ
โ€ข
START WITH manager_id IS NULL: ์ตœ์ƒ์œ„ ๋ถ€์„œ์˜ ์ง์›๋“ค์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ตœ์ƒ์œ„ ์ง์›์ด ๋งค๋‹ˆ์ €๊ฐ€ ์—†๋Š” ์ง์›๋“ค์ž…๋‹ˆ๋‹ค.
โ€ข
CONNECT BY PRIOR employee_id = manager_id: ๊ฐ ํ–‰์„ ์ด์ „ ํ–‰๊ณผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ํ–‰(PRIOR)์˜ employee_id๊ฐ€ ํ˜„์žฌ ํ–‰์˜ manager_id์™€ ๊ฐ™์€ ๊ฒฝ์šฐ, ๊ณ„์ธต์ ์œผ๋กœ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.
โ€ข
LEVEL: ๊ฐ ํ–‰์˜ ๊ณ„์ธต ๋ ˆ๋ฒจ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
โ—ฆ
1 : ๋Œ€ํ‘œ
โ—ฆ
2 : ๋ถ€์„œ์žฅ (์ƒ์‚ฌ)
โ—ฆ
3 : ์ง์› (๋ถ€ํ•˜)