๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DB/Oracle

[Oracle] ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ ์ดํ•ดํ•˜๊ธฐ (์„ค๋ช…/์˜ˆ์ œ)

by yunamom 2022. 4. 29.
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” yunamom ์ž…๋‹ˆ๋‹ค :D 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Oracle10g ๋ถ€ํ„ฐ CONNECT BY ์ ˆ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

๊ณ„์ธต๊ตฌ์กฐ ์ฟผ๋ฆฌ

 ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋ž€ ๋ฌด์—‡์ธ๊ฐ€? 

ํ•œ ํ…Œ์ด๋ธ”์— ๋‹ด๊ฒจ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ ˆ์ฝ”๋“œ๋“ค์ด ์„œ๋กœ ์ƒํ•˜ ๊ด€๊ณ„(๋ถ€๋ชจ, ์ž์‹) ๊ด€๊ณ„๋ฅผ ์ด๋ฃจ๋ฉฐ ์กด์žฌํ•  ๋•Œ, 

์ด ๊ด€๊ณ„์— ๋”ฐ๋ผ ๋ ˆ์ฝ”๋“œ๋ฅผ hierarchical(์ƒํ•˜์œ„) ํ•œ ๊ตฌ์กฐ๋กœ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ๋˜๋Š” SQL์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๊ตฌ๋ถ„ ์„ค๋ช…
LEVEL - ํ˜„์žฌ ํ…Œ์ด๋ธ”์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ปฌ๋Ÿผ
- ์˜ค๋ผํด์˜ ๋ชจ๋“  SQL์—์„œ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ ๋‹จ๊ณ„์ด๋ƒ๋ฅผ ์˜๋ฏธํ•จ
START WITH - ์–ด๋””๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•จ ( ์ตœ์ƒ์œ„ ๋ ˆ์ฝ”๋“œ )
CONNECT BY PRIOR - ๊ณ„์ธต ๊ตฌ์กฐ์—์„œ ๊ฐ ํ–‰์˜ ์—ฐ๊ฒฐ ๊ด€๊ณ„๋ฅผ ์„ค์ •
- EMP ํ…Œ์ด๋ธ”์—์„œ EMPNO, MGR ๊ฐ™์€
PRIOR - ์–ด๋””๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•ด ์ค€๋‹ค.

โœจSTART WITH  

- ๊ณ„์ธต ์งˆ์˜์˜ ๋ฃจํŠธ(๋ถ€๋ชจํ–‰)๋กœ ์‚ฌ์šฉ๋  ํ–‰์„ ์ง€์ •ํ•œ๋‹ค.

- ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

START WITH ๊ตฌ๋ฌธ์—์„œ๋Š” ์–ด๋–ค ๋ ˆ์ฝ”๋“œ๋ฅผ ์ตœ์ƒ์œ„ ๋ ˆ์ฝ”๋“œ๋กœ ์žก์„์ง€ ์ง€์ •ํ•œ๋‹ค.

โœจCONNECT_BY_ROOT  

๊ณ„์ธต๊ตฌ์กฐ ์ฟผ๋ฆฌ์—์„œ LEVEL์ด 0์ธ ์ตœ์ƒ์œ„ ๋กœ์šฐ์˜ ์ •๋ณด๋ฅผ ์–ป์–ด ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

-PRIOR์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์ธต๊ตฌ์กฐ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

-CONNECT BY PRIOR์ž์‹์ปฌ๋Ÿผ = ๋ถ€๋ชจ์ปฌ๋Ÿผ : ๋ถ€๋ชจ์—์„œ ์ž์‹์œผ๋กœ ํŠธ๋ฆฌ ๊ตฌ์„ฑ (Top Down)

-CONNECT BY PRIOR๋ถ€๋ชจ์ปฌ๋Ÿผ = ์ž์‹์ปฌ๋Ÿผ : ์ž์‹์—์„œ ๋ถ€๋ชจ๋กœ ํŠธ๋ฆฌ ๊ตฌ์„ฑ (Bottom Up)

SELECT LPAD(' ', 4*(LEVEL-1)) || ename ename, empno,
CONNECT_BY_ROOT  empno "Root empno", level
  FROM emp
 START WITH job='PRESIDENT'
CONNECT BY PRIOR empno=mgr;
 

ENAME                    EMPNO  Root empno     LEVEL
------------------     ------- -----------   -------
KING                      7839    7839           1
    JONES                 7566    7839           2
        SCOTT             7788    7839           3
            ADAMS         7876    7839           4
        FORD              7902    7839           3
            SMITH         7369    7839           4

 

๊ณ„์ธตํ˜• ๊ตฌ์กฐ (ํ•˜ํ–ฅ์‹)

โœจCONNECT_BY_ISLEAF  

๊ณ„์ธต๊ตฌ์กฐ ์ฟผ๋ฆฌ์—์„œ ๋กœ์šฐ์˜ ์ตœํ•˜์œ„ ๋ ˆ๋ฒจ(Leaf) ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ตœํ•˜์œ„ ๋ ˆ๋ฒจ์ด๋ฉด 1, ์•„๋‹ˆ๋ฉด 0

SELECT LPAD(' ', 4*(LEVEL-1)) || ename ename, empno,
       CONNECT_BY_ISLEAF "leaf", level
  FROM emp
 START WITH job='PRESIDENT'
CONNECT BY NOCYCLE PRIOR empno=mgr;
 
 
ENAME                     EMPNO       leaf      LEVEL
-------------------- ---------- ---------- ----------
KING                       7839          0          1
    JONES                  7566          0          2
        SCOTT              7788          0          3
            ADAMS          7876          1          4
        FORD               7902          1          3
    BLAKE                  7698          0          2
        MARTIN             7654          1          3
        TURNER             7844          1          3
        JAMES              7900          1          3
    CLARK                  7782          0          2
        MILLER             7934          1          3

โœจSYS_CONNECT_BY_PATH  

๊ณ„์ธต๊ตฌ์กฐ ์ฟผ๋ฆฌ์—์„œ ํ˜„์žฌ ๋กœ์šฐ ๊นŒ์ง€์˜ PATH ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

-- ์‚ฌ์ด์ฆˆ์กฐ์ ˆ
COL PATH FORMAT A40 
 
 
-- SYS_CONNECT_BY_PATH ์˜ˆ์ œ
SELECT LPAD(' ', 4*(LEVEL-1)) || ename ename, empno,
       SYS_CONNECT_BY_PATH(ename, '/') "PATH"
  FROM emp
 START WITH job='PRESIDENT'
CONNECT BY PRIOR empno=mgr;
 
 
ENAME                     EMPNO PATH
-------------------- ---------- ---------------------------
KING                       7839 /KING
    JONES                  7566 /KING/JONES
        SCOTT              7788 /KING/JONES/SCOTT
            ADAMS          7876 /KING/JONES/SCOTT/ADAMS
        FORD               7902 /KING/JONES/FORD
    BLAKE                  7698 /KING/BLAKE
        MARTIN             7654 /KING/BLAKE/MARTIN
        TURNER             7844 /KING/BLAKE/TURNER
        JAMES              7900 /KING/BLAKE/JAMES
    CLARK                  7782 /KING/CLARK
        MILLER             7934 /KING/CLARK/MILLER

Leaf Node(์ตœํ•˜์œ„, ๋งจ ๋งˆ์ง€๋ง‰ ๋) ์˜ ์ „์ฒด PATH ์ •๋ณด๋ฅผ ์ถœ๋ ฅ

SELECT LEVEL, SUBSTR(SYS_CONNECT_BY_PATH(ename, ','), 2) path
  FROM emp
 WHERE CONNECT_BY_ISLEAF = 1
 START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr;
 
 
    LEVEL PATH
--------- -------------------------
        4 KING,JONES,SCOTT,ADAMS
        3 KING,JONES,FORD
        3 KING,BLAKE,MARTIN
        3 KING,BLAKE,TURNER
        3 KING,BLAKE,JAMES
        3 KING,CLARK,MILLER

โœจORDER SIBLINGS BY  

๊ณ„์ธต๊ตฌ์กฐ ์ฟผ๋ฆฌ์—์„œ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ์ •๋ ฌ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

COL ename FORMAT A25 
COL ename2 FORMAT A10
 
 
-- ORDER SIBLINGS BY ์˜ˆ
-- ์ •๋ ฌ์ด ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ํ™•์ธ
SELECT LPAD(' ', 4*(LEVEL-1)) || ename ename, 
       ename ename2, empno, level
  FROM emp
 START WITH job='PRESIDENT'
CONNECT BY NOCYCLE PRIOR empno=mgr
 ORDER SIBLINGS BY ename2;
 
 
ENAME                ENAME2          EMPNO      LEVEL
-------------------- ---------- ---------- ----------
KING                 KING             7839          1
    BLAKE            BLAKE            7698          2
        JAMES        JAMES            7900          3
        MARTIN       MARTIN           7654          3
        TURNER       TURNER           7844          3
    CLARK            CLARK            7782          2
        MILLER       MILLER           7934          3
    JONES            JONES            7566          2
        FORD         FORD             7902          3
        SCOTT        SCOTT            7788          3
            ADAMS    ADAMS            7876          4
 
 
 
 
-- ORDER BY ์˜ˆ
-- ์ •๋ ฌ์ด ์ด์ƒํ•˜๊ฒŒ ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ํ™•์ธ
SELECT LPAD(' ', 4*(LEVEL-1)) || ename ename, 
       ename ename2, empno, level
  FROM emp
 START WITH job='PRESIDENT'
CONNECT BY NOCYCLE PRIOR empno=mgr
 ORDER BY ename2;
 
 
ENAME                ENAME2          EMPNO      LEVEL
-------------------- ---------- ---------- ----------
            ADAMS    ADAMS            7876          4
    BLAKE            BLAKE            7698          2
    CLARK            CLARK            7782          2
        FORD         FORD             7902          3
        JAMES        JAMES            7900          3
    JONES            JONES            7566          2
KING                 KING             7839          1
        MARTIN       MARTIN           7654          3
        MILLER       MILLER           7934          3
        SCOTT        SCOTT            7788          3
        TURNER       TURNER           7844          3

 

์ฐธ๊ณ  : http://www.gurubee.net/

300x250

์ฝ”๋“œ