[Day10] Oracle 10 [10/11]

--1. ์˜ค๋ผํด ๊ฐ DataType ์— ๋Œ€ํ•ด ์ƒ์„ธํžˆ ์„ค๋ช…ํ•˜์„ธ์š”

    ใ„ฑ. ๋ฌธ์ž,๋ฌธ์ž์—ด          ''
        ใ„ด ๊ณ ์ •๊ธธ์ด  char,[n]char  2000 byte
        ใ„ด ๊ฐ€๋ณ€๊ธธ์ด  varchar2(== varchar ์‹œ๋…ธ๋‹˜), nvarchar2     4000byte     
                    Long 2GB

    ใ„ด. LOB
        ใ„ด CLOB
        ใ„ด BLOB
        ใ„ด NCLOB
        
    ใ„ท. ์ˆซ์ž
        ใ„ด ์ •์ˆ˜   NUMBER( p, 0) == NUMBER( p )
        ใ„ด ์‹ค์ˆ˜   NUMBER( p, s )
        
                    p 38,    s 127
                 NUMBER   == NUMBER( 38, 127 )
                 FLOAT = ์‹ค์ˆ˜ NUMBER
    ใ„น.  ๋‚ ์งœ
       ใ„ด Date ๋‚ ์งœ + ์‹œ/๋ถ„/์ดˆ
       ใ„ด TIMESTAMP(n=6)  ms, ns
       
       ๋“ฑ๋“ฑ

--2.  emp ํ…Œ์ด๋ธ”์—์„œ [๋…„๋„๋ณ„] [์›”๋ณ„] ์ž…์‚ฌ์‚ฌ์›์ˆ˜ ์ถœ๋ ฅ.( PIVOT() ํ•จ์ˆ˜ ์‚ฌ์šฉ )
--
--    [์‹คํ–‰๊ฒฐ๊ณผ]
--    1982   1   0   0   0   0   0   0   0   0   0   0   0
--    1980   0   0   0   0   0   0   0   0   0   0   0   1
--    1981   0   2   0   1   1   1   0   0   2   0   1   2

ใ„ฑ. ํ”ผ๋ด‡๋Œ€์ƒ

SELECT EXTRACT( YEAR FROM hiredate) hiredate_year
, EXTRACT( MONTH FROM hiredate) hiredate_month
FROM emp;


ใ„ด. 

SELECT *
FROM ( 
        SELECT EXTRACT( YEAR FROM hiredate) hiredate_year
        , EXTRACT( MONTH FROM hiredate) hiredate_month
        FROM emp
)
PIVOT( COUNT(*) FOR hiredate_month IN ( 1,2,3,4,5,6,7,8,9,10,11,12 ) );



--2-2.   emp ํ…Œ์ด๋ธ”์—์„œ ๊ฐ JOB๋ณ„ ์ž…์‚ฌ๋…„๋„๋ณ„ 1์›”~ 12์›” ์ž…์‚ฌ์ธ์›์ˆ˜ ์ถœ๋ ฅ.  ( PIVOT() ํ•จ์ˆ˜ ์‚ฌ์šฉ ) 
--    [์‹คํ–‰๊ฒฐ๊ณผ]
--    ANALYST      1981   0   0   0   0   0   0   0   0   0   0   0   1
--    CLERK      1980   0   0   0   0   0   0   0   0   0   0   0   1
--    CLERK      1981   0   0   0   0   0   0   0   0   0   0   0   1
--    CLERK      1982   1   0   0   0   0   0   0   0   0   0   0   0
--    MANAGER      1981   0   0   0   1   1   1   0   0   0   0   0   0
--    PRESIDENT   1981   0   0   0   0   0   0   0   0   0   0   1   0
--    SALESMAN   1981   0   2   0   0   0   0   0   0       
--    

SELECT *
FROM ( 
        SELECT job
        , EXTRACT( YEAR FROM hiredate) hiredate_year
        , EXTRACT( MONTH FROM hiredate) hiredate_month
        FROM emp
)
PIVOT( COUNT(*) FOR hiredate_month IN ( 1,2,3,4,5,6,7,8,9,10,11,12 ) )
ORDER BY job ASC;



--3. empํ…Œ์ด๋ธ”์—์„œ ์ž…์‚ฌ์ผ์ž๊ฐ€ ์˜ค๋ž˜๋œ ์ˆœ์œผ๋กœ 3๋ช… ์ถœ๋ ฅ ( TOP 3 )
--    [์‹คํ–‰๊ฒฐ๊ณผ]
--    1   7369   SMITH   CLERK       7902   80/12/17   800          20
--    2   7499   ALLEN   SALESMAN   7698   81/02/20   1600   300   30
--    3   7521   WARD   SALESMAN   7698   81/02/22   1250   500   30   

ใ„ฑ. TOP-N ๋ฐฉ์‹
    1) ์ž…์‚ฌ์ผ์ž ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ
    2) ROWNUM ์˜์‚ฌ์ปฌ๋Ÿผ

SELECT ROWNUM  seq, t.*   
FROM (    
    SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno
    FROM emp
    ORDER BY hiredate ASC
) t
WHERE ROWNUM <=3;


ใ„ด. RANK ํ•จ์ˆ˜

WITH 
temp AS (
SELECT emp.*
       , RANK() OVER( ORDER BY hiredate ASC ) seq
FROM emp
)
SELECT *
FROM temp
WHERE seq <= 3;


--4. SMS ์ธ์ฆ๋ฒˆํ˜ธ  ์ž„์˜์˜  6์ž๋ฆฌ ์ˆซ์ž ์ถœ๋ ฅ ( dbms_random  ํŒจํ‚ค์ง€ ์‚ฌ์šฉ )

SELECT dbms_random.value
     , dbms_random.value(100000, 1000000)
FROM dual;



--4-2. ์ž„์˜์˜ ๋Œ€์†Œ๋ฌธ์ž 5๊ธ€์ž ์ถœ๋ ฅ( dbms_random  ํŒจํ‚ค์ง€ ์‚ฌ์šฉ )

SELECT dbms_random.string('A',5)
FROM dual;

 

--5. ๊ฒŒ์‹œ๊ธ€์„ ์ €์žฅํ•˜๋Š” ํ…Œ์ด๋ธ” ์ƒ์„ฑ
--   ใ„ฑ.   ํ…Œ์ด๋ธ”๋ช… : tbl_test
--   ใ„ด.   ์ปฌ๋Ÿผ
--         ๊ธ€๋ฒˆํ˜ธ    seq            ์ž๋ฃŒํ˜•  ํฌ๊ธฐ    ๋„ํ—ˆ์šฉ์—ฌ๋ถ€    ๊ณ ์œ ํ‚ค
--         ์ž‘์„ฑ์ž    writer     
--         ๋น„๋ฐ€๋ฒˆํ˜ธ passwd      
--         ๊ธ€์ œ๋ชฉ    title       
--         ๊ธ€๋‚ด์šฉ    content
--         ์ž‘์„ฑ์ผ    regdate   
--    ใ„ท.  ๊ธ€๋ฒˆํ˜ธ, ์ž‘์„ฑ์ž, ๋น„๋ฐ€๋ฒˆํ˜ธ, ๊ธ€ ์ œ๋ชฉ์€ ํ•„์ˆ˜ ์ž…๋ ฅ ์‚ฌํ•ญ์œผ๋กœ ์ง€์ •
--    ใ„น.  ๊ธ€๋ฒˆํ˜ธ๊ฐ€  ๊ธฐ๋ณธํ‚ค( PK )๋กœ ์ง€์ • : PK_TABLE_COLUMN
--    ใ….  ์ž‘์„ฑ์ผ์€ ํ˜„์žฌ ์‹œ์Šคํ…œ์˜ ๋‚ ์งœ๋กœ ์ž๋™ ์„ค์ •

CREATE TABLE tbl_test(
   seq        NUMBER(38)    NOT NULL CONSTRAINT PK_TBLTEST_SEQ PRIMARY KEY  -- SYS_????
   , writer   NVARCHAR2(10) NOT NULL
   , passwd   VARCHAR2(15)  NOT NULL
   , title    NVARCHAR2(30)   NOT NULL
   , content  NCLOB
   , regdate  DATE       DEFAULT SYSDATE
);

-- Table TBL_TEST์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


--5-2. ์กฐํšŒ์ˆ˜    read   ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ ( ๊ธฐ๋ณธ๊ฐ’ 0 ์œผ๋กœ  ์„ค์ • ) 

ALTER TABLE TBL_TEST
ADD read NUMBER DEFAULT 0;  -- ํ•œ๊ฐœ์˜ ์ปฌ๋Ÿผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” () ๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅ
ADD ();
-- Table TBL_TEST์ด(๊ฐ€) ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


--5-3. ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ํ™•์ธ 

DESC  TBL_TEST;


--5-4. CRUD  ( insert, select, update, delete ) 

--   ใ„ฑ. ์ž„์˜์˜ ๊ฒŒ์‹œ๊ธ€ 5๊ฐœ๋ฅผ ์ถ”๊ฐ€ insert

INSERT INTO TBL_TEST (SEQ, WRITER,PASSWD,TITLE, CONTENT ) VALUES (1, '๊ด€๋ฆฌ์ž', '1234$', 'test-1', 'test-1');
-- 1 ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
-- 00947. 00000 -  "not enough values"
INSERT INTO TBL_TEST  VALUES (2, 'ํ™๊ธธ๋™', '1234$', 'test-2', 'test-2', SYSDATE, 0);
--1 ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
-- SQL ์˜ค๋ฅ˜: ORA-00913: too many values
INSERT INTO TBL_TEST (SEQ, WRITER,PASSWD,TITLE ) VALUES (3, '๊น€๊ธฐ์ˆ˜', '1234$', 'test-3' );

COMMIT;
-- ์ปค๋ฐ‹ ์™„๋ฃŒ.


--   ใ„ด. ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ select

SELECT *
FROM tbl_test;
--   ใ„ท. 3๋ฒˆ ๊ฒŒ์‹œ๊ธ€์˜ ๊ธ€ ์ œ๋ชฉ, ๋‚ด์šฉ ์ˆ˜์ • update
-- ORA-01722: invalid number
UPDATE tbl_test
SET title = '[e]' || title , content = 'edit content'
wHERE seq = 3;


--   ใ„น. 4๋ฒˆ ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ delete

DELETE FROM tbl_Test
WHERE seq = 3;

COMMIT;


--5-5. tbl_board ํ…Œ์ด๋ธ” ์‚ญ์ œ  

DROP TABLE tbl_test PURGE;

--Table TBL_TEST์ด(๊ฐ€) ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


--6-1. ์˜ค๋Š˜์˜ ๋‚ ์งœ์™€ ์š”์ผ ์ถœ๋ ฅ 
-- [์‹คํ–‰๊ฒฐ๊ณผ]
--์˜ค๋Š˜๋‚ ์งœ     ์ˆซ์ž์š”์ผ  ํ•œ์ž๋ฆฌ์š”์ผ       ์š”์ผ
----------      ---          ------     ------------
--22/04/15      6             ๊ธˆ                ๊ธˆ์š”์ผ      

SELECT SYSDATE
     , TO_CHAR( SYSDATE, 'D' )
     , TO_CHAR( SYSDATE, 'DY' )
     , TO_CHAR( SYSDATE, 'DAY' ) 
     -- ์ผ DD
FROM dual;


--6-2. ์ด๋ฒˆ ๋‹ฌ์˜ ๋งˆ์ง€๋ง‰ ๋‚ ๊ณผ ๋‚ ์งœ๋งŒ ์ถœ๋ ฅ 
-- [์‹คํ–‰๊ฒฐ๊ณผ]
--์˜ค๋Š˜๋‚ ์งœ         ์ด๋ฒˆ๋‹ฌ๋งˆ์ง€๋ง‰๋‚ ์งœ                  ๋งˆ์ง€๋ง‰๋‚ ์งœ(์ผ)
----------         --------                      --------------------
--22/04/15        22/04/30 30                             30

SELECT SYSDATE
      , LAST_DAY( SYSDATE )
      , TO_CHAR( LAST_DAY( SYSDATE )  , 'DD' )
FROM dual;


--6-3.
-- [์‹คํ–‰๊ฒฐ๊ณผ]
--์˜ค๋Š˜๋‚ ์งœ    ์›”์˜์ฃผ์ฐจ    ๋…„์˜์ฃผ์ฐจ    ๋…„์˜ ์ฃผ์ฐจ
----------     ----           ----           ----
--22/04/15     3               15              15

 -- IW, WW ์ฐจ์ด์   ~ ( ๊ธฐ์–ต )

SELECT SYSDATE
    , TO_CHAR( SYSDATE, 'W') 
    , TO_CHAR( SYSDATE, 'IW') 
    , TO_CHAR( SYSDATE, 'WW') 
FROM dual;



DESC tbl_member;

์ด๋ฆ„                   ๋„?                          ์œ ํ˜•             
-----            --------                -------------- 
ID             NOT NULL                NVARCHAR2(10)  
NAME      NOT NULL               NVARCHAR2(10)  
AGE                                            NUMBER(3)      
BIRTH                                         DATE           
TEL          NOT NULL                CHAR(13)       
ETC                                            NVARCHAR2(100) 

1)  ์ปฌ๋Ÿผ ์ถ”๊ฐ€ : tel, etc
2)   etc ์ปฌ๋Ÿผ์˜ ์ž๋ฃŒํ˜•์˜ ํฌ๊ธฐ  NVARCHAR2(100) -> 200 ์ˆ˜์ •

ALTER TABLE MODIFY

ใ€ํ˜•์‹ใ€‘
        ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
        MODIFY (์ปฌ๋Ÿผ๋ช… datatype [DEFAULT ๊ฐ’]
               [,์ปฌ๋Ÿผ๋ช… datatype]...);

• ๋ฐ์ดํ„ฐ์˜ type, ***[size]***, default ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
• ๋ณ€๊ฒฝ ๋Œ€์ƒ ์ปฌ๋Ÿผ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๊ฑฐ๋‚˜ null ๊ฐ’๋งŒ ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋Š” [ size๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.]
• ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๋ณ€๊ฒฝ์€ CHAR์™€ VARCHAR2 ์ƒํ˜ธ๊ฐ„์˜ ๋ณ€๊ฒฝ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
• *** ์ปฌ๋Ÿผ ํฌ๊ธฐ์˜ ๋ณ€๊ฒฝ์€ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํด ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.  **
• NOT NULL ์ปฌ๋Ÿผ์ธ ๊ฒฝ์šฐ์—๋Š” size์˜ ํ™•๋Œ€๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
• ์ปฌ๋Ÿผ์˜ ๊ธฐ๋ณธ๊ฐ’ ๋ณ€๊ฒฝ์€ ๊ทธ ์ดํ›„์— ์‚ฝ์ž…(INSERT)๋˜๋Š” ํ–‰๋ถ€ํ„ฐ ์˜ํ–ฅ์„ ์ค€๋‹ค.
• ์ปฌ๋Ÿผ์ด๋ฆ„์˜ [์ง์ ‘์ ์ธ ๋ณ€๊ฒฝ]์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
• ์ปฌ๋Ÿผ์ด๋ฆ„์˜ ๋ณ€๊ฒฝ์€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ์‹œ alias๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
• alter table ... modify๋ฅผ ์ด์šฉํ•˜์—ฌ ์ œ์•ฝ์กฐ๊ฑด(constraint)๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค

ALTER TABLE tbl_member
MODIFY (  etc NVARCHAR2(200)   );  -- ์ž๋ฃŒํ˜•, ํฌ๊ธฐ ํ™•๋Œ€, ์ถ•์†Œ(์ œ์•ฝ)
-- Table TBL_MEMBER์ด(๊ฐ€) ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


3) etc ์ปฌ๋Ÿผ๋ช…์„ bigo ์ปฌ๋Ÿผ๋ช…์œผ๋กœ ์ˆ˜์ •.
  ใ„ฑ. ๋ณ„์นญ( alias ) ์‚ฌ์šฉ

  SELECT etc AS "bigo"
  FROM tbl_member;


   ใ„ด RENAME ์ปฌ๋Ÿผ๋ช…์„ ์ˆ˜์ •. 

   ALTER TABLE tbl_member
   RENAME COLUMN etc TO bigo;
-- Table TBL_MEMBER์ด(๊ฐ€) ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


4) bigo ์ปฌ๋Ÿผ์„ ์‚ญ์ œ.

ALTER TABLE DROP COLUMN

ใ€ํ˜•์‹ใ€‘
        ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
        DROP COLUMN ์ปฌ๋Ÿผ๋ช…; 

• ์ปฌ๋Ÿผ์„ ์‚ญ์ œํ•˜๋ฉด ํ•ด๋‹น ์ปฌ๋Ÿผ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋œ๋‹ค.
• ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ ์ปฌ๋Ÿผ๋งŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
• ์‚ญ์ œ ํ›„ ํ…Œ์ด๋ธ”์—๋Š” ์ ์–ด๋„ ํ•˜๋‚˜์˜ ์ปฌ๋Ÿผ์€ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.
• DDL๋ฌธ์œผ๋กœ ์‚ญ์ œ๋œ ์ปฌ๋Ÿผ์€ ๋ณต๊ตฌํ•  ์ˆ˜ ์—†๋‹ค.

 

ALTER TABLE tbl_member
DROP COLUMN bigo;

-- Table TBL_MEMBER์ด(๊ฐ€) ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


5) tbl_member ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์„  tbl_customer ๋ณ€๊ฒฝ(์ˆ˜์ •)

RENAME tbl_member TO tbl_customer;
-- ํ…Œ์ด๋ธ” ์ด๋ฆ„์ด ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.



-- ํ…Œ์ด๋ธ” ์ƒ์„ฑ (์„œ๋ธŒ์ฟผ๋ฆฌ)
1) ํ…Œ์ด๋ธ”์„ ๋งŒ๋“œ๋Š” ๊ฐ€์žฅ ๋‹จ์ˆœํ•˜๋ฉด์„œ๋„ ์ผ๋ฐ˜์ ์ธ ๋ช…๋ น ํ˜•์‹์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•.
2) SUBQUERY๋ฅผ ์ด์šฉํ•œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ ๋ฐฉ๋ฒ•.
   ใ„ฑ. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ…Œ์ด๋ธ”์ด ์žˆ๊ณ ,
   ใ„ด. SELECT ~ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ 
   ใ„ท. ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” ์ƒ์„ฑ +  ๋ฐ์ดํ„ฐ ์ž๋™ ์ถ”๊ฐ€.
   ใ„น. ํ˜•์‹)
   CREATE TABLE ํ…Œ์ด๋ธ”๋ช… [ ์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช…... ]
   AS ์„œ๋ธŒ์ฟผ๋ฆฌ;
   
   ใ…) ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ํŠน์ • ์ปฌ๋Ÿผ๊ณผ ํ–‰์„ ์ด์šฉํ•œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ.
   ใ…‚) ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋œ๋‹ค.
   ใ……)CREATE TABLE ํ…Œ์ด๋ธ”๋ช… [ ์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช…... ]   
       ์ปฌ๋Ÿผ๋ช…์„ ๋ช…์‹œํ•  ๊ฒฝ์šฐ์—๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ปฌ๋Ÿผ์ˆ˜์™€ ๊ฐ™๊ฒŒ ๋ช…์‹œํ•ด์•ผ ๋œ๋‹ค. 
                  ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ปฌ๋Ÿผ๋ช…๊ณผ ๊ฐ™๊ฒŒ ๋œ๋‹ค. 
   ใ…‡) ์ œ์•ฝ์กฐ๊ฑด์€ ๋ณต์‚ฌ๋˜์ง€ ์•Š๋Š”๋‹ค.  ( PK X, FK X,       NN ์ œ์•ฝ์กฐ๊ฑด์€ ๋ณต์‚ฌ๊ฐ€ ๋œ๋‹ค. )  
   

SELECT *
   FROM user_constraints
   WHERE table_name = UPPER(  'tbl_emp10'  );   
   WHERE table_name = UPPER(  'emp'  );  -- PK, FK
   
   user_ ์ ‘๋‘์–ด, dba_ , all_
   FROM user_tables;  -- tabs
   FROM user_users;


                  
  -- ์˜ˆ) emp ํ…Œ์ด๋ธ”์˜ ์‚ฌ์›๋ฒˆํ˜ธ,์‚ฌ์›๋ช…, ์ž…์‚ฌ์ผ์ž, pay ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ง€๋Š” ํ…Œ์ด๋ธ” ์ƒ์„ฑ
        +  10๋ฒˆ ๋ถ€์„œ์›๋งŒ ๋ฐ์ดํ„ฐ๋กœ ์ถ”๊ฐ€

   --CREATE TABLE tbl_emp10  -- [ ์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๋ช…... ]    ์ƒ๋žต
   CREATE TABLE tbl_emp10  ( eno, name, hdate , pay  )
   AS (
      SELECT empno, ename, hiredate, sal + NVL( comm, 0 )  pay
      FROM emp
      WHERE deptno = 10
   );
-- Table TBL_EMP10์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.



์ด๋ฆ„              ๋„?         ์œ ํ˜•           
--------       --      ------------ 
EMPNO                  NUMBER(4)    
ENAME                  VARCHAR2(10) 
HIREDATE                DATE         
PAY                          NUMBER     -- sal + comm

ใ…ˆ) ์›๋ž˜ ํ…Œ์ด๋ธ” ( CRUD X )  ๋ณด์กดํ•œ์ฑ„๋กœ ๋ณต์‚ฌ ํ…Œ์ด๋ธ”
       

CREATE TABLE tbl_empCopy 
     AS (  SELeCT * FROM emp );
--     Table TBL_EMPCOPY์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
SELECT *
FROM tbl_empcopy; -- delete, update, insert ๋“ฑ๋“ฑ



     ํ…Œ์ด๋ธ” ์‚ญ์ œ

     DROP TABLE tbl_emp10 PURGE;
     DROP TABLE tbl_empcopy PURGE;
   
-- Table TBL_EMP10์ด(๊ฐ€) ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
-- Table TBL_EMPCOPY์ด(๊ฐ€) ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


   ใ…Š) (๋ฌธ์ œ) ๊ธฐ์กด ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ๋ณต์‚ฌํ•ด์„œ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ 
           + ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ X
     

  CREATE TABLE tbl_empcopy
     AS (
            SELECT *
            FROM emp
            WHERE 1 = 0       -- ์ด ์กฐ๊ฑด์„ ์ฃผ๋ฉด ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌ๋˜๊ณ  ๋ฐ์ดํ„ฐ๋Š” ๋ณต์‚ฌ์•ˆ๋จ!
     );


DML - INSERT , UPDATE, DELETE ( COMMIT, ROLLBACK ) -- TRUNCATE
1. tbl_member ํ…Œ์ด๋ธ” ํ™•์ธ

SELECT *
FROM tabs
WHERE REGEXP_LIKE( table_name ,  'member', 'i' );


2. tbl_member ํ…Œ์ด๋ธ” ํ™•์ธ -> ์‚ญ์ œ

DROP TABLE  tbl_member PURGE;


3. tbl_member ํ…Œ์ด๋ธ” ์ƒ์„ฑ.
         ๋ฉค๋ฒ„(ํšŒ์›์ •๋ณด)
        ์ถ”์ถœ ์†์„ฑ(  ๋‚˜์ด, ์ƒ์ผ, ์„ฑ๋ณ„ ์ปฌ๋Ÿผ ) X -> ์™œ ? ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ
        
NOT NULL      NN
PRIMARY KEY   PK

CREATE TABLE tbl_member(
   id              VARCHAR2(10)  NOT NULL  CONSTRAINT PK_TBLMEMBER_ID   PRIMARY KEY -- PK
   , name          VARCHAR2(20)  NOT NULL
   , age           NUMBER(3)
   , birth         DATE
   , regdate       DATE   DEFAULT SYSDATE
   , point        NUMBER  DEFAULT 100
);


-- Table TBL_MEMBER์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

-- TBL_MEMBER ํ…Œ์ด๋ธ”์˜ ์ œ์•ฝ์กฐ๊ฑด ํ™•์ธ ( ์•”๊ธฐ )

SELECT *
FROM user_constraints
WHERE table_name = 'TBL_MEMBER';



PK : PK_TBLMEMBER_ID  / P
NN :  SYS_C009024     / C
NN :  SYS_C009025     / C
    ์ œ์•ฝ์กฐ๊ฑด๋ช…์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด  SYS_  ์ ‘๋‘์‚ฌ๋กœ ์ž๋™ ์ง€์ •๋œ๋‹ค. 
-- FK                  / R  

INSERT INTO tbl_member ( id, name, age, birth, regdate, point )
           VALUES      ( 'admin', '๊ด€๋ฆฌ์ž', 40,  TO_DATE('01/23/1980', 'MM/DD/YYYY'), SYSDATE, 1000);



ORA-00001: unique constraint (SCOTT.PK_TBLMEMBER_ID) violated
            ์œ ์ผ์„ฑ ์ œ์•ฝ์กฐ๊ฑด
          PK (= UK +NN ) ์ œ์•ฝ์กฐ๊ฑด ์œ„๋ฐฐ๋œ๋‹ค. - ID๋Š” ์ค‘๋ณต X ( ๊ณ ์œ ํ•œ ํ‚ค )

INSERT INTO tbl_member ( id, name, age, birth, regdate, point )
           VALUES      ( 'admin', 'ํ™๊ธธ๋™', 20,  TO_DATE('11/20/2003', 'MM/DD/YYYY'), SYSDATE, 1000);


INSERT INTO tbl_member ( id, name, age, birth )
           VALUES      ( 'hong', 'ํ™๊ธธ๋™', 20,  TO_DATE('11/20/2003', 'MM/DD/YYYY') );

--  ORA-00933: SQL command not properly ended
INSERT INTO tbl_member ( id, name, age )
           VALUES      ( 'park', '๋ฐ•์ง€์ˆœ', 25 );
--         1 ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

INSERT INTO tbl_member ( id, name  )
           VALUES      ( 'kim', '๊น€์ง€์ˆœ' );
-- 00947. 00000 -  "not enough values"
INSERT INTO tbl_member         VALUES      ( 'lee', '์ด์ง€์ˆœ', null, null , SYSDATE, 100 );           
-- 1 ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


 

์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” INSERT ๋ฌธ


  INSERT INTO ํ…Œ์ด๋ธ”๋ช…  [ ( ์ปฌ๋Ÿผ๋ช….... )  ] VALUES      ( ์ปฌ๋Ÿผ๊ฐ’...);
  INSERT INTO ํ…Œ์ด๋ธ”๋ช… ( ์„œ๋ธŒ์ฟผ๋ฆฌ );
  
  1) tbl_emp10 ํ…Œ์ด๋ธ” -> ์ƒ์„ฑ

  CREATE TABLE tbl_emp10
  AS ( 
         SELECT *
         FROM emp
         WHERE 1 = 0
       )


  2) 

  SELECT * 
  FROM tbl_emp10;


  3) 3๊ฐœ ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

  INSERT INTO tbl_emp10   ( 
                                 SELECT *
                                 FROM emp
                                 WHERE deptno = 10
                           );
  COMMIT;
  
  DROP TABLE tbl_emp10 PURGE;


  
-- [ Multitable insert ๋ฌธ  ] --
-- ํ•˜๋‚˜์˜ INSERT ๋ฌธ์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์— ๋™์‹œ์— ํ–‰์„ ์ž…๋ ฅํ•˜๋Š” ๋ฌธ
   
 --  4๊ฐ€์ง€
 1) unconditional insert all
    ์กฐ๊ฑด์ด ์—†๋Š” 
 2) conditional insert all
    ์กฐ๊ฑด์ด ์žˆ๋Š” 
 3) conditional first insert
 4) pivoting insert
  
  
  
  [ 1) unconditional insert all ]
  ์กฐ๊ฑด๊ณผ ์ƒ๊ด€์—†์ด ๊ธฐ์ˆ ๋˜์–ด์ง„ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

• ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ๋ถ€ํ„ฐ ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ ํ–‰์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ๊ฐ๊ฐ insert ์ ˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
• into ์ ˆ๊ณผ values ์ ˆ์— ๊ธฐ์ˆ ํ•œ ์ปฌ๋Ÿผ์˜ ๊ฐœ์ˆ˜์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค.

ใ€ํ˜•์‹ใ€‘
   INSERT ALL | FIRST
     [INTO ํ…Œ์ด๋ธ”1 VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)]
     [INTO ํ…Œ์ด๋ธ”2 VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)]
     .......
   Subquery;

์—ฌ๊ธฐ์„œ 
 ALL : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ํ•ด๋‹นํ•˜๋Š” insert ์ ˆ์— ๋ชจ๋‘ ์ž…๋ ฅ
 FIRST : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ํ•ด๋‹นํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ insert ์ ˆ์— ์ž…๋ ฅ
 subquery : ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์„ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ํ–‰์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๊ฐ insert ์ ˆ ์ˆ˜ํ–‰

  ์˜ˆ) emp ํ…Œ์ด๋ธ”์— 12๋ช… ์‚ฌ์› ์กด์žฌ
  SELECT * FROM emp;
  
  tbl_emp10 ํ…Œ์ด๋ธ”์ƒ์„ฑ
  tbl_emp20 ํ…Œ์ด๋ธ”์ƒ์„ฑ
  tbl_emp30 ํ…Œ์ด๋ธ”์ƒ์„ฑ
  tbl_emp40 ํ…Œ์ด๋ธ”์ƒ์„ฑ
  
 

  CREATE TABLE tbl_emp10
  AS 
     (
         SELECT *
         FROM emp
         WHERE deptno = 10
     )
  
    CREATE TABLE tbl_emp20
  AS 
     (
         SELECT *
         FROM emp
         WHERE deptno = 20
     )
     
       CREATE TABLE tbl_emp30
  AS 
     (
         SELECT *
         FROM emp
         WHERE deptno = 30
     )
     
       CREATE TABLE tbl_emp40
  AS 
     (
         SELECT *
         FROM emp
         WHERE deptno = 40
     )


  -- ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ๋ณต์‚ฌ + ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ

์ „๋ถ€๋‹ค ๋“ค์–ด๊ฐ„ ํ˜•ํƒœ์ž„
  

  CREATE TABLE tbl_emp10  AS      (         SELECT *         FROM emp         WHERE 1 = 0      );
  CREATE TABLE tbl_emp20  AS      (         SELECT *         FROM emp         WHERE 1 = 0      );
  CREATE TABLE tbl_emp30  AS      (         SELECT *         FROM emp         WHERE 1 = 0      );
  CREATE TABLE tbl_emp40  AS      (         SELECT *         FROM emp         WHERE 1 = 0      );

 

 INSERT ALL
        INTO tbl_emp10 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
        INTO tbl_emp20 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
        INTO tbl_emp30 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
        INTO tbl_emp40 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
   SELECT * FROM emp;

   -- 48๊ฐœ ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

  
    [ 2) conditional insert all ]
    ใ€ํ˜•์‹ใ€‘
   INSERT ALL
   WHEN ์กฐ๊ฑด์ ˆ1 THEN
     INTO [ํ…Œ์ด๋ธ”1] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
   WHEN ์กฐ๊ฑด์ ˆ2 THEN
     INTO [ํ…Œ์ด๋ธ”2] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
   ........
   ELSE
     INTO [ํ…Œ์ด๋ธ”3] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
   Subquery;
• subquery๋กœ๋ถ€ํ„ฐ ํ•œ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ํ–‰์„ ๋ฆฌํ„ด๋ฐ›์•„ WHEN...THEN์ ˆ์—์„œ ์ฒดํฌํ•œ ํ›„, ์กฐ๊ฑด์— ๋งž๋Š” ์ ˆ์— ๊ธฐ์ˆ ๋œ ํ…Œ์ด๋ธ”์— insert ์ ˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
• VALUES ์ ˆ์— ์ง€์ •ํ•œ DEFAULT ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ default๊ฐ’์ด ์ง€์ •๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด, MULL ๊ฐ’์ด ์‚ฝ์ž…๋œ๋‹ค.

  INSERT ALL
      WHEN deptno = 10 THEN
        INTO tbl_emp10 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
      WHEN deptno = 20 THEN  
        INTO tbl_emp20 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
      WHEN deptno = 30 THEN   
        INTO tbl_emp30 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
      ELSE  
        INTO tbl_emp40 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
   SELECT * FROM emp;


   -- 12๊ฐœ ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
   
    
 3) conditional first insert ๋ฌธ
    WHEN ~ THEN ์กฐ๊ฑด ์žˆ๋Š”

Conditional first insert ๋ฌธ์€ conditional insert all ๋ฌธ๊ณผ ๋™์ผํ•˜๊ฒŒ ํŠน์ • ์กฐ๊ฑด๋“ค์„ ๊ธฐ์ˆ ํ•˜์—ฌ ๊ทธ ์กฐ๊ฑด์— ๋งž๋Š” ํ–‰(row)๋“ค์„ ์›ํ•˜๋Š” ํ…Œ์ด๋ธ”์— ๋‚˜๋ˆ„์–ด ์‚ฝ์ž…ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜์ง€๋งŒ, conditional insert all ๋ฌธ๊ณผ ๋‹ฌ๋ฆฌ ์ฒซ ๋ฒˆ์งธ when ์ ˆ์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ ๋‹ค์Œ์˜ when ์ ˆ์€ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ๋ถ€ํ„ฐ ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ ํ–‰์„ ๋ฐ˜ํ™˜๋ฐ›์•„ when ... then ์ ˆ์—์„œ ์กฐ๊ฑด์„ ์ฒดํฌํ•œ ํ›„ ์กฐ๊ฑด์— ๋งž๋Š” ์ ˆ์— ๊ธฐ์ˆ ๋œ ํ…Œ์ด๋ธ”์— insert ์ ˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์—ฌ๋Ÿฌ ๊ฐœ์˜ when ... then ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ์ฒซ ๋ฒˆ์งธ when ์ ˆ์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด into ์ ˆ์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ๋‹ค์Œ์˜ when ์ ˆ์€ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

ใ€ํ˜•์‹ใ€‘
INSERT FIRST
        WHEN ์กฐ๊ฑด์ ˆ1 THEN
          INTO [ํ…Œ์ด๋ธ”1] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
        WHEN ์กฐ๊ฑด์ ˆ2 THEN
          INTO [ํ…Œ์ด๋ธ”2] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
        ........
        ELSE
          INTO [ํ…Œ์ด๋ธ”3] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
Sub-Query;

• conditional INSERT FIRST๋Š” ์กฐ๊ฑด์ ˆ์„ ๊ธฐ์ˆ ํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ’๋“ค์„ ์›ํ•˜๋Š” ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.
• ์—ฌ๋Ÿฌ ๊ฐœ์˜ WHEN...THEN์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์กฐ๊ฑด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
๋‹จ, ์ฒซ ๋ฒˆ์งธ WHEN ์ ˆ์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ๋‹ค๋ฉด, INTO ์ ˆ์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ๋‹ค์Œ์˜ WHEN ์ ˆ๋“ค์€ ๋” ์ด์ƒ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
• subquery๋กœ๋ถ€ํ„ฐ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ํ–‰์„ ๋ฆฌํ„ด ๋ฐ›์•„ when...then์ ˆ์—์„œ ์กฐ๊ฑด์„ ์ฒดํฌํ•œ ํ›„ ์กฐ๊ฑด์— ๋งž๋Š” ์ ˆ์— ๊ธฐ์ˆ ๋œ ํ…Œ์ด๋ธ”์— insert๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.
• ์กฐ๊ฑด์„ ๊ธฐ์ˆ ํ•œ when ์ ˆ๋“ค์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์ด ์—†์„ ๊ฒฝ์šฐ else์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ into ์ ˆ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. else์ ˆ์ด ์—†์„ ๊ฒฝ์šฐ ๋ฆฌํ„ด ๋œ ๊ทธํ–‰์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ฌด๋Ÿฐ ์ž‘์—…๋„ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

7934   MILLER   CLERK   7782   82/01/23   1300      10
 10๋ฒˆ ๋ถ€์„œ์›์ด๋ฉด์„œ ์žก์ด   CLERK ์ธ ๋ฐ€๋Ÿฌ  ์‚ฌ์›

INSERT FIRST
      WHEN deptno = 10 THEN
        INTO tbl_emp10 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
      WHEN job = 'CLERK' THEN  
        INTO tbl_emp20 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
      ELSE  
        INTO tbl_emp40 VALUES (  empno, ename, job, mgr, hiredate, sal, comm, deptno  )
   SELECT * FROM emp;


 4) Pivoting insert ๋ฌธ
 pivoting insert ๋ฌธ์€ unconditional insert all ๋ฌธ๊ณผ ๊ฑฐ์˜ ๋™์ผํ•˜๋‚˜ into ์ ˆ์— ๋‹จ ํ•œ ๊ฐœ์˜ ํ…Œ์ด๋ธ”๋งŒ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
 ๋Œ€๋ถ€๋ถ„์˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ๋•Œ ์œ ์‚ฌํ•œ ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜๋Š” ์ปฌ๋Ÿผ์€ ํ•˜๋‚˜์˜ ๊ณตํ†ต ์ปฌ๋Ÿผ์œผ๋กœ ์„ค๊ณ„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ณดํ†ต์ด๋‹ค.
ํ•˜์ง€๋งŒ, ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค๊ณ„ํ•˜์ง€ ์•Š๊ณ  ๊ฐ๊ฐ ํ•„์š”ํ•œ ์ปฌ๋Ÿผ์„ ๋”ฐ๋กœ ์„ค๊ณ„ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. 
pivoting insert ๋ฌธ์€ ์ด์™€ ๊ฐ™์ด ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ์˜ ์ปฌ๋Ÿผ์„ ์ฝ์–ด์„œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ์˜ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๊ด„ ์ž…๋ ฅ์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
์—ฌ๋Ÿฌ ๊ฐœ์˜ into ์ ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, into ์ ˆ ๋’ค์— ์˜ค๋Š” ํ…Œ์ด๋ธ”์€ ๋ชจ๋‘ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค. 
 
 
ใ€ํ˜•์‹ใ€‘
INSERT ALL
  WHEN ์กฐ๊ฑด์ ˆ1 THEN
  INTO [๋™์ผํ•œ ํ…Œ์ด๋ธ”1] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
  INTO [๋™์ผํ•œ ํ…Œ์ด๋ธ”1] VALUES (์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,...)
  ..........
Sub-Query;

• ์ฃผ๋กœ ์—ฌ๋Ÿฌ ๊ณณ์˜ ์‹œ์Šคํ…œ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ž‘์—…ํ•˜๋Š” dataware house์— ์ ํ•ฉํ•˜๋‹ค. ์ •๊ทœํ™” ๋˜์ง€ ์•Š์€ data source๋“ค์ด๋‚˜ ๋‹ค๋ฅธ format์œผ๋กœ ์ €์žฅ๋œ data source๋“ค์„ Oracle์˜ ๊ด€๊ณ„ํ˜• DB์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ์— ์ ํ•ฉํ•œ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
• ์ •๊ทœํ™” ๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ oracle์ด ์ œ๊ณตํ•˜๋Š” relationalํ•œ ํ˜•ํƒœ๋กœ ํ…Œ์ด๋ธ”์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์„ pivoting์ด๋ผ๊ณ  ํ•œ๋‹ค.
 

create table tbl_sales(
    employee_id        number(6)   ,
    week_id            number(2)   ,
    sales_mon          number(8, 2), -- ์›” ํŒ๋งค๋Ÿ‰
    sales_tue          number(8, 2) ,
    sales_wed          number(8, 2) ,
    sales_thu          number(8, 2) ,
    sales_fri          number(8, 2)   -- ๊ธˆ ํŒ๋งค๋Ÿ‰
); 
-- Table TBL_SALES์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
 insert into tbl_sales values(1101,4,100,150,80,60,120);
 insert into tbl_sales values(1102,5,300,300,230,120,150);
 
 COMMIT;

 

 create table tbl_sales_data(
   employee_id        number(6),
   week_id            number(2),
   sales              number(8,2)
);


 
  -- [ PIVOTING INSERT. ๋ฌธ ] == UNC~ INSERT ALL  

  insert all
  
    into tbl_sales_data values(employee_id, week_id, sales_mon)
    into tbl_sales_data values(employee_id, week_id, sales_tue)
    into tbl_sales_data values(employee_id, week_id, sales_wed)
    into tbl_sales_data values(employee_id, week_id, sales_thu)
    into tbl_sales_data values(employee_id, week_id, sales_fri)       
    
    select employee_id, week_id, sales_mon, sales_tue, sales_wed,
           sales_thu, sales_fri
    from tbl_sales;

 

ex)
a) 1101๋ฒˆ ์‚ฌ์›์ด 4์งธ์ฃผ ์›”์š”์ผ 100๊ฐœ ํ™”์š”์ผ 120๊ฐœ ์ˆ˜์š”์ผ 320๊ฐœ ๋ชฉ์š”์ผ 500๊ฐœ ~
๋ฅผ ํ…Œ์ด๋ธ”์— 
b)
1101 4์งธ์ฃผ ์›”์š”์ผ 100๊ฐœ
1101 4์งธ์ฃผ ํ™”์š”์ผ 120๊ฐœ
.
.
.
๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฆ‰
a ๋ฐ์ดํ„ฐ๋ฅผ b์— ์ง‘์–ด๋„ฃ๊ธฐ ์œ„ํ•ด์„œ Pivot insert ์‚ฌ์šฉ

 

-- UPDATE / DELETE 
-- ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” ์ƒ์„ฑ
--( ๋ฌธ์ œ1 ) insaํ…Œ์ด๋ธ”์—์„œ num, name ์ปฌ๋Ÿผ๋งŒ ๋ณต์‚ฌํ•ด์„œ tbl_score ํ…Œ์ด๋ธ” ์ƒ์„ฑ
   -- ์กฐ๊ฑด)  num <=1005 ๋ ˆ์ฝ”๋“œ ๋ณต์‚ฌ

   CREATE TABLE tbl_score
   AS (
               SELECT num, name
               FROM insa
               WHERE num <= 1005
   );


--( ๋ฌธ์ œ2 )  tbl_score ํ…Œ์ด๋ธ”์— kor, eng, mat, tot, avg, grade, rank ์ปฌ๋Ÿผ ์ถ”๊ฐ€

ALTER TABLE tbl_score
ADD (
         kor   NUMBER(3) DEFAULT 0
         , eng NUMBER(3) DEFAULT 0
         , mat NUMBER(3) DEFAULT 0
         , tot NUMBER(3)
         , avg NUMBER(5,2)
         , grade CHAR(1 CHAR)
         , rank NUMBER(3)
     );



 -- ( ๋ฌธ์ œ3 )  kor, eng, mat ์ ์ˆ˜๋ฅผ ์ž„์˜์˜ ์ •์ˆ˜( 0~100)๋กœ ์ˆ˜์ •.

   UPDATE tbl_score
   SET  kor = TRUNC(  dbms_random.value( 0, 101 )  )
        , eng = TRUNC(  dbms_random.value( 0, 101 )  )
        , mat = FLOOR(  dbms_random.value( 0, 101 )  );
   --WHERE ;
  COMMIT;


 -- ( ๋ฌธ์ œ4 )  ๋ชจ๋“  ํ•™์ƒ๋“ค์˜ tot, avg ๊ณ„์‚ฐ , ์ˆ˜์ •( UPDATE ) 
 
-- 100.81733122398513865487065964899694120306 -> tbl_socre - kor, eng, mat

UPDATE tbl_score
SET     tot = kor + eng +mat
        , avg = (kor + eng +mat)/3;       87.12[3 ๋ฐ˜์˜ฌ๋ฆฝ]131312313123132  number(5,2)
-- WHERE;


  -- ( ๋ฌธ์ œ5 ) ํ‰๊ท  ๋ฌด์กฐ๊ฑด ์†Œ์ˆ˜์  2์ž๋ฆฌ ๊นŒ์ง€ ์ถœ๋ ฅ  22.70

SELECT num, name, kor, eng, mat, tot
      , TO_CHAR( avg , '999.00' ) avg
      , grade, rank
FROm tbl_score;


 -- ( ๋ฌธ์ œ6 ) grade ๋“ฑ๊ธ‰ ์ปฌ๋Ÿผ ์ˆ˜์ •
 --     ํ‰๊ท ์ด 90 ์ด์ƒ A, 80 ์ด์ƒ B, 70 ์ด์ƒ C , 60 ์ด์ƒ D, ๊ทธ์™ธ  F  char(1  char)

UPDATE tbl_score
SET  grade = 'A'
WHERE avg BETWEEN 90 AND 100;

UPDATE tbl_score
SET  grade = 'B'
WHERE avg BETWEEN 80 AND 89;

UPDATE tbl_score
SET  grade = 'C'
WHERE avg BETWEEN 70 AND 79;

UPDATE tbl_score
SET  grade = 'D'
WHERE avg BETWEEN 60 AND 69;

UPDATE tbl_score
SET  grade = 'F'
WHERE avg BETWEEN 0 AND 59;


-- CASE ํ•จ์ˆ˜

UPDATE tbl_score
SET  grade =  CASE
                  WHEN avg >= 90 THEN 'A'
                  WHEN avg >= 80 THEN 'B'
                  WHEN avg >= 70 THEN 'C'
                  WHEN avg >= 60 THEN 'D'
                  ELSE 'F'
              END;
-- DECODE ํ•จ์ˆ˜ :    = ๋น„๊ต์—ฐ์‚ฐ์ž๋งŒ ์‚ฌ์šฉ.
UPDATE tbl_score
SET  grade =  DECODE(  TRUNC( avg/10 ) , 10, 'A', 9 , 'A', 8, 'B', 7, 'C', 6 , 'D', 'F') ;


-- ( ๋ฌธ์ œ 6 ) rank ๋“ฑ์ˆ˜ ์ฒ˜๋ฆฌ.... 

UPDATE tbl_score  a
SET rank = (     SELECT COUNT(*) + 1 FROM tbl_Score WHERE tot > a.tot );

SET rank = (
            SELECT r
             FROM ( 
                   SELECT num, tot, RANK() OVER( ORDER BY tot DESC ) r
                   FROM tbl_score
              ) t
            WHERE a.num =   t.num
);
COMMIT;



-- ( ๋ฌธ์ œ7 ) ๊ตญ์–ด๋ฌธ์ œ 1๊ฐœ ์ •๋‹ต ์—†์–ด์š”..
        --  ๋ชจ๋“  ํ•™์ƒ์˜ ๊ตญ์–ด ์ ์ˆ˜๋ฅผ 5์  ์ฆ๊ฐ€.

UPDATE tbl_score
SET kor = CASE 
                 WHEN kor >= 95 THEN   100
                 ELSE kor + 5
          END  ;



-- ( ๋ฌธ์ œ8 *** ) 1001 ๋ฒˆ ํ•™์ƒ์˜ ๊ตญ์–ด,์˜์–ด ์ ์ˆ˜๋ฅผ 1005๋ฒˆ ํ•™์ƒ์˜ ๊ตญ์–ด, ์˜์–ด ์ ์ˆ˜๋กœ ์ˆ˜์ •

UPDATE tbl_score
SET  kor = (  SELECT kor FROM tbl_score WHERE num = 1005 )
   , eng = (  SELECT eng FROM tbl_score WHERE num = 1005 )
WHERE num = 1001;


-- **** (๊ธฐ์–ต)

UPDATE tbl_score 
SET ( kor, eng ) =  (  SELECT kor, eng FROM tbl_score WHERE num = 1005 )
WHERE num = 1001;

COMMIT;


-- ( ๋ฌธ์ œ9 )tbl_score ํ…Œ์ด๋ธ”์—์„œ [์—ฌํ•™์ƒ]๋“ค๋งŒ ์˜์–ด ์ ์ˆ˜๋ฅผ 5์ ์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ.

UPDATE tbl_score
SET eng = CASE 
                 WHEN eng >= 95 THEN 100
                 ELSE   eng + 5
          END
WHERE  num   IN (
                    SELECT num
                    FROM insa
                    WHERE MOD( SUBSTR( ssn, -7, 1 ), 2 ) = 0 
                 );


-- ANY ์—ฐ์‚ฐ์ž --

UPDATE tbl_score
SET eng = CASE 
                 WHEN eng >= 95 THEN 100
                 ELSE   eng + 5
          END
WHERE  num   = ANY (          -- >= ALL       ์ตœ๊ณ ๊ธ‰์—ฌ์ž
                    SELECT num
                    FROM insa
                    WHERE MOD( SUBSTR( ssn, -7, 1 ), 2 ) = 0 
                 );


-- ์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ

UPDATE tbl_score 
SET eng = CASE 
                 WHEN eng >= 95 THEN 100
                 ELSE   eng + 5
          END
WHERE num = (
            SELECT ts.num
            FROM tbl_score ts,(
                        SELECT num, DECODE (MOD( SUBSTR(ssn, -7,1),2),0,'์—ฌ์ž') gender  
                        FROM insa)i
            WHERE ts.num = i.num AND gender IS NOT NULL
            ) ;



-- MERGE( ํ•ฉ๋ณ‘ํ•˜๋‹ค, ํ•ฉ์น˜๋‹ค )   : ๋ณ‘ํ•ฉ, ํ†ตํ•ฉ.
1) ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ๋น„๊ตํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ํ•ฉ์น˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์กฐ์ž‘์ด๋‹ค.
   A                   B  =>      A, B
2) ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•˜๋ฃจ์— ์ˆ˜๋งŒ๊ฑด์”ฉ ๋ฐœ์ƒํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์— ๊ด€๋ฆฌํ•  ๊ฒฝ์šฐ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋กœ ์ธํ•ด ์งˆ์˜๋ฌธ์˜ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋œ๋‹ค.
์ด๋Ÿฐ ๊ฒฝ์šฐ, ์ง€์ ๋ณ„๋กœ ๋ณ„๋„์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ด€๋ฆฌํ•˜๋‹ค๊ฐ€ ๋…„๋ง์— ์ข…ํ•ฉ ๋ถ„์„์„ ์œ„ํ•ด ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ํ•ฉ์น  ๋•Œ merge ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค.
3)  mergeํ•˜๊ณ ์ž ํ•˜๋Š” ์†Œ์Šค ํ…Œ์ด๋ธ”์˜ ํ–‰์„ ์ฝ์–ด ํƒ€ํ‚ท ํ…Œ์ด๋ธ”์— ๋งค์น˜๋˜๋Š” ํ–‰์ด ์กด์žฌํ•˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ UPDATE๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋งŒ์ผ ๋งค์น˜๋˜๋Š” ํ–‰์ด ์—†์„ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ํ–‰์„ ํƒ€ํ‚ท ํ…Œ์ด๋ธ”์—์„œ INSERT๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. 


  A                
   ํ™๊ธธ๋™  15        UPDATE     
   ๊น€๊ธธ๋™   5        INSERT 


4) merge ๋ฌธ์—์„œ where ์ ˆ์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋Œ€์‹  on์ด ์‚ฌ์šฉ๋œ๋‹ค. ๋˜ํ•œ when matched then ์ ˆ๊ณผ when not matched then ์ ˆ์—๋Š” ํ…Œ์ด๋ธ”๋ช… ๋Œ€์‹  alias๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

5.ใ€ํ˜•์‹ใ€‘
    MERGE [hint] INTO [schema.] {table ¦ view} [t_alias]
      USING {{[schema.] {table ¦ view}} ¦ subquery} [t_alias]
      ON (condition) [merge_update_clause] [merge_insert_clause] [error_logging_clause];

ใ€merge_update_clause ํ˜•์‹ใ€‘
   WHEN MATCHED THEN UPDATE SET {column = {expr ¦ DEFAULT},...}
     [where_clause] [DELETE where_clause]

ใ€merge_insert_clause ํ˜•์‹ใ€‘
   WHEN NOT MATCHED THEN INSERT [(column,...)]
    VALUES ({expr,... ¦ DEFAULT}) [where_clause]
   
ใ€where_clause ํ˜•์‹ใ€‘
   WHERE condition

ใ€error_logging_clause ํ˜•์‹ใ€‘
   LOG ERROR [INTO [schema.] table] [(simple_expression)]
     [REJECT LIMIT {integer ¦ UNLIMITED}]

6) ํ…Œ์ŠคํŠธ

CREATE TABLE tbl_emp(
   id   number not null constraint PK_tblEmp_id primary key
   , name varchar2(10) not null
   , salary number
   , bonus  number default 100
);
 insert into tbl_emp(id,name,salary) values(1001,'jijoe',150);
 insert into tbl_emp(id,name,salary) values(1002,'cho',130);
 insert into tbl_emp(id,name,salary) values(1003,'kim',140);
COMMIT;
CREATE TABLE tbl_bonus(
  id number
  , bonus number default 100
);
INSERT INTO tbl_bonus ( id )  ( SELECT id FROM tbl_emp );
COMMIT;
INSERT INTO tbl_bonus VALUES ( 1004, 50 );


tbl_bonus
1001   100
1002   100
1003   100
1004   50

tbl_emp
1001   jijoe   150   100
1002   cho   130   100
1003   kim   140   100

-- MERGE : tbl_emp + tbl_bonus ๋‘ ํ…Œ์ด๋ธ” ๋ณ‘ํ•ฉ

-- ORA-00942: table or view does not exist
-- USING  tbl_emp  e  4์ปฌ๋Ÿผ X

USING ( SELECT id, salary FROM tbl_emp ) e
ON ( b.id = e.id )
WHEN MATCHED THEN -- UPDATE
   UPDATE   SET b.bonus = b.bonus   + e.salary * 0.01
WHEN NOT MATCHED THEN -- INSERT
  INSERT (b.id, b.bonus) VALUES ( e.id, e.salary * 0.01 );



-- ๋ณ‘ํ•ฉ ๋ฌธ์ œ) 

CREATE TABLE tbl_merge1
(
   id number primary key
   , name varchar2(20)
   , pay number
   , sudang number            --
);

INSERT INTO tbl_merge1 (id, name, pay, sudang) VALUES (1, 'a', 100, 10);
INSERT INTO tbl_merge1 (id, name, pay, sudang) VALUES (2, 'b', 150, 20);
INSERT INTO tbl_merge1 (id, name, pay, sudang) VALUES (3, 'c', 130, 0);

COMMIT;
CREATE TABLE tbl_merge2
(
   id number primary key 
   , sudang number            --
);
    
INSERT INTO tbl_merge2 (id, sudang) VALUES (2,5);
INSERT INTO tbl_merge2 (id, sudang) VALUES (3,10);
INSERT INTO tbl_merge2 (id, sudang) VALUES (4,20);

COMMIT;



--  [ tbl_merge2 ] ํ…Œ์ด๋ธ”์—  ๋‘ ๊ฐœ ํ…Œ์ด๋ธ”์˜ ์ˆ˜๋‹น์„  ๋ณ‘ํ•ฉํ•ด์„œ ์ถœ๋ ฅ(์กฐํšŒ)
SELECT id, sudang
FROM tbl_merge1;

1   10               X  INSERT 
2   20               O  UPDATE 
3   0                O  UPDATE

SELECT id, sudang
FROM tbl_merge2; 

2   5
3   10
4   20 

-- ๋ณ‘ํ•ฉ ์ฟผ๋ฆฌ ์ž‘์„ฑ, ํ™•์ธ.--

1   10 INSERT 
2   15 UPDATE 
3   10 UPDATE 
4   20

MERGE INTO tbl_merge2 t2
USING ( SELECT id, sudang FROM tbl_merge1 ) t1
ON ( t1.id = t2.id )
WHEN MATCHED THEN     -- UPDATE
   UPDATE  
   SET t2.sudang = t2.sudang + t1.sudang
WHEN NOT MATCHED THEN -- INSERT
  INSERT  ( t2.id, t2.sudang ) VALUES ( t1.id, t1.sudang );

 



-- ์ œ์•ฝ ์กฐ๊ฑด( constraint ) --
1.  ์ œ์•ฝ์กฐ๊ฑด์„ ํ™•์ธ : user_constraints ๋ทฐ(View) -- (์‹œํ—˜)

SELECT *
FROM user_constraints
WHERE table_name = 'EMP';



2. ์ œ์•ฝ์กฐ๊ฑด ์‚ฌ์šฉํ•˜๋Š” ๋ชฉ์  ? 
  ใ„ฑ. ํ…Œ์ด๋ธ”์—  ํ–‰์„ ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ( DML )ํ•  ๋•Œ ์ ์šฉ๋˜๋Š” ๊ทœ์น™
     DATA INTERGRITY( ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ )์„ ์œ„ํ•ด์„œ ์ œ์•ฝ ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•œ๋‹ค. 
     ์˜ˆ) ID  ๊ณ ์œ ํ•œ๊ฐ’ - ์ค‘๋ณต X, ์œ ์ผํ•œ ๊ฐ’
         ID  ์ˆ˜์ • X
    ์˜ˆ)  kor  NUMBER(3)   -999~999  101์ถ”๊ฐ€  , ์ˆ˜์ •

3. ๋ฌด๊ฒฐ์„ฑ(integrity )
  - ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•์„ฑ๊ณผ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๊ณ , ๋ฐ์ดํ„ฐ์— ๊ฒฐ์†๊ณผ ๋ถ€์ •ํ•ฉ์ด ์—†์Œ์„ ๋ณด์ฆํ•˜๋Š” ๊ฒƒ
  - ์˜ˆ๋ฅผ ๋“ค์–ด, 
     ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ(data integrity)์ด๋ผ ํ•˜๋ฉด
     ๋ฐ์ดํ„ฐ๋ฅผ ๋ณดํ˜ธํ•˜๊ณ , ํ•ญ์ƒ ์ •์ƒ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•˜๋Š”  ๊ฒƒ
3-2. ๋ฌด๊ฒฐ์„ฑ 3๊ฐ€์ง€ ์ข…๋ฅ˜
    • 1) ๊ฐœ์ฒด ๋ฌด๊ฒฐ์„ฑ(Entity Integrity) : pk ์ค‘๋ณต์•ˆ๋จ
         emp  PK- empno ์‚ฌ์›๋ฒˆํ˜ธ
         7369  SMITH~         
         INSERT     empno    7369 KENIK  X
    
    • 2) ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ(Relational Integrity) : dept ํ…Œ์ด๋ธ”์˜ deptno๋ฅผ ์ฐธ์กฐํ•˜๋Š”๋ฐ ์—†์œผ๋ฉด ์•ˆ๋จ
    dept 
    deptno 10,20,30,40 
    
    emp
    INSERT     deptno 50  X
    
    • 3) ๋„๋ฉ”์ธ ๋ฌด๊ฒฐ์„ฑ(domain integrity)    : ํ…Œ์ด๋ธ” ๋งŒ๋“ค๋•Œ ์ˆซ์ž 3๊ฐœ๋กœ ์„ค์ •ํ–ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ๋ฌธ์ž๋Š” ์•ˆ๋จ  
      zipcode    000-000      12A-12B  X
      ssn          14             15   X

4. ์ œ์•ฝ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  : ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด ์ œ์•ฝ์กฐ๊ฑด ์‚ฌ์šฉ
5. ์ œ์•ฝ์กฐ๊ฑด์˜ ํŠน์ง•

    • DML ์ž‘์—…์—์„œ ์ž˜๋ชป๋˜๋Š” ๊ฒƒ์„ ์ œ์•ฝ์กฐ๊ฑด์— ์˜ํ•ด [๋ฐฉ์ง€]ํ•œ๋‹ค.
    • ๋ชจ๋“  ์ •๋ณด๊ฐ€ [***๋ฐ์ดํ„ฐ ์‚ฌ์ „***]์— ์ €์žฅ๋œ๋‹ค.
    • ์–ธ์ œ๋“ ์ง€  ์ œ์•ฝ์กฐ๊ฑด์„  disable, enable์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
    • ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๊ฐ€ ์ฆ‰์‹œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋„˜๊ฒจ์ง„๋‹ค.
    • ํ•œ ์ปฌ๋Ÿผ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ œ์•ฝ์กฐ๊ฑด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ํ•˜๋‚˜์˜ ์ปฌ๋Ÿผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ์ปฌ๋Ÿผ์„ ์กฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ key๋ฅผ ๊ตฌ์„ฑํ•˜๋Š”
        composit key(๋ณตํ•ฉํ‚ค)๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: PRIMARY KEY(pno, ename) ****
    • ์ œ์•ฝ์กฐ๊ฑด์˜ ๊ด€๋ฆฌ๋Š” DB server๊ฐ€ ๋‹ด๋‹นํ•œ๋‹ค.


6.  ์ œ์•ฝ์กฐ๊ฑด ์„ ์–ธ ๋ฐ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 
• CREATE TABLE
• ALTER TABLE
• CREATE VIEW
• ALTER VIEW


-- ์ œ์•ฝ์กฐ๊ฑด ๋ณต์‚ฌ X, 
CREATE TABLE tbl_empcopy  -- empno ์ปฌ๋Ÿผ PK ์ œ์•ฝ์กฐ๊ฑด ์„ค์ •ํ•ด ์ค˜์•ผ ๋œ๋‹ค. 
AS
  (  SELECT * FROM emp );
--
SELECT *
FROM tbl_empcopy;
-- ID๊ฐ€ ๋˜‘๊ฐ™์€ ๋‹ค๋ฅธ ์‚ฌ์›์„ ์ถ”๊ฐ€
INSERT INTO tbl_empcopy (empno, ename) VALUES ( 7369, 'KENIK' );

7. ์ œ์•ฝ์กฐ๊ฑด( constraint )์„  ์ƒ์„ฑํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•
  1) IN-LINE ์ œ์•ฝ์กฐ๊ฑด ๋ฐฉ๋ฒ• (== COLUMN LEVEL )
  2) OUT-OF-LINE ์ œ์•ฝ์กฐ๊ฑด ๋ฐฉ๋ฒ•( == TABLE LEVEL )

8. ์ œ์•ฝ ์กฐ๊ฑด์˜ 5๊ฐ€์ง€ ์ข…๋ฅ˜
  1) PRIMARY KEY( PK )
  2) FOREIGN KEY( FK )
  3) NOT NULL   ( NN )
  4) UNIQUE KEY ( UK )
  5) CHECK      ( CK )



  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ