๐Ÿ‘€ VIEW

๐Ÿ’โ€โ™€๏ธ ๋ทฐ(VIEW)๋ž€,
SELECT ์ฟผ๋ฆฌ๋ฌธ์„ ์ €์žฅํ•œ ๊ฐ์ฒด๋กœ ์‹ค์งˆ์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์ง€์•Š์€ ๋…ผ๋ฆฌ์ ์ธ ํ…Œ์ด๋ธ”์ด๋‚˜ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

  • ๋ณต์žกํ•œ SELCET ๋ฌธ์„ ๋‹ค์‹œ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์Œ
  • ๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆจ๊ธธ ์ˆ˜ ์žˆ์Œ

๐Ÿ™‹โ€ ์ž ๊น ! ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ(Date Dictionary)๋„ ์•Œ์•„๋ด…์‹œ๋‹ค !
์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ์‹œ์Šคํ…œ ํ…Œ์ด๋ธ”

  • ์‚ฌ์šฉ์ž๊ฐ€ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ์‚ฌ์šฉ์ž๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„œ๋ฒ„์— ์˜ํ•ด ์ž๋™์œผ๋กœ ๊ฐฑ์‹ ๋˜๋Š” ํ…Œ์ด๋ธ”๋กœ ์‚ฌ์šฉ์ž๋Š” ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ ๋‚ด์šฉ์„ ์ง์ ‘ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์—†์Œ
  • ์›๋ณธ ํ…Œ์ด๋ธ”์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ์›๋ณธ ํ…Œ์ด๋ธ”์˜ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”(VIEW)
  • ๋ทฐ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ
    USER_VIEWS
SELECT
        UV.*
    FROM USER_VIEWS UV;

๐Ÿ‘‰ VIEW ์ƒ์„ฑ ๋ฐ ์‚ญ์ œ

[1] system๊ณ„์ •์—์„œ EMPLOYEE๊ณ„์ •์œผ๋กœ ๋ทฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌ

GRANT CREATE VIEW TO C##EMPLOYEE;

>>> ๋ฐ˜๋“œ์‹œ system๊ณ„์ •์œผ๋กœ ์‹คํ–‰
>>> 'Grant์„(๋ฅผ) ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.' ์ถœ๋ ฅ

[2] V_RESULT_EMP ๋ทฐ ์ƒ์„ฑ (์‚ฌ๋ฒˆ, ์ด๋ฆ„, ์ง๊ธ‰๋ช…, ๋ถ€์„œ๋ช…, ๊ทผ๋ฌด์ง€์—ญ์„ ์กฐํšŒํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ทฐ๋กœ ์ƒ์„ฑ)

CREATE [OR REPLACE] VIEW ๋ทฐ์ด๋ฆ„ AS ์„œ๋ธŒ์ฟผ๋ฆฌ;

CREATE OR REPLACE VIEW V_RESULT_EMP -- OR REPLACE : ์žˆ์œผ๋ฉด ์ด๊ฒƒ์œผ๋กœ ๋ฎ์–ด์“ฐ๊ธฐ
AS
SELECT
        EMP_ID
    ,   EMP_NAME
    ,   JOB_NAME
    ,   DEPT_TITLE
    ,   LOCAL_NAME
    FROM EMPLOYEE E
    LEFT JOIN JOB J ON (E.JOB_CODE = J.JOB_CODE)
    LEFT JOIN DEPARTMENT D ON (E.DEPT_CODE = D.DEPT_ID)
    LEFT JOIN LOCATION L ON (D.LOCATION_ID = L.LOCAL_CODE);

[3] ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

SELECT
        V.*
    FROM V_RESULT_EMP V
    WHERE V.EMP_ID = '205'; 

[4] ๋ทฐ์— ๋ณ„์นญ์„ ๋ถ€์—ฌํ•ด์„œ ์ƒ์„ฑ

CREATE OR REPLACE VIEW ๋ทฐ๋ช… (๋ทฐ์ปฌ๋Ÿผ๋ณ„์นญ) AS ์„œ๋ธŒ์ฟผ๋ฆฌ

CREATE OR REPLACE VIEW V_EMP
(
    ์‚ฌ๋ฒˆ -- EMP_ID์— ๋ณ„์นญ๋ถ€์—ฌ
,   ์ด๋ฆ„
,   ๋ถ€์„œ
)
AS
SELECT
        EMP_ID
    ,   EMP_NAME
    ,   DEPT_CODE
    FROM EMPLOYEE;
SELECT
        V.*
    FROM V_EMP V;

[5] ๋ทฐ ์‚ญ์ œ

DROP VIEW ๋ทฐ๋ช…

DROP VIEW V_EMP; 

[6] ๋ทฐ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ ์•ˆ์— ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ํฌํ•จ

>>> ์ด ๋•Œ๋Š” '๋ฐ˜๋“œ์‹œ ๋ณ„์นญ์„ ๋ถ€์—ฌํ•ด์„œ ์ƒ์„ฑ'ํ•ด์•ผํ•จ

CREATE OR REPLACE VIEW V_EMP_JOB
(
    ์‚ฌ๋ฒˆ -- ๋ทฐ์— ๋ณ„์นญ ๋ถ™์ด๊ธฐ
,   ์ด๋ฆ„
,   ์ง๊ธ‰
,   ์„ฑ๋ณ„
,   ๊ทผ๋ฌด๋…„์ˆ˜
)
AS
SELECT
        EMP_ID
    ,   EMP_NAME
    ,   JOB_NAME
    ,   DECODE(SUBSTR(EMP_NO, 8, 1), 1, '๋‚จ', '์—ฌ') 
    	>>> ํ•จ์ˆ˜์‹์€ ๋ณ„์นญ ํ•„์ˆ˜! 
    	>>> ์—†์œผ๋ฉด, '์ด ์‹์€ ์—ด์˜ ๋ณ„๋ช…๊ณผ ํ•จ๊ป˜ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค' ์˜ค๋ฅ˜
    ,   EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM HIRE_DATE)
    FROM EMPLOYEE
    JOIN JOB USING(JOB_CODE);
SELECT
        V.*
    FROM V_EMP_JOB V; -- ๋ณ„์นญ์ด ๋ถ€์—ฌ๋œ ๋ทฐ ํ™•์ธ ๊ฐ€๋Šฅ

๐Ÿ‘‰ ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ์˜ ์ •๋ณด ๋ณ€๊ฒฝ

[1] ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ์˜ EMP_NAME ๋ณ€๊ฒฝ

UPDATE
        EMPLOYEE
    SET EMP_NAME = 'ํ™๊ธธ๋™'
    WHERE EMP_ID = '200';

[2] ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ํ™•์ธ

SELECT
        E.*
    FROM EMPLOYEE E
    WHERE EMP_ID = '200'; -- EMP_NAME์ด ํ™๊ธธ๋™์œผ๋กœ ๋ณ€๊ฒฝ๋จ

[3] ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด VIEW์˜ ์ •๋ณด๋„ ๊ฐ™์ด ๋ณ€๊ฒฝ (VIEW๋Š” ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•ด์˜ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—)

SELECT
        V.*
    FROM V_RESULT_EMP V
    WHERE EMP_ID = '200'; -- ํ™๊ธธ๋™์œผ๋กœ ๋ณ€๊ฒฝ๋˜์–ด์žˆ์Œ

๐Ÿ‘‰ VIEW ํ…Œ์ด๋ธ”์—์„œ์˜ ์ •๋ณด ๋ณ€๊ฒฝ

[1] ๋ทฐ๋ฅผ ํ†ตํ•ด ๊ฐ’ ์‚ฝ์ž…

INSERT
    INTO V_JOB
(
    JOB_CODE, JOB_NAME
)
VALUES
(
    'J8', '์ธํ„ด'
);
SELECT
        V.*
    FROM V_JOB V; -- 'J8', '์ธํ„ด' ๊ฐ’ ์กฐํšŒ๊ฐ€๋Šฅ
SELECT
        J.*
    FROM JOB J; -- ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ๋„ 'J8', '์ธํ„ด' ๊ฐ’ ์กฐํšŒ๊ฐ€๋Šฅ

[2] ๋ทฐ๋ฅผ ํ†ตํ•ด JOB_NAME ์ˆ˜์ •

UPDATE
        V_JOB V
    SET JOB_NAME = '์•Œ๋ฐ”'
    WHERE JOB_CODE = 'J8';
SELECT
        V.*
    FROM V_JOB V; -- 'J8', '์•Œ๋ฐ”'๋กœ ๋ณ€๊ฒฝ๋œ ๊ฐ’ ์กฐํšŒ๊ฐ€๋Šฅ
SELECT
        J.*
    FROM JOB J; -- ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ๋„ 'J8', '์•Œ๋ฐ”'๋กœ ๋ณ€๊ฒฝ๋œ ๊ฐ’ ์กฐํšŒ๊ฐ€๋Šฅ

[3] ๋ทฐ๋ฅผ ํ†ตํ•ด J8 ์•Œ๋ฐ”ํ–‰ ์‚ญ์ œ

DELETE
    FROM V_JOB
    WHERE JOB_CODE = 'J8'; 
    -- ์‚ญ์ œํ•œ ํ›„ ์กฐํšŒ ์‹œ, ๋ทฐ์™€ ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋‘ ์‚ญ์ œ๋œ ๊ฒƒ ํ™•์ธ ๊ฐ€๋Šฅ

๐Ÿ‘‰ DML ๋ช…๋ น์–ด๋กœ VIEW ์กฐ์ž‘์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ

๋ทฐ๋ฅผ ํ†ตํ•ด ๋ฒ ์ด์Šคํ…Œ์ด๋ธ” ์‚ฝ์ž…/๋ณ€๊ฒฝ/์‚ญ์ œ ๊ฐ€๋Šฅํ•œ ๊ฒƒ ํ™•์ธํ–ˆ์œผ๋‚˜, ๋ฐ˜๋ฉด์— ์กฐ์ž‘์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ

๐Ÿ“ VIEW ์กฐ์ž‘์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ

[1] ๋ทฐ ์ •์˜์— ํฌํ•จ๋˜์ง€ ์•Š์€ ์ปฌ๋Ÿผ์„ ์กฐ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ
[2] ๋ทฐ์— ํฌํ•จ๋˜์ง€ ์•Š์€ ์ปฌ๋Ÿผ ์ค‘์—, ๋ฒ ์ด์Šค๊ฐ€ ๋˜๋Š” ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ์ด NOT NULL ์ œ์•ฝ์กฐ๊ฑด์ด ์ง€์ • ๋œ ๊ฒฝ์šฐ
[3] ์‚ฐ์ˆ  ํ‘œํ˜„์‹์œผ๋กœ ์ •์˜ ๋œ ๊ฒฝ์šฐ
[4] JOIN์„ ์ด์šฉํ•ด ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•œ ๊ฒฝ์šฐ
[5] DISTINCT๋ฅผ ํฌํ•จํ•œ ๊ฒฝ์šฐ
[6] ๊ทธ๋ฃนํ•จ์ˆ˜๋‚˜ GROUP BY์ ˆ์„ ํฌํ•จํ•œ ๊ฒฝ์šฐ

๐Ÿ‘‰ VIEW ์˜ต์…˜

[1] OR REPLACE

OR REPLACE : ๊ธฐ์กด์— ๋™์ผํ•œ ๋ทฐ ์ด๋ฆ„์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ๋ฎ์–ด์“ฐ๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๋Š” ์˜ต์…˜
FORCE(๊ฐ•์ œ ์‹คํ–‰) : ์„œ๋ธŒ ์ฟผ๋ฆฌ์— ์‚ฌ์šฉ ๋œ ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๋ทฐ ์ƒ์„ฑํ•˜๋Š” ์˜ต์…˜

CREATE OR REPLACE FORCE VIEW V_EMP
AS
SELECT TCODE
    ,  TNAME
    ,  TCONTENT
    FROM TT;   
    >>> FORCE๋กœ ์ธํ•ด ๊ฐ•์ œ๋กœ ๋ทฐ ์ƒ์„ฑ ๋ฐ '๊ฒฝ๊ณ : ์ปดํŒŒ์ผ ์˜ค๋ฅ˜์™€ ํ•จ๊ป˜ ๋ทฐ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.' ์ถœ๋ ฅ

[2] NOFORCE

NOFORCE : ์„œ๋ธŒ์ฟผ๋ฆฌ์— ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•ด์•ผ๋งŒ ๋ทฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์˜ต์…˜(๊ธฐ๋ณธ๊ฐ’)

CREATE OR REPLACE NOFORCE VIEW V_EMP >>> ๊ธฐ๋ณธ๊ฐ’์ด๋ฏ€๋กœ NOFORCE๋ฅผ ๋ช…์‹œํ•˜์ง€์•Š์•„๋„๋จ
AS
SELECT TCODE
    ,  TNAME
    ,  TCONTENT
    FROM TT;    
    >>> 'ํ…Œ์ด๋ธ” ๋˜๋Š” ๋ทฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค' ์˜ค๋ฅ˜

[3] WITH CHECK OPTION

WITH CHECK OPTION : ์กฐ๊ฑด์ ˆ์— ์‚ฌ์šฉ ๋œ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•˜์ง€ ๋ชป ํ•˜๊ฒŒ ํ•˜๋Š” ์˜ต์…˜

CREATE OR REPLACE VIEW V_EMP2
AS
SELECT
        E.*
    FROM EMPLOYEE E
    WHERE MANAGER_ID = '200'
    >>> ์ด ๋ถ€๋ถ„์€ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด๋ฏ€๋กœ ๋ทฐ๋ฅผ ํ†ตํ•ด ์ˆ˜์ •ํ•˜์ง€ ๋ชป ํ•˜๊ฒŒ ํ•จ
    WITH CHECK OPTION;
โœ… MANAGER_ID ์ˆ˜์ • ์‹œ๋„
UPDATE
        V_EMP2
    SET MANAGER_ID = '900'
    WHERE MANAGER_ID = '200'; -- '๋ทฐ์˜ WITH CHECK OPTION์˜ ์กฐ๊ฑด์— ์œ„๋ฐฐ ๋ฉ๋‹ˆ๋‹ค' ์˜ค๋ฅ˜

[4] WITH READ ONLY

WITH READ ONLY : DML ์ˆ˜ํ–‰์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์˜ต์…˜

CREATE OR REPLACE VIEW V_DEPT
AS
SELECT
        D.*
    FROM DEPARTMENT D
    WITH READ ONLY; 
    >>> ์ฝ๊ธฐ ์ „์šฉ์ด๋ฏ€๋กœ ์‚ฝ์ž…/์ˆ˜์ •/์‚ญ์ œ ๋ถˆ๊ฐ€๋Šฅ
โœ… V_DEPT ์‚ญ์ œ ์‹œ๋„
DELETE
    FROM V_DEPT; -- '์ฝ๊ธฐ ์ „์šฉ ๋ทฐ์—์„œ๋Š” DML ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.' ์˜ค๋ฅ˜

๐Ÿ‘€ SEQUENCE

๐Ÿ’โ€โ™€๏ธ ์‹œํ€€์Šค(SEQUENCE)๋ž€,
์ž๋™ ๋ฒˆํ˜ธ ๋ฐœ์ƒ๊ธฐ ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด

  • ์ˆœ์ฐจ์ ์œผ๋กœ ์ •์ˆ˜ ๊ฐ’์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

๐Ÿ‘‰ SEQUENCE ์ƒ์„ฑ, ์‹คํ–‰ ๋ฐ ์กฐํšŒ

CREATE SEQUENCE ์‹œํ€€์Šค๋ช…
[INCREMENT BY ์ˆซ์ž] -- ๋‹ค์Œ ๊ฐ’์— ๋Œ€ํ•œ ์ฆ๊ฐ€์น˜, ์ƒ๋žตํ•˜๋ฉด ์ž๋™ 1์ด ๊ธฐ๋ณธ
[START WITH ์ˆซ์ž] -- ์ฒ˜์Œ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฐ’ ์ง€์ •, ์ƒ๋žตํ•˜๋ฉด ์ž๋™ 1์ด ๊ธฐ๋ณธ
[MAXVALUE ์ˆซ์ž or NOMAXVALUE] -- ๋ฐœ์ƒ์‹œํ‚ฌ ์ตœ๋Œ€ ๊ฐ’ ์ง€์ •(10์˜ 27์Šน)
[MINVALUE ์ˆซ์ž or NOMINVALUE] -- ๋ฐœ์ƒ์‹œํ‚ฌ ์ตœ์†Œ ๊ฐ’ ์ง€์ •(-10์˜ 26์Šน)
[CYCLE or NOCYCLE] -- ๊ฐ’ ์ˆœํ™˜ ์—ฌ๋ถ€
[CACHE ๋ฐ”์ดํŠธํฌ๊ธฐ or NOCACHE] --์บ์‰ฌ ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ณธ ๊ฐ’์€ 20๋ฐ”์ดํŠธ, ์ตœ์†Œ 2๋ฐ”์ดํŠธ

[1] SEQ_EMPID ์‹œํ€€์Šค ์ƒ์„ฑ

CREATE SEQUENCE SEQ_EMPID
START WITH 300
INCREMENT BY 5
MAXVALUE 310
NOCYCLE
NOCACHE;

[2] SEQ_EMPID ์‹œํ€€์Šค ์‹คํ–‰

์‹œํ€€์Šค๋ช….CURRVAL : ํ•ด๋‹น ์‹œํ€€์Šค๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” CURRENT VALUE(ํ˜„์žฌ ๊ฐ’)
์‹œํ€€์Šค๋ช….NEXTVAL : ํ•ด๋‹น ์‹œํ€€์Šค๊ฐ€ ๊ฐ€์งˆ NEXT VALUE(๋‹ค์Œ ๊ฐ’) ๋ฆฌํ„ด. NEXTVAL์„ 1ํšŒ ์ˆ˜ํ–‰ํ•ด์•ผ CURRVAL๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Œ


SELECT SEQ_EMPID.CURRVAL FROM DUAL; 
>>> CURRVAL ๋ฐ”๋กœ ์‹คํ–‰ ์‹œ, 
>>> '์‹œํ€€์Šค SEQ_EMPID.CURRVAL์€ ์ด ์„ธ์…˜์—์„œ๋Š” ์ •์˜ ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค' ์˜ค๋ฅ˜

SELECT SEQ_EMPID.NEXTVAL FROM DUAL; -- 300
SELECT SEQ_EMPID.CURRVAL FROM DUAL; -- 300
SELECT SEQ_EMPID.NEXTVAL FROM DUAL; -- 305
SELECT SEQ_EMPID.CURRVAL FROM DUAL; -- 305
SELECT SEQ_EMPID.NEXTVAL FROM DUAL; -- 310
SELECT SEQ_EMPID.CURRVAL FROM DUAL; -- 310

SELECT SEQ_EMPID.NEXTVAL FROM DUAL;
>>> MAXVALUE๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ
>>> '์‹œํ€€์Šค SEQ_EMPID.NEXTVAL exceeds MAXVALUE์€ ์‚ฌ๋ก€๋กœ ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค'

[3] ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ†ตํ•ด SEQ_EMPID ์‹œํ€€์Šค ์กฐํšŒ

USER_SEQUENCES

SELECT * FROM USER_SEQUENCES;

๐Ÿ‘‰ SEQUENCE ๋ณ€๊ฒฝ ๋ฐ ์‚ญ์ œ

[1] SEQ_EMPID ์‹œํ€€์Šค ๋ณ€๊ฒฝ

ALTER SEQUENCE ์‹œํ€€์Šค๋ช… ...

ALTER SEQUENCE SEQ_EMPID
INCREMENT BY 10
MAXVALUE 400
NOCYCLE
NOCACHE;
>>> START WITH ๊ฐ’์€ '๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ'์ด๋ฏ€๋กœ, 
>>> ํ•ด๋‹น ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด DROP์œผ๋กœ ์‚ญ์ œ ํ›„ ๋‹ค์‹œ ์ƒ์„ฑ

SELECT SEQ_EMPID.NEXTVAL FROM DUAL; -- 320
SELECT SEQ_EMPID.CURRVAL FROM DUAL; -- 320
SELECT SEQ_EMPID.NEXTVAL FROM DUAL; -- 330
SELECT SEQ_EMPID.CURRVAL FROM DUAL; -- 330
>>> ์‹œํ€€์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด 10์”ฉ ์ฆ๊ฐ€ํ•˜๊ณ  ์ตœ๋Œ€๊ฐ’์ด 400๊นŒ์ง€ ๋Š˜์–ด๋‚˜ ์ถ”๊ฐ€๊ฐ€ ์ง€์†

[2] SEQ_EMPID ์‹œํ€€์Šค ์‚ญ์ œ

DROP SEQUENCE ์‹œํ€€์Šค๋ช…;

DROP SEQUENCE SEQ_EMPID;

๐Ÿ‘‰ SEQUENCE์˜ ์‚ฌ์šฉ

SEQUENCE๋Š” 'ํ…Œ์ด๋ธ”์˜ ์‹๋ณ„์ž(PK)' ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ (๋„˜๋ฒ„๋ง)

[1] SEQ_EID ์‹œํ€€์Šค ์ƒ์„ฑ

CREATE SEQUENCE SEQ_EID
START WITH 300
INCREMENT BY 1
MAXVALUE 10000
NOCYCLE
NOCACHE;

[2] EMPLOYEE ํ…Œ์ด๋ธ”์˜ EMP_ID ์ปฌ๋Ÿผ์— ์‹œํ€€์Šค ์‚ฝ์ž…

INSERT
    INTO EMPLOYEE

-- (๋ชจ๋“  ์ปฌ๋Ÿผ๋ช…) 
-- => ์•„๋ž˜ VALUES์—์„œ ๋ชจ๋“  ์ปฌ๋Ÿผ์— ๋Œ€ํ•œ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅํ•˜์˜€์œผ๋ฏ€๋กœ ๋ช…์‹œ ๋ถˆํ•„์š”

VALUES
(
    SEQ_EID.NEXTVAL, 'ํ™๊ธธ๋™', '660101-1111111', 'hong_gd@greedy.com', '01012345678',
    'D2', 'J7', 'S1', 5000000, 0.1, 200, SYSDATE, NULL, DEFAULT
);
>>> 300๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ  1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” SEQ_EID.NEXTVAL๋ฅผ EMP_ID ์ปฌ๋Ÿผ์— ๊ฐ’์œผ๋กœ ๋„ฃ์–ด 
>>> ์ถ”๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค 300, 301, 302,...๋กœ ์ถ”๊ฐ€๋˜๊ฒŒ๋” ์‹œํ€€์Šค ์‚ฝ์ž…

[3] EMPLOYEE ํ…Œ์ด๋ธ” ์กฐํšŒํ•˜์—ฌ ์‹œํ€€์Šค ์‚ฝ์ž…๋œ ๊ฒƒ ํ™•์ธ

SELECT
        E.*
    FROM EMPLOYEE E;

๐Ÿ‘€ INDEX

๐Ÿ’โ€โ™€๏ธ ์ธ๋ฑ์Šค(INDEX)๋ž€,
SQL ๋ช…๋ น๋ฌธ์˜ ๊ฒ€์ƒ‰์ฒ˜๋ฆฌ ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ปฌ๋Ÿฌ์— ๋Œ€ํ•ด ์ƒ์„ฑํ•˜๋Š” ๊ฐ์ฒด

  • ํ•˜๋“œ ๋””์Šคํฌ์˜ ์–ด๋–ค ์œ„์น˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ์ฃผ์†Œ๋ก์œผ๋กœ ์ธ๋ฑ์Šค๋Š” DATA์™€ ROWID๋กœ ๊ตฌ์„ฑ
  • ์ธ๋ฑ์Šค์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋Š” ์ด์ง„ ํŠธ๋ฆฌ ํ˜•์‹์œผ๋กœ ๊ตฌ์„ฑ (๋…ธ๋“œ๋“ค์ด ์ •๋ ฌ๋œ ํ˜•ํƒœ)
  • ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‹œ๊ฐ„์ด ํ•„์š”ํ•˜๋ฉฐ ์ธ๋ฑ์Šค๋ฅผ ์œ„ํ•œ ์ถ”๊ฐ€ ์ €์žฅ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ ์žฅ์ ๋งŒ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹˜
  • ์ธ๋ฑ์Šค๊ฐ€ ์ƒ์„ฑ ๋œ ์ปฌ๋Ÿผ์—์„œ DML ์ž‘์—…์ด ๋นˆ๋ฒˆํ•œ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋Š๋ ค์ง

๐Ÿ’โ€ ์ธ๋ฑ์Šค์˜ ์žฅ์  & ๋‹จ์ 

  • ์žฅ์ 
    • ๊ฒ€์ƒ‰ ์†๋„ ํ–ฅ์ƒ
    • ์‹œ์Šคํ…œ ๋ถ€ํ•˜๋ฅผ ์ค„์—ฌ(FULL SCAN์„ ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ) ์‹œ์Šคํ…œ ์„ฑ๋Šฅ ํ–ฅ์ƒ
  • ๋‹จ์ 
    • ์ถ”๊ฐ€ ์ €์žฅ ๊ณต๊ฐ„, ์ƒ์„ฑ ์‹œ๊ฐ„ ํ•„์š”
    • DML์ด ๋นˆ๋ฒˆํ•˜๋ฉด REBUILD(ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ฌ) ์ž‘์—…์„ ์ฃผ๊ธฐ์ ์œผ๋กœ ํ•ด์•ผํ•˜๋ฉฐ REBUILD ํ•˜์ง€ ์•Š์œผ๋ฉด ์˜คํžˆ๋ ค ์„ฑ๋Šฅ ์ €ํ•˜

๐Ÿ‘‰ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์ฒด ๋กœ์šฐ์˜ 15% ์ดํ•˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉ (์ ์€ ๋ฐ์ดํ„ฐ๋ฅผ FULL SCANํ•˜๋ฉด ๋น„ํšจ์œจ์ ์ด๋ฏ€๋กœ)


๐Ÿ™‹โ€ ์ž ๊น ! ROWID์˜ ๊ตฌ์กฐ๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?
์˜ค๋ธŒ์ ํŠธ ๋ฒˆํ˜ธ, ์ƒ๋Œ€ํŒŒ์ผ ๋ฒˆํ˜ธ, ๋ธ”๋ก ๋ฒˆํ˜ธ, ๋ฐ์ดํ„ฐ ๋ฒˆํ˜ธ (๋ฌผ๋ฆฌ์ ์ธ ID๊ฐ’)

SELECT
        ROWID
    ,   EMP_ID
    ,   EMP_NAME
    FROM EMPLOYEE;


๐Ÿ‘‰ INDEX์˜ ์ข…๋ฅ˜

๐Ÿ“ INDEX์˜ ์ข…๋ฅ˜

[1] ๊ณ ์œ  ์ธ๋ฑ์Šค(UNIQUE INDEX)
[2] ๋น„๊ณ ์œ  ์ธ๋ฑ์Šค(NONUNIQUE INDEX)
[3] ๋‹จ์ผ ์ธ๋ฑ์Šค(SINGLE INDEX)
[4] ๊ฒฐํ•ฉ ์ธ๋ฑ์Šค(COMPOSITE INDEX)
[5] ํ•จ์ˆ˜ ๊ธฐ๋ฐ˜ ์ธ๋ฑ์Šค(FUNTION BASED INDEX)

๐Ÿ™‹โ€ ์ž ๊น ! ์ธ๋ฑ์Šค ํžŒํŠธ ๋จผ์ € ์•Œ๊ณ ๊ฐ‘์‹œ๋‹ค !
์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์ ์ ˆํ•œ ์ธ๋ฑ์Šค๋ฅผ ํƒ€๊ฑฐ๋‚˜ ํ’€ ์Šค์บ๋‹์„ ํ•ด์„œ ๋น„์šฉ์ด ์ ๊ฒŒ ๋“œ๋Š” ํšจ์œจ์ ์ธ ๋ฐฉ์‹์„ ์„ ํƒ. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ตฌ๋ฌธ(ํžŒํŠธ)๋ฅผ ํ†ตํ•ด์„œ ์„ ํƒํ•  ์ˆ˜๋„ ์žˆ์Œ

  • SELECT์ ˆ ์ฒซ ์ค„์— ํžŒํŠธ ์ฃผ์„ /* + ๋‚ด์šฉ */์„ ์ž‘์„ฑํ•˜์—ฌ ์ ์ ˆํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Œ

[1] UNIQUE INDEX (๊ณ ์œ  ์ธ๋ฑ์Šค)

  • ๊ณ ์œ  ์ธ๋ฑ์Šค๋กœ ์ƒ์„ฑ ๋œ ์ปฌ๋Ÿผ์—๋Š” ์ค‘๋ณต ๊ฐ’์ด ํฌํ•จ๋  ์ˆ˜ ์—†์œผ๋ฉฐ PK, UNIQUE ์ œ์•ฝ ์กฐ๊ฑด์„ ์ƒ์„ฑํ•˜๋ฉด ์ž๋™์œผ๋กœ ํ•ด๋‹น ์ปฌ๋Ÿผ์— UNIQUE INDEX๊ฐ€ ์ƒ์„ฑ.
  • ํ•ด๋‹น ์ปฌ๋Ÿผ์œผ๋กœ ACCESS ํ•˜๋Š” ๊ฒฝ์šฐ ์„ฑ๋Šฅ ํ–ฅ์ƒ ํšจ๊ณผ
  • ๋‹จ์ผ ์ธ๋ฑ์Šค๋ผ๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ์Œ
โœ… ์ธ๋ฑ์Šค ์˜์—ญ์—์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์Šค์บ” (INDEX)
SELECT /*+ INDEX(E ์—”ํ„ฐํ‹ฐ1_PK)*/ >>> ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์กฐํšŒํ•ด๋‹ฌ๋ผ๋Š” ํžŒํŠธ
        E.*
    FROM EMPLOYEE E;
    >>> ์กฐํšŒํ–ˆ์„ ๋•Œ, ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ€์žฅ ๋น ๋ฅธ ์กฐํšŒ๋ฒ•์„ ์ฐพ์Œ 
    >>> (์ธ๋ฑ์Šค๋กœ ์ฐพ๊ฑฐ๋‚˜ ํ’€ ์Šค์บ”์œผ๋กœ ์ฐพ๊ฑฐ๋‚˜)
โœ… ์ธ๋ฑ์Šค ์˜์—ญ์—์„œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์Šค์บ” (INDEX_DESC)
SELECT /*+ INDEX_DESC(E ์—”ํ„ฐํ‹ฐ1_PK)*/ >>> ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ์กฐํšŒํ•ด๋‹ฌ๋ผ๋Š” ํžŒํŠธ 
        E.*
    FROM EMPLOYEE E;
โœ… ์ธ๋ฑ์Šค ์ƒ์„ฑ (์ค‘๋ณต ๊ฐ’์ด ์žˆ๋Š” ์ปฌ๋Ÿผ์€ UNIQUE ์ธ๋ฑ์Šค ์ƒ์„ฑ ๊ฐ€๋Šฅ)

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

CREATE UNIQUE INDEX IDX_EMP_NO
ON EMPLOYEE(EMP_NO);
โœ… ์ค‘๋ณต ๊ฐ’์ด ์žˆ๋Š” ์ปฌ๋Ÿผ์€ UNIQUE ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑ X
CREATE UNIQUE INDEX IDX_DEPT_CODE
ON EMPLOYEE(DEPT_CODE);

>>> DEPT_CODE์˜ ๊ฐ ์ปฌ๋Ÿผ์— ๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์Œ. 
>>> '์ค‘๋ณต ํ‚ค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' ์˜ค๋ฅ˜

[2] NONUNIQUE INDEX (๋น„๊ณ ์œ  ์ธ๋ฑ์Šค)

  • WHERE์ ˆ์—์„œ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ์ผ๋ฐ˜ ์ปฌ๋Ÿผ์„ ๋Œ€์ƒ์œผ๋กœ ์ƒ์„ฑ
  • ๋‹จ์ผ ์ธ๋ฑ์Šค๋ผ๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ์Œ
CREATE INDEX IDX_DEPT_CODE	>>> UNIQUE ํ‚ค์›Œ๋“œ๋งŒ ๋นผ๋ฉด NONUNIQUE INDEX ์ƒ์„ฑ ๊ฐ€๋Šฅ
ON EMPLOYEE(DEPT_CODE);

>>> ์ค‘๋ณต๊ฐ’์ด ์žˆ๋Š” DEPT_CODE ์ปฌ๋Ÿผ์€ NONUNIQUE ์ธ๋ฑ์Šค ์ƒ์„ฑ

[3] COMPOSIRE INDEX (๊ฒฐํ•ฉ ์ธ๋ฑ์Šค)

๊ฒฐํ•ฉ ์ธ๋ฑ์Šค๋Š” ์ค‘๋ณต ๊ฐ’์ด ๋‚ฎ์€ ๊ฐ’์ด ๋จผ์ € ์˜ค๋Š” ๊ฒƒ์ด ๊ฒ€์ƒ‰ ์†๋„๋ฅผ ํ–ฅ์ƒ ์‹œํ‚ด

โœ… IDX_DEPT ์ธ๋ฑ์Šค ์ƒ์„ฑ
CREATE INDEX IDX_DEPT
ON DEPARTMENT(DEPT_ID, DEPT_TITLE); >>> ์ค‘๋ณต ๊ฐ’์ด ๋” ๋‚ฎ์€ DEPT_ID ์ปฌ๋Ÿผ ๋จผ์ € ๋ช…์‹œ
โœ… IDX_DEPT ์ธ๋ฑ์Šค ์‚ฌ์šฉ
SELECT /*+ INDEX_DESC(D IDX_DEPT)*/
        D.DEPT_ID
    FROM DEPARTMENT D
    WHERE D.DEPT_TITLE > '0'	>>> ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’๋“ค์ด ์ •๋ ฌ๋˜์—ˆ์œผ๋ฉด ํ–ˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์กฐ๊ฑด 
    							>>> (๋ˆ„๊ตฐ๊ฐ€๋ฅผ ๊ฑฐ๋ฅผ ๋•Œ ์“ฐ๋Š” ์กฐ๊ฑด X)
    AND D.DEPT_ID > '0';

[4] FUNTION BASED INDEX (ํ•จ์ˆ˜ ๊ธฐ๋ฐ˜ ์ธ๋ฑ์Šค)

  • SELECT์ ˆ์ด๋‚˜ WHERE์ ˆ์—์„œ ์‚ฐ์ˆ  ๊ณ„์‚ฐ์‹์ด๋‚˜ ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒฝ์šฐ, ๊ณ„์‚ฐ์— ํฌํ•จ๋œ ์ปฌ๋Ÿผ์€ ์ธ๋ฑ์Šค์˜ ์ ์šฉ์„ ๋ฐ›์ง€ ์•Š์Œ
  • ๊ณ„์‚ฐ์‹์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค๋ฉด, ์ˆ˜์‹์ด๋‚˜ ํ•จ์ˆ˜์‹์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ปฌ๋Ÿผ์„ ์ธ๋ฑ์Šค๋กœ ๋งŒ๋“ค์ˆ˜๋„ ์žˆ๋‹ค.
โœ… IDX_EMP_SALCALC ์ธ๋ฑ์Šค ์ƒ์„ฑ
CREATE INDEX IDX_EMP_SALCALC
ON EMPLOYEE((SALARY + (SALARY * NVL(BONUS,0))) * 12);
โœ… IDX_EMP_SALCALC ์ธ๋ฑ์Šค ์‚ฌ์šฉ
SELECT /*+ INDEX_DESC(E IDX_EMP_SALCALC)*/
        E.EMP_ID
    ,   E.EMP_NAME
    ,   (E.SALARY + (E.SALARY * NVL(E.BONUS,0))) * 12 ์—ฐ๋ด‰
    FROM EMPLOYEE E
    WHERE (E.SALARY + (E.SALARY * NVL(E.BONUS,0))) * 12 > 1000000;

profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€