ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • SQL - ๋ทฐ, ์‹œํ€€์Šค, ์ธ๋ฑ์Šค, ์‚ฌ์šฉ์ž ๊ด€๋ฆฌ
    DATABASE 2020. 7. 25. 22:57

    ๐ŸŽฏ SQL ์˜ค๋ผํด ๊ฐ์ฒด์˜ ๋ทฐ, ์‹œํ€€์Šค, ์ธ๋ฑ์Šค์™€ ์‚ฌ์šฉ์ž ๊ด€๋ฆฌ๋ฅผ ์•Œ์•„๋ณธ๋‹ค.

     

     

     

     

    ๋ทฐ (VIEW)

     

    ๊ธฐ์กด์˜ ํ…Œ์ด๋ธ”์—์„œ ํ•„์š”ํ•œ ๊ฒƒ๋งŒ ๋ฝ‘์•„์„œ ํ•˜๋‚˜์˜ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”์„ ๋‹ค์‹œ ๋งŒ๋“œ๋Š” ๊ฒƒ.

    ๋ทฐ์—์„œ ์ฐธ์กฐํ•˜๋Š” ์›๋ณธํ…Œ์ด๋ธ”์„ ๊ธฐ๋ณธํ…Œ์ด๋ธ”(base table)์ด๋ผ๊ณ  ํ•œ๋‹ค.

    10๋ฒˆ ๋ถ€์„œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋งค๋ฒˆ ์…€๋ ‰ํ•˜๊ธฐ๋ณด๋‹ค ๊ทธ๋ƒฅ ๋”ฐ๋กœ ๋ทฐ๋ฅผ ๋งŒ๋“ค์–ด๋‘๋ฉด ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•˜๋‹ค.

    ๋ณด์•ˆ์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์„ ํƒ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ ‘๊ทผ์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค. 

    ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ๋ฅผ ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“ค์ˆ˜ ์žˆ๋‹ค. ๋งค๋ฒˆ ๋งŒ๋“ค๊ธฐ ๋ณต์žกํ•œ ์กฐ์ธ๋ฌธ์„ ๋ทฐ๋กœ ๋งŒ๋“ค์–ด๋†“์œผ๋ฉด ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    GRANT CREATE VIEW TO SCOTT;

     

    ๋จผ์ € SYS๊ณ„์ •์—์„œ ์Šค์ฝง๊ณ„์ •์— ๋ทฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์ค€๋‹ค.

     

    CREATE VIEW EMP_VIEW
    AS
    SELECT EMPNO, ENAME, SAL, HIREDATE
    FROM EMP
    WHERE DEPTNO = 10;
    
    
    SELECT * FROM EMP_VIEW;
    
    
    
    CREATE VIEW DEPT_VIEW
    AS
    SELECT DEPTNO NO, DNAME NAME
    FROM DEPT;
    
    
    SELECT VIEW_NAME
    FROM USER_VIEWS;

     

    ์›ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ๋ฝ‘์•„ ๋ทฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

     

    ํ•œ๋ฒˆ ๋ทฐ๋ฅผ ๋งŒ๋“ค์–ด๋†“์œผ๋ฉด ์˜๊ตฌ ์ €์žฅ๋จ.

     

    ์‚ฌ์›์ •๋ณด๋ฅผ ์ผ๋ฐ˜์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰ํ•  ๋•Œ, ๊ธ‰์—ฌ ์ •๋ณด๋Š” ๋งค์šฐ ๋ฏผ๊ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์…€๋ ‰ํ•  ๋•Œ ๋ทฐ๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ๋˜์ง€ ์•Š๋„๋ก ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํ…Œ์ด๋ธ”์„ SELECTํ•  ๊ฒฝ์šฐ, ํ–‰์„ ์ œํ•œํ•  ์ˆœ ์žˆ์ง€๋งŒ ํŠน์ • ์ปฌ๋Ÿผ์„ ์ œํ•œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿด ๋• ๋ทฐ๋ฅผ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์›”๊ธ‰์ปฌ๋Ÿผ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ปฌ๋Ÿผ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ทฐ ๋งŒ๋“ค๊ธฐ
    
    
    CREATE VIEW EMP_VIEW4
    AS
    SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, COMM, DEPTNO
    FROM EMP;
    
    SELECT * FROM EMP_VIEW4;
    

     

    USER_VIEWS ๋ฐ์ดํ„ฐ์‚ฌ์ „์˜ TEXT ์ปฌ๋Ÿผ์„ ํ™•์ธํ•ด๋ณด๋ฉด ๋ทฐ๋ฅผ ์ •์˜ํ•  ๋•Œ ์ผ๋˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋งŒ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด์žˆ๋‹ค.

    ๋ทฐ๋Š” ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ๋””์Šคํฌ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•ด ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๊ฒŒ ์•„๋‹ˆ๋‹ค.

     

     

    ๋ทฐ ์ˆ˜์ •

    ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๋ทฐ์˜ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ.

    CREATE OR REPLACE ๋ฌธ์„ ์ด์šฉํ•œ๋‹ค.

    ๊ธฐ์กด์— ๋ทฐ๊ฐ€ ์žˆ๋‹ค๋ฉด REPLACE๋กœ ๋ฎ์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜๊ณ , ์—†์—ˆ์œผ๋ฉด CREATE๋กœ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

     

    CREATE OR REPLACE VIEW DEPT_VIEW
    AS
    SELECT DEPTNO NO, DNAME NAME, LOC
    FROM DEPT;
    
    DESC DEPT_VIEW;

     

     

    ๋ทฐ ์ข…๋ฅ˜

     

    ๋‹จ์ˆœ๋ทฐ : ํ•˜๋‚˜์˜ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด ์ •์˜ํ•œ ๋ทฐ

    ๋‹จ์ˆœ๋ทฐ์—์„œ DML๋ฌธ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ฒฐ๊ณผ๊ฐ€ ์‹ค์ œ๋กœ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์— ๋ฐ˜์˜๋œ๋‹ค.

    ๋ทฐ์ด๋ฆ„ ๋’ค์— ์ปฌ๋Ÿผ ๋ณ„์นญ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ทฐ ์ƒ์„ฑ์‹œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜๋“œ์‹œ ์ปฌ๋Ÿผ ๋ณ„์นญ์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

    ๋‹จ์ˆœ ๋ทฐ๊ฐ€ ๊ทธ๋ฃนํ•จ์ˆ˜, GROUP BY, DISTINCT๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ์—” DML ์ž‘์—… ๋ถˆ๊ฐ€๋Šฅ.

     

    ๋ณตํ•ฉ๋ทฐ : ๋‘๊ฐœ ์ด์ƒ์˜ ๊ธฐ๋ณธํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์ •์˜ํ•œ ๋ทฐ

    ๋งค๋ฒˆ ์กฐ์ธํ•  ํ•„์š” ์—†์ด ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐ์ธ ์ฟผ๋ฆฌ๋ฌธ์„ ๋ทฐ๋กœ ์ƒ์„ฑํ•œ ๊ฒŒ ๋ณตํ•ฉ๋ทฐ๋‹ค.

     

    CREATE VIEW EMP_DEPT_VIW
    AS
    SELECT EMPNO, ENAME, DNAME
    FROM EMP, DEPT
    WHERE EMP.DEPTNO = DEPT.DEPTNO
    AND DEPT.DEPTNO = 30;
    
    
    SELECT * FROM EMP_DEPT_VIEW;
    
    
    -- ๋ณต์žกํ•œ ์กฐ์ธ๋ฌธ์„ ๋ทฐ๋กœ ๋งŒ๋“ค์–ด ๊ณ„์† ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

     

     

    SELECT * FROM EMP_VIEW;
    
    DELETE FROM EMP_VIEW
    WHERE EMPNO= 7782;
    
    SELECT * FROM EMP_VIEW;
    
    ROLLBACK;
    
    
    UPDATE EMP_VIEW
    SET ENAME='MILLY' WHERE ENAME = 'CLARK';
    
    ROLLBACK;

     

     

    ๋ทฐ์˜ ์ œ์•ฝ์กฐ๊ฑด

     

    WITH CHECK OPTION

    WHERE ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ INSERT์™€ UPDATE ๊ฐ€๋Šฅ

    WITH READ ONLY

    ์ฝ๊ธฐ์ „์šฉ์œผ๋กœ ์ž๋ฃŒ ๊ฒ€์ƒ‰๋งŒ ๊ฐ€๋Šฅํ•˜๊ฒŒ ์„ค์ •.

    DML์ž‘์—…์€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ƒํƒœ

     

     

    ๋ทฐ ์ œ๊ฑฐ

     

    DROP VIEW ๋ทฐ์ด๋ฆ„;

     

    ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์— ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

     

     

    ์ธ๋ผ์ธ๋ทฐ

     

    ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ณดํ†ต WHERE์ ˆ์—์„œ ์‚ฌ์šฉ๋˜๋Š”๋ฐ ์ด๊ฑด FROM ์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ํ˜•ํƒœ๋‹ค. ํ”„๋กฌ์ ˆ์—” ํ…Œ์ด๋ธ”๋ช…์ด ์™€์•ผํ•˜์ง€๋งŒ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ํ•˜๋‚˜์˜ ๊ฐ€์ƒํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. FROM ์ ˆ์—์„œ ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ” ํฌ๊ธฐ๊ฐ€ ํด ๊ฒฝ์šฐ ํ•„์š”ํ•œ ํ–‰๊ณผ ์ปฌ๋Ÿผ๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ง‘ํ•ฉ์„ ์žฌ์ •์˜ํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฌธ์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    SELECT E.DEPTNO, TOTAL_SUM, TOTAL_AVG, CNT
    FROM (SELECT DEPTNO, SUM(SAL) TOTAL_SUM, AVG(SAL) TOTAL_AVG, COUNT(*) CNT
    FROM EMP
    GROUP BY DEPTNO) E, DEPT D
    WHERE E.DEPTNO = D.DEPTNO;
    
    -- ์„œ๋ธŒ์ฟผ๋ฆฌ ํฌ์ŠคํŠธ์˜ ์ธ๋ผ์ธ๋ทฐ ์ฐธ๊ณ 

     

     

     

    ์‹œํ€€์Šค SEQUENCE

     

    ๋ฌด์ˆ˜ํžˆ ๋งŽ์€ ๊ฒŒ์‹œ๋ฌผ ์ค‘ ์›ํ•˜๋Š” ๊ธ€์„ ์ฐพ๊ณ ์‹ถ์„ ๋•Œ, ๊ธ€์ œ๋ชฉ์€ ์ค‘๋ณต๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด์„œ ๊ธ€ ๋ฒˆํ˜ธ๋กœ ์ฐพ๋Š”๊ฒŒ ์ข‹๋‹ค. ๋ณดํ†ต ๊ธ€๋ฒˆํ˜ธ์— ๊ธฐ๋ณธํ‚ค ์ œ์•ฝ์กฐ๊ฑด์„ ๋ถ™์ธ๋‹ค.

    ์‹œํ€€์Šค๋Š” ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ๊ณ ์œ ํ•œ ์ˆซ์ž๋กœ ํŠน์ • ์ปฌ๋Ÿผ๊ฐ’์„ ๋„˜๋ฒ„๋งํ•œ๋‹ค.

    ์‹œํ€€์Šค์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์˜ต์…˜์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

     

    CREATE SEQUENCE ์‹œํ€€์Šค๋ช…
    [START WITH N]
    [INCREMENT BY N]
    [MAXVALUE N]
    [MINVALUE N]
    [CYCLE]
    [CACHE]

     

    ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ๋„˜๋ฒ„๋ง์ด ์‹คํ–‰๋œ๋‹ค.

    ๋ฒˆํ˜ธ๋Š” ์Šคํƒ€ํŠธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ธํฌ๋ฆฌ๋จผํŠธ ๊ฐ’๋งŒํผ ์ฆ๊ฐ€ํ•˜๋ฉฐ ๋งฅ์Šค๋ฐธ๋ฅ˜๊นŒ์ง€ ๋„๋‹ฌํ•œ๋‹ค. ๋งฅ์Šค๋ฐธ๋ฅ˜๋ฅผ ๋„˜์–ด์„œ๋Š” ์ˆœ๊ฐ„ ์‹ธ์ดํด์ด ์‹คํ–‰๋˜์–ด ๋ฏผ๋ฐธ๋ฅ˜์—์„œ ๋‹ค์‹œ ๋งฅ์Šค๊นŒ์ง€ ๊ฐ„๋‹ค.

    ์‚ฌ์ดํด ๋Œ์•˜์„ ๋•Œ ์ฐ๋Š” ์ตœ๋Œ“๊ฐ’์ด MAXVALUE ์ด๋ฉฐ, ์‹ธ์ดํด์„ ์ƒˆ๋กœ ์‹œ์ž‘ํ•  ๋•Œ์˜ ์‹œ์ž‘๊ฐ’์ด MINVALUE ์ด๋‹ค.

     

    CREATE SEQUENCE EMP_SEQ
    INCREMENT BY 1
    START WITH 100
    MAXVALUE 9999
    NOCACHE
    NOCYCLE;
    
    
    SELECT SEQUENCE_NAME, MIN_VALUE, MAX_VALUE, INCREMENT_BY
    FROM USER_SEQUENCES;
    
    
    -- ์‚ฌ์ดํด ์—†์ด 100๋ฒˆ๋ถ€ํ„ฐ 9999๋ฒˆ๊นŒ์ง€์˜ ์‹œํ€€์Šค๋ฅผ ์‹คํ–‰ํ•œ๋‹ค

     

     

    ์‹œํ€€์Šค ์‚ฌ์šฉ

    ์‹œํ€€์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ์‹œํ€€์Šค ๊ฐ์ฑ„๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

     

    1) ์‹œํ€€์Šค๋ช….NEXTVAL

    ์ง€์ •๋œ ์‹œํ€€์Šค์—์„œ ์ˆœ์ฐจ์ ์ธ ์‹œํ€€์Šค ๋ฒˆํ˜ธ๋ฅผ ์ถ”์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

    ๋„ฅ์ŠคํŠธ๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ˆœ๊ฐ„ ๊ฐ’์ด ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€๋˜๋‹ˆ๊นŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋‘๋ฒˆ์—ฐ์† INSTERTํ•˜์ง€ ์•Š๊ฒŒ ์ฃผ์˜ํ•  ๊ฒƒ.

     

    2) ์‹œํ€€์Šค๋ช….CURRVAL

    ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฉ๊ธˆ ์ถ”์ถœํ•œ ์‹œํ€€์Šค ๋ฒˆํ˜ธ๋ฅผ ์ฐธ์กฐํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

    ๋ฐ˜๋“œ์‹œ NEXTVAL๋กœ ๋ฒˆํ˜ธ๋ฅผ ์ถ”์ถœํ•œ ํ›„์— ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

     

    INSERT INTO EMP
    VALUES (EMP_SEQ.NEXTVAL, 'ํ™๊ธธ๋™', '์ธ์‚ฌ', NULL, SYSDATE,
    2500, 300, 40);
    
    
    SELECT EMPNO, ENAME, JOB
    FROM EMP
    WHERE DEPTNO = 40;
    
    
    SELECT EMP_SEQ.CURRVAL FROM DUAL;

     

     

    ์‹œํ€€์Šค ์‚ญ์ œ

    DROP SEQUENCE EMP_SEQ;

    ์‹œํ€€์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด ๋“œ๋žํ•˜๊ณ  ๋‹ค์‹œ ๋งŒ๋“ค์–ด์•ผ๋˜๋ฏ€๋กœ ์ƒ์„ฑ ์‹œ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

     

     

    ์‹œํ€€์Šค ๋ณ€๊ฒฝ

     

    ALTER SEQUENCE EMP_SEQ
    INCREMENT BY 2
    MAXVALUE 10000
    NOCACHE
    NOCYCLE;

     

    ์‹œํ€€์Šค์˜ ์˜ต์…˜์„ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ ๋‹ค์Œ ์‹œํ€€์Šค ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์ ์šฉ๋˜์–ด ์ถœ๋ ฅ๋œ๋‹ค. 

    START WITH ์˜ต์…˜์€ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ MAXVALUE ๊ฐ’์€ ํ˜„์žฌ ์‹œํ€€์Šค ๋ฒˆํ˜ธ๋ณด๋‹ค ํฐ ๋ฒˆํ˜ธ๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

     

    ์‹œํ€€์Šค ๋ณ€๊ฒฝ ๊ฒฐ๊ณผ, ๋ฒˆํ˜ธ๊ฐ€ 2์”ฉ ์ฆ๊ฐ€

     

     

    CREATE SEQUENCE DEPT_DEPTNO_SEQ
    START WITH 10
    INCREMENT BY 10
    MAXVALUE 100
    MINVALUE 5
    CYCLE 
    NOCACHE;
    
    
    SELECT DEPT_DEPTNO_SEQ.NEXTVAL,
    DEPT_DEPTNO_SEQ.CURRVAL
    FROM DUAL;
    
    
    -- ์ฆ๊ฐ€๊ฐ’์— ๋งˆ์ด๋„ˆ์Šค๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
    -- 100๋ถ€ํ„ฐ 0๊นŒ์ง€ -10์”ฉ ๋–จ์–ด์ง€๊ณ  ์‚ฌ์ดํด์€ 150๋ถ€ํ„ฐ ์‹œ์ž‘.
    
    CREATE SEQUENCE DEPT_DEPTNO_SEQ
    START WITH 100
    INCREMENT BY -10
    MAXVALUE 150
    MINVALUE 0
    CYCLE 
    NOCACHE;

     

     

     

    ์ธ๋ฑ์Šค INDEX

     

    0~10000๊นŒ์ง€์˜ ๋ฒˆํ˜ธ ์ค‘์—์„œ 9999๋ฅผ ์ฐพ์„ ๋•Œ ์–ด๋–ค ๊ฒ€์ƒ‰ ๋ฐฉ๋ฒ•์ด ๋” ๋น ๋ฅผ๊นŒ? 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š”๊ฒƒ๋ณด๋‹จ ๋’ค์—์„œ ํ•œ ๋ฒˆ ๋นจ๋ฆฌ ์ฐพ๋Š” ๊ฒŒ ๋” ์‰ฝ๋‹ค. 

    ์ธ๋ฑ์Šค๋Š” ๋ฐ์ดํ„ฐ์˜ ์‹ค์ œ ์ €์žฅ์œ„์น˜์ธ ROWID ๊ณ ์œ ๋ฒˆํ˜ธ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น ๋ฅธ ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ผ๋‹จ ์ˆซ์ž๋ฅผ ๋ฐ˜์œผ๋กœ ๋š ์ž˜๋ผ์„œ ๊ฐ€์ง€์น˜๊ธฐ ํ•˜๋ฉฐ ์ˆซ์ž๋ฅผ ์ฐพ๋Š” ๊ฒŒ ํŠธ๋ฆฌ์ธ๋ฑ์Šค ๋ฐฉ๋ฒ•์ด๋‹ค.

    ์ธ๋ฑ์Šค ์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฑธ ํ’€์Šค์บ”์ด๋ผ๊ณ  ํ•œ๋‹ค. ํ–‰์ด 3๊ฐœ๋ฐ–์— ์—†๋Š” ๊ฒฝ์šฐ์—” ํ’€์Šค์บ”์ด ๋น ๋ฅด๋‹ค.

     

     

    ์ธ๋ฑ์Šค ์ƒ์„ฑ

     

    ์œ ๋‹ˆํฌ,PK ์ œ์•ฝ์กฐ๊ฑด ์ง€์ • ์‹œ ์œ ๋‹ˆํฌ ์ธ๋ฑ์Šค๊ฐ€ ์ž๋™์ƒ์„ฑ๋œ๋‹ค.

    ์ˆ˜๋™์ƒ์„ฑ์€ ํ•œ๊ฐœ ์ปฌ๋Ÿผ ๋˜๋Š” ์—ฌ๋Ÿฌ ์ปฌ๋Ÿผ(๋ณตํ•ฉ ์ธ๋ฑ์Šค)์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    CREATE [UNIQUE] INDEX ์ธ๋ฑ์Šค๋ช…
    ON ํ…Œ์ด๋ธ”(์ปฌ๋Ÿผ[,์ปฌ๋Ÿผ]);

     

     

    ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

    • ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์„ ๋•Œ
    • ์ปฌ๋Ÿผ๊ฐ’์˜ ๋ฒ”์œ„๊ฐ€ ๋„“์„ ๋•Œ
    • WHERE์ ˆ์ด๋‚˜ JOIN ์กฐ๊ฑด์— ์‚ฌ์šฉ๋˜๋Š” ์ปฌ๋Ÿผ
    • ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ์•ฝ 2 ~ 4% ์ด๋‚ด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ
    • NULL ๊ฐ’์ด ๋งŽ์€ ์ปฌ๋Ÿผ ( NULL ๊ฐ’์€ ์ธ๋ฑ์Šค์— ํฌํ•จ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค ํฌ๊ธฐ๊ฐ€ ๊ฐ์†Œ)

     

    ์ธ๋ฑ์Šค ์•ˆํ†ตํ•˜๊ณ  ํ’€์Šค์บ” ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

    • ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์„ ๋•Œ
    • WHERE๋ฌธ์— ํ•ด๋‹น ์ปฌ๋Ÿผ์ด ์ž์ฃผ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„๋•Œ
    • ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์ „์ฒด๋ฐ์ดํ„ฐ์˜ 10~15% ์ด์ƒ์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ
    • ํ…Œ์ด๋ธ”์— DML์ž‘์—…์ด ๋งŽ์„ ๋•Œ
    • ์ธ๋ฑ์Šค๊ฐ€ ์ ์šฉ๋œ ์ปฌ๋Ÿผ์ด NOT์—ฐ์‚ฐ์ž, IS NULL,ํ•จ์ˆ˜์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ

     

     

    ์ธ๋ฑ์Šค ์‚ญ์ œ

    DROP INDEX ์ธ๋ฑ์Šค๋ช…;

     

     

     

    ์‚ฌ์šฉ์ž๊ด€๋ฆฌ

     

    ๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๋“ค์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ ๋ณด์•ˆ์„ ์œ„ํ•ด ์ธ์ฆ๊ณผ ๊ถŒํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•œ๋‹ค.

    ์ธ์ฆ(Authentication)์ด๋ž€ ์‹œ์Šคํ…œ ์ˆ˜์ค€์—์„œ ์ ‘๊ทผ๊ณผ ์‚ฌ์šฉ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๊ณ , ๊ถŒํ•œ(Authorization)์ด๋ž€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด ์‚ฌ์šฉ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

     

     

    ์‚ฌ์šฉ์ž ์ƒ์„ฑ

    ์‚ฌ์šฉ์ž ์ƒ์„ฑ์€ ๋ฐ˜๋“œ์‹œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ด€๋ฆฌ์ž(DBA) SYS ๊ณ„์ •์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    CREATE USER ๊ณ„์ •
    IDENTIFIED BY ๋น„๋ฐ€๋ฒˆํ˜ธ;
    
    
    
    CREATE USER USER01 --์œ ์ €๋ช…
    IDENTIFIED BY ORACLE; --ํŒจ์Šค์›Œ๋“œ

     

    ์ด ๋กœ๊ทธ์ธ ํŒจ์Šค์›Œ๋“œ๋งŒ ๋งž์œผ๋ฉด ์ธ์ฆ ํ—ˆ์šฉํ•œ๋‹ค.

    ๊ณ„์ •์„ ์ƒ์„ฑํ–ˆ๋‹ค๊ณ  ํ•ด์„œ ์ ‘์† ํ›„ ๋ฐ”๋กœ ๋ชจ๋“  ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค. ๊ฐœ๋ณ„์ ์ธ ์ธ์ฆ๊ณผ ๊ถŒํ•œ์ด ํ•„์š”ํ•˜๋‹ค.

     

     

    ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ

    ALTER USER USER01
    IDENTIFIED BY user01;

     

     

    ๊ถŒํ•œ ์ƒ์„ฑ

     

    ์‹œ์Šคํ…œ๊ถŒํ•œ : CREATE USER, DROP USER, DROP ANY TABLE ...

    ์‚ฌ์šฉ์ž์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ๊ถŒํ•œ : CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE VIEW ... (์ ‘์†๊ถŒํ•œ, ํ…Œ์ด๋ธ”์‚ฌ์šฉํ• ์ˆ˜์žˆ๋Š” ๊ถŒํ•œ)

     

     

    GRANT CREATE SESSION, CREATE TABLE
    TO USER01;
    
    --  USER01 ํ…Œ์ด๋ธ”์— ์˜ค๋ผํด ์ ‘์†๊ถŒํ•œ๊ณผ ํ…Œ์ด๋ธ” ์ƒ์„ฑ๊ถŒํ•œ์„ ๋ถ€์—ฌ

     

     

    ์ƒˆ ์ ‘์† ์ƒ์„ฑ

     

    USER01 ๋กœ ์ ‘์†ํ•ด์„œ ๊ฐ€์ง„ ๊ถŒํ•œ ๋ณด๊ธฐ

     

     

    ์‹œ์Šคํ…œ ๊ถŒํ•œ ํšŒ์ˆ˜

    REVOKE CREATE SESSION
    FROM USER01;

     

     

    ๊ฐ์ฒด ๊ถŒํ•œ

    ํ…Œ์ด๋ธ”, ๋ทฐ, ์‹œํ€€์Šค, ํ”„๋กœ์‹œ์ € ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ

     

    -- ์Šค์ฝง์ด USER01์—๊ฒŒ ํ…Œ์ด๋ธ” ์ค‘ DEPT๋งŒ ๊ณต์œ ํ–ˆ์Œ
    GRANT SELECT
    ON DEPT
    TO USER01;

     

    DEPT๋ถˆ๋Ÿฌ์˜ฌ๋•Œ ์•ž์— ์Šคํ‚ค๋งˆ ๋ถ™์—ฌ์ค˜์•ผํ•จ. ์Šค์ฝง๊บผ๋ผ

     

    -- SCOTT ๊ณ„์ •์—์„œ USER01๊ณ„์ •์— ๊ถŒํ•œ ๋ถ€์—ฌ 
    
    GRANT UPDATE(DNAME)
    ON DEPT
    TO USER01;
    
    -- USER01 ๊ณ„์ •์—์„œ ๋ฐ์ดํ„ฐ ์ˆ˜์ •
    
    UPDATE SCOTT.DEPT
    SET DNAME='์ธ์‚ฌ'
    WHERE DEPTNO = 40;

     

     

    WITH GRANT OPTION

    ๊ถŒํ•œ์„ ๋ถ€์—ฌ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ๋ถ€์—ฌ ๋ฐ›์€ ๊ถŒํ•œ์„ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋‹ค์‹œ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•

     

     

    ๊ฐ์ฒด๊ถŒํ•œ ํšŒ์ˆ˜

    REVOKE ๊ฐ์ฒด๊ถŒํ•œ
    ON ๊ฐ์ฒด๋ช…
    FROM ์‚ฌ์šฉ์ž๊ณ„์ •|ROLE|PUBLIC
    [CASCADE CONSTRAINTS];

     

     

    ๋กค

    ์ธํ„ฐ๋„ท ์นดํŽ˜๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ์ฃผ๋ฉด ์ฝ๊ธฐ, ์“ฐ๊ธฐ, ํšŒ์›๊ด€๋ฆฌ ๋‹ค ๊ฐ€๋Šฅํ•˜๋‹ค. ๋กค์„ ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ๋กค์€ ๊ถŒํ•œ์˜ ๋ฌถ์Œ์ด๋‹ค. ๋ถ€์—ฌ๋œ ๋กค์€ ํ™œ์„ฑํ™” ๋น„ํ™œ์„ฑํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    ์˜ค๋ผํด์—” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ์‚ฌ์ „์ •์˜๋œ(BUILT-IN)๋กค์ด ์žˆ๋‹ค.

    CONNECT ์ ‘์†๊ถŒํ•œ ๋กค

    RESOURCE ํ…Œ์ด๋ธ” ์‚ฌ์šฉ๊ถŒํ•œ ๋กค

    DBA๋Š” ์‹œ์Šค์˜ ๊ถŒํ•œ์ด๋ผ ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฃผ๋ฉด ์•ˆ๋œ๋‹ค.

     

    SELECT role
    FROM dba_roles;

    SYS ๊ณ„์ • ์ ‘์†ํ•ด์„œ dba roles ๋ฐ์ดํ„ฐ ์‚ฌ์ „์„ ํ†ตํ•ด์„œ ๋นŒํŠธ์ธ ๋กค์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜์žˆ๋‹ค.

    GRANT connect, resource
    To ๊ณ„์ •๋ช…

    ์ด ๋‘๊ฐ€์ง€ ๋กค์„ ๋ถ€์—ฌํ•˜๋Š”๊ฒŒ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ด๋‹ค.

     

     

    ์‚ฌ์šฉ์ž ๋กค ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ

     

    -- ์‚ฌ์šฉ์ž ๊ณ„์ •์ƒ์„ฑ
    CREATE USER user02
    IDENTIFIED BY user02
    
    
    -- ๋กค ์ƒ์„ฑ
    
    --SYS
    CREATE ROLE clerk;
    
    
    -- ์‹œ์Šคํ…œ ๊ถŒํ•œ ๋ถ€์—ฌ
    
    --SYS
    GRANT CREATE SESSION, CREATE TABLE
    TO clerk;
    
    GRANT SELECT
    ON SCOTT.DEPT
    TO CLERK;
    
    
    -- ์‚ฌ์šฉ์ž์—๊ฒŒ clerk ๋กค ๋ถ€์—ฌ
    --SYS
    GRANT CLERK
    TO user03;

     

    ๋กค์€ ๋™์ ์œผ๋กœ ๊ถŒํ•œ์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋กค์— ์ƒˆ๋กœ์šด ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋ฉด ์ž๋™์œผ๋กœ ๊ทธ ๋กค์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž์—๊ฒŒ๋„ ์ƒˆ๋กœ์šด ๊ถŒํ•œ ๋ถ€์—ฌ๋œ๋‹ค. ํšŒ์ˆ˜๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค.

     

    -- ์ƒˆ ์ ‘์† ์ƒ์„ฑ ํ›„ sys๊ณ„์ •์—์„œ
    
    create user ๊ณ„์ • IDENTIFIED by ๋น„๋ฐ€๋ฒˆํ˜ธ;
    alter user ๊ณ„์ • account unlock;
    grant connect, resource to ๊ณ„์ •;
    
    -- ์š” ์„ธ๋ฌธ์žฅ์€ ๊ธฐ์–ตํ•ด๋‘๊ธฐ

     

     

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€