[Day11] Oracle11 [10/12]


--1.  truncate / delete / drop ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜์„ธ์š”
--     DDL : CREATE, ALTER , [ DROP ] 
--     DML : INSERT, UPDATE, [DELETE], [TRUNCATE], RENAME + ์ปค๋ฐ‹, ๋กค๋ฐฑ
                                                       ์‚ญ์ œ         ์‚ญ์ œ + ์ž๋™ ์ปค๋ฐ‹.

--2.  insert ๋ฌธ ์ˆ˜ํ–‰ ์ค‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค๋ฉด ์ด์œ ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์„ธ์š”
--  ใ„ฑ. 00947. 00000 -  "not enough values"
     INSERT INTO emp ( empno, ename, hiredate ) VALUES ( 2222, 'aaa');
--  ใ„ด. ORA-00001: unique constraint (SCOTT.SYS_C007770) violated  
      SYS_?????  ์ œ์•ฝ์กฐ๊ฑด ์ด๋ฆ„ ์ž๋™ ๋ถ€์—ฌ
      PK_ํ…Œ์ด๋ธ”๋ช…_์ปฌ๋Ÿผ๋ช…
      PK_EMP_EMPNO
      UK ์œ ์ผ์„ฑ ์ œ์•ฝ์กฐ๊ฑด, ์ค‘๋ณต X
      
    empno 7369 SMITH
    INSERT    empno    VALUES ( 7369 )  PK ์ œ์•ฝ์กฐ๊ฑด ์œ„๋ฐฐ = UK + NN
    
     ๊ฐœ์ฒด ๋ฌด๊ฒฐ์„ฑ..
      
--  ใ„ท. ORA-02291: integrity constraint (SCOTT.FK_DEPTNO) violated - parent key not found
--    ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด ์œ„๋ฐฐ :  FK_DEPTNO ํฌ๋ฆฐํ‚ค==์ฐธ์กฐํ‚ค== ์™ธ๋ž˜ํ‚ค
    dept ํ…Œ์ด๋ธ”์— : deptno 10,20,30,40     ๋ถ€๋ชจํ…Œ์ด๋ธ”( parent )
    
    ๊ด€๊ณ„: RDBMS      ์†Œ์†๊ด€๊ณ„  dept.deptno(PK) -> emp.deptno(FK) 
    
    INSERT emp ํ…Œ์ด๋ธ”์—์„œ : deptno(FK) 50  ์ž์‹ํ…Œ์ด๋ธ”( child )
    
    ๊ฐœ์ฒด, [์ฐธ์กฐ], ๋„๋ฉ”์ธ ๋ฌด๊ฒฐ์„ฑ
    
    kor NUMBER(3)  -999~999                  0~100 CHECK ์ œ์•ฝ์กฐ๊ฑด ์„ค์ •


--3. ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ
--  ใ„ฑ. deptno, dname, empno, ename, sal+nvl(comm,0) pay, grade ์ปฌ๋Ÿผ์„ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” ์ƒ์„ฑ
--  ใ„ด. ํ…Œ์ด๋ธ”๋ช… : tbl_empdeptgrade   

dept : deptno, dname
emp  : empno, ename, sal, comm
salgrade : grade 

3๊ฐœ ํ…Œ์ด๋ธ” ์กฐ์ธ
-- ORA-00918: column ambiguously defined

CREATE TABLE tbl_empdeptgrade
AS ( 
     SELECT d.deptno, dname, empno, ename, sal+nvl(comm,0) pay, grade
     FROM emp e JOIN dept d ON d.deptno = e.deptno
                JOIN salgrade s ON e.sal BETWEEN losal AND hisal
);



--4-1. insa ํ…Œ์ด๋ธ”์—์„œ num, name ๊ฐ€์ ธ์™€์„œ tbl_score ํ…Œ์ด๋ธ” ์ƒ์„ฑ
--     (tbl_score ํ…Œ์ด๋ธ”์ด ์žˆ์œผ๋ฉด ์‚ญ์ œ ํ›„ ์‹œ์ž‘ํ•˜์„ธ์š” )

CREATE TABLE tbl_score
AS ( 
     SELECT num, name
     FROM insa
);



--4-2. kor, eng, mat, tot, avg , grade, rank ์ปฌ๋Ÿผ ์ถ”๊ฐ€

ALTER TABLE tbl_score
ADD (
        kor NUMBER(3)
        , 
        
        , grade NVARCHAR2(3)      --  CHAR(1 CHAR) 'A'~'F'
        , rank NUMBER(3)
);



--4-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 = TRUNC( dbms_random.value(0, 101) );

dbms_random.value -> ์‹ค์ˆ˜ ๊ฐ€์ ธ์˜ด

TRUNC ํ†ตํ•ด์„œ ์ •์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผํ•จ

--4-4. ์ด์ , ํ‰๊ท , ๋“ฑ๊ธ‰, ๋“ฑ์ˆ˜ ์ˆ˜์ •     *****
--    ์กฐ๊ฑด)
--     ๋“ฑ๊ธ‰์€ ๋ชจ๋“  ๊ณผ๋ชฉ์ด 40์ ์ด์ƒ์ด๊ณ , ํ‰๊ท  60 ์ด์ƒ์ด๋ฉด "ํ•ฉ๊ฒฉ"
--           ํ‰๊ท  60 ์ด์ƒ์ด๋ผ๋„ ํ•œ ๊ณผ๋ชฉ์ด๋ผ 40์  ๋ฏธ๋งŒ์ด๋ผ๋ฉด  "๊ณผ๋ฝ"
--           ๊ทธ์™ธ๋Š” "๋ถˆํ•ฉ๊ฒฉ" ์ด๋ผ๊ณ  ์ €์žฅ.

์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ๋Šฅ์‚ฌ/๊ธฐ์‚ฌ ์‹ค๊ธฐ          ๊ณผ๋ฝ  40 ๋ฏธ๋งŒ,  ํ‰๊ท  60 

UPDATE tbl_score  a
SET  tot =  kor+eng+mat
, avg = (kor+eng+mat)/3   -- NUMBER(5,2)
, grade = CASE
                    WHEN  kor>=40 AND eng>=40 AND mat>=40 AND ํ‰ >=60 THEN 'ํ•ฉ๊ฒฉ'
                    WHEN (kor<40 OR eng<40 OR mat<40) AND ํ‰ >=60  THEN '๊ณผ๋ฝ'
                    ELSE '๋ถˆํ•ฉ๊ฒฉ'
          END
, rank = (  SELECT COUNT(*)+1  FROM tbl_score b WHERE b.์ด > a.์ด)


--5.  emp ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋ฅผ ํ™•์ธํ•˜๊ณ , ์ œ์•ฝ์กฐ๊ฑด์„ ํ™•์ธํ•˜๊ณ , ์ž„์˜์˜ ์‚ฌ์› ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” INSERT ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
--   ใ„ฑ. ๊ตฌ์กฐํ™•์ธ ์ฟผ๋ฆฌ 
DESC emp;

--   ใ„ด. ์ œ์•ฝ์กฐ๊ฑด ํ™•์ธ ์ฟผ๋ฆฌ

SELECT *
FROM user_constraints
WHERE table_name = 'EMP'; -- ๋ทฐ(VIEW)
FROM user_users;
FROM user_tables;


--   ใ„ท. INSERT ์ฟผ๋ฆฌ 
--
--6-1. emp ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌํ•ด์„œ ์ƒˆ๋กœ์šด tbl_emp10, tbl_emp20, tbl_emp30, tbl_emp40 ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์„ธ์š”. 

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 );



--6-2. emp ํ…Œ์ด๋ธ”์˜ ๊ฐ ๋ถ€์„œ์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์›์ •๋ณด๋ฅผ  ์œ„์—์„œ ์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ”์— INSERT ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.   

๋‹ค์ค‘ INSERT๋ฌธ  4๊ฐ€์ง€
1. UC I A           X
2. C I A
3. P I              X
4. C I F

INSERT ALL
      WHEN deptno = 10 THEN
        INTO tbl_emp10 VALUES ( ์นผ๋Ÿผ๋ช…~ )
      WHEN deptno = 20 THEN  
        INTO tbl_emp20 VALUES ( ์นผ๋Ÿผ๋ช…~ )
      WHEN deptno = 30 THEN   
        INTO tbl_emp30 VALUES ( ์นผ๋Ÿผ๋ช…~ )
      ELSE  
        INTO tbl_emp40 VALUES ( ์นผ๋Ÿผ๋ช…~ )
   SELECT * FROM emp;



--7. ์กฐ๊ฑด์ด ์žˆ๋Š” ๋‹ค์ค‘ INSERT ๋ฌธ์—์„œ  INSERT ALL ๊ณผ INSERT FIRST ๋ฌธ์— ๋Œ€ํ•œ ์ฐจ์ด์ ์„ ์„ค๋ช…ํ•˜์„ธ์š”.
INSERT ALL : ๋ชจ๋“  WHEN ์กฐ๊ฑด ์‹คํ–‰
INSERT FIRST : ์ฒซ๋ฒˆ์งธ ๋งŒ์กฑํ•˜๋Š” WHEN๋งŒ ์‹คํ–‰

 

--8. ์ œ์•ฝ์กฐ๊ฑด( Contratrint ) 
--  ใ„ฑ. ์ œ์•ฝ์กฐ๊ฑด์ด๋ž€ ?  ํ…Œ์ด๋ธ”์— DML ์ž‘์—… ์‹œ ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ ์œ„ํ•œ ๊ทœ์น™ 
--       ๋ฐ์ดํƒ€ ๋ฌด๊ฒฐ์„ฑ ? ๋ฐ์ดํ„ฐ๊ฐ€ ํ—ˆ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฐ’์œผ๋กœ ์ถ”๊ฐ€,์ˆ˜์ •,์‚ญ์ œ ์ œํ•œํ•˜๋Š” ํŠน์„ฑ.

--  ใ„ด. ์ œ์•ฝ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์„ธ์š”.
--        ์ปฌ๋Ÿผ๋ ˆ๋ฒจ - IN-LINE
          ํ…Œ์ด๋ธ”๋ ˆ๋ฒจ - OUT-LINE
          
--  ใ„ท. ์ œ์•ฝ์กฐ๊ฑด์˜ 5๊ฐ€์ง€ ์ข…๋ฅ˜ 
--      PK, FK, NN, CK, UK

--  ใ…. ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ ์ข…๋ฅ˜ ๋ฐ ์„ค๋ช…
       ๊ฐœ์ฒด,
       ์ฐธ์กฐ,
       ๋„๋ฉ”์ธ ๋ฌด๊ฒฐ์„ฑ


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

drop table ํ…Œ์ด๋ธ”๋ช…;

 

* ํœด์ง€ํ†ต์— ๋„ฃ๊ณ  ๋น„์šฐ๊ธฐ
DROP TABLE ํ…Œ์ด๋ธ”๋ช… purge;

purge recyclebin;


* ํœด์ง€ํ†ต์— ์žˆ๋Š” ํ…Œ์ด๋ธ” ๋ณต์›
FLASHBACK TABLE ํ…Œ์ด๋ธ”๋ช… TO BEFORE DROP



-- ์ œ์•ฝ์กฐ๊ฑด[constraint] --

1. ํ…Œ์ด๋ธ”์— ์ œ์•ฝ ์กฐ๊ฑด ํ™•์ธ : user_constraints ๋ทฐ(view);

SELECT *
FROM user_constraints
WHERE table_name = 'EMP';


2. ์ œ์•ฝ ์กฐ๊ฑด ? 
   ใ„ฑ. ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ(integrity constraint )์„ ์œ„ํ•ด์„œ ํ…Œ์ด๋ธ”์— ๋ ˆ์ฝ”๋“œ(ํ–‰)์„ ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œํ•  ๋•Œ ์ ์šฉ๋˜๋Š” ๊ทœ์น™
   ใ„ด. ํ…Œ์ด๋ธ”์˜ ์‚ญ์ œ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด์„œ๋„ ์ œ์•ฝ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•œ๋‹ค. 
         ex) DEPT( deptno PK ) -- EMP ( deptno  FK )

DELETE FROM dept
WHERE deptno = 30;
-- ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found


    ใ„ท. ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ? ๋ฐ์ดํ„ฐ๊ฐ€ ํ—ˆ๊ฐ€๋˜์ง€ ์•Š๋Š” ๊ฐ’์œผ๋กœ ์ถ”๊ฐ€,์ˆ˜์ •,์‚ญ์ œ ์ œํ•œํ•˜๋Š”   ํŠน์„ฑ
        • 1) ๊ฐœ์ฒด ๋ฌด๊ฒฐ์„ฑ(Entity Integrity)

INSERT INTO dept  VALUES ( 10, 'QC', 'SEOUL');
-- ORA-00001: unique constraint (SCOTT.PK_DEPT) violated

- ๋ฆด๋ ˆ์ด์…˜(table)์— ์ €์žฅ๋˜๋Š” ํŠœํ”Œ(tuple == ํ–‰ == row == record )์˜ ์œ ์ผ์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ œ์•ฝ์กฐ๊ฑด์ด๋‹ค == PK ๊ณ ์œ ํ‚ค

        • 2) ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ(Relational Integrity)
        

UPDATE emp
SET deptno = 90   -- deptํ…Œ์ด๋ธ”์—์„œ๋Š” 90 ๋ฒˆ ๋ถ€์„œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์ฐธ์กฐํ•  ์ˆ˜ ์—†๋‹ค. 
WHERE empno = 7369;

ORA-02291: integrity constraint (SCOTT.FK_DEPTNO) violated - parent key not found

 

 - ๋ฆด๋ ˆ์ด์…˜(ํ…Œ์ด๋ธ”) ๊ฐ„์˜ ๋ฐ์ดํ„ฐ์˜ [์ผ๊ด€์„ฑ]์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ œ์•ฝ์กฐ๊ฑด์ด๋‹ค

        
      • 3) ๋„๋ฉ”์ธ ๋ฌด๊ฒฐ์„ฑ(domain integrity)
           ์ปฌ๋Ÿผ(์†์„ฑ)์˜ ๊ฐ’์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…, ๊ธธ์ด, ๊ธฐ๋ณธ ํ‚ค, [์œ ์ผ์„ฑ], null ํ—ˆ์šฉ, ํ—ˆ์šฉ ์ œ์•ฝ ์กฐ๊ฑด
           ์˜ˆ) ๊ตญ์–ด ์ ์ˆ˜
               kor NUMBER(3) NOT NULL  DEFAULT 0   ์ž…๋ ฅ์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’ 0 ์œผ๋กœ ์ž…๋ ฅ
                                    ํ•„์ˆ˜ ์ž…๋ ฅ ์‚ฌํ•ญ        NOT NULL
                                    
                -999~999 ์ •์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.                  0<=  ์ •์ˆ˜ <=100 ( ๋ฒ”์œ„ ์ œํ•œ )


 3. ์ œ์•ฝ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ? DML ํ•  ๋•Œ ์ž˜๋ชป ์กฐ์ž‘๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ...
      DML  = insert, update, delete

 4. ์ œ์•ฝ ์กฐ๊ฑด์„ ์„ ์–ธ(์„ค์ •)ํ•˜๋Š” ๋ฐฉ๋ฒ•
    ใ„ฑ. CREATE TABLE ํ…Œ์ด๋ธ” ์ƒ์„ฑ
    ใ„ด. ALTER TABLE ํ…Œ์ด๋ธ” ์ˆ˜์ •..
    
    
 5. ์ œ์•ฝ ์กฐ๊ฑด์˜ ์ข…๋ฅ˜ 5๊ฐ€์ง€.
   ใ„ฑ. PRIMARY KEY ์ œ์•ฝ์กฐ๊ฑด( ๊ณ ์œ ํ‚ค  PK)
   ใ„ด. FOREIGN KEY ์ œ์•ฝ์กฐ๊ฑด( ์™ธ๋ž˜ํ‚ค, ์ฐธ์กฐํ‚ค FK )
   ใ„ท. NOT NULL ์ œ์•ฝ์กฐ๊ฑด (NN)
   ใ„น. UNIQUE ์ œ์•ฝ์กฐ๊ฑด ( ์œ ์ผ์„ฑ   UK )
   ใ…. CHECK ์ œ์•ฝ์กฐ๊ฑด ( CK )
   
 6. ์ œ์•ฝ์กฐ๊ฑด์„ ์ƒ์„ฑํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•
   ใ„ฑ. ์ปฌ๋Ÿผ ๋ ˆ๋ฒจ( column level ) == IN-LINE constaint ๋ฐฉ๋ฒ•
   ใ„ด. ํ…Œ์ด๋ธ” ๋ ˆ๋ฒจ( table level )  == OUT-OF-LINE constaint ๋ฐฉ๋ฒ•
       
------------------------------------------------------------------------     

-- ๊ฐ ์‚ฌ์›์„ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์œ ํ•œ ํ‚ค  : empno   PK  ์„ค์ • == NN+UK
--                       ํ›„ ๋ณดํ‚ค   : empno, [email X] , rrn, [ hp X ], ๋“ฑ๋“ฑ
--                                   0000              14

-- ์ œ์•ฝ์กฐ๊ฑด :  1๊ฐœ( PK )  DML X  -> ๋ถ€์—ฌ(์„ค์ •)
1) ์ปฌ๋Ÿผ ๋ ˆ๋ฒจ( in-line ๋ฐฉ์‹)
   -- PK, NN,  FK, UK, CK

 CREATE TABLE tbl_column_level
 (
    empno  number(4) NOT NULL  CONSTRAINT PK_TBLCOLUMNLEVEL_EMPNO PRIMARY KEY -- UK + NN
    , ename  varchar2(20) NOT NULL
    
    -- dept ํ…Œ์ด๋ธ”์˜ deptno(PK)       ์ฐธ์กฐํ•˜๋Š” ์™ธ๋ž˜ํ‚ค( FK )
    , deptno number(2) NOT NULL CONSTRAINT FK_TBLCOLUMNLEVEL_DEPTNO REFERENCES dept(deptno)
    -- -999~999 X   [0~100 ์ •์ˆ˜]
    , kor  number(3) CONSTRAINT CK_TBLCOLUMNLEVEL_KOR CHECK( kor BETWEEN 0 AND 100 )
    -- ์œ ์ผํ•ด์•ผ๋œ๋‹ค.( ์œ ์ผ์„ฑ )
    , email varchar2(250)  CONSTRAINT UK_TBLCOLUMNLEVEL_EMAIL UNIQUE
    -- ์„œ์šธ, ๋ถ€์‚ฐ, ๋Œ€๊ตฌ, ๋Œ€์ „   ๋ชฉ๋ก์—์„œ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฒดํฌ ์ œ์•ฝ ์„ค์ •
    , city varchar2(20) CONSTRAINT CK_TBLCOLUMNLEVEL_CITY CHECK( city IN ('์„œ์šธ', '๋ถ€์‚ฐ', '๋Œ€๊ตฌ', '๋Œ€์ „') )
 );
-- Table TBL_COLUMN_LEVEL์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
COMMIT;



-- ORA-02291: integrity constraint (SCOTT.FK_TBLCOLUMNLEVEL_DEPTNO) violated - parent key not found
INSERT INTO TBL_COLUMN_LEVEL ( empno, ename, deptno, kor, email, city )
VALUES ( 1111, 'ํ™๊ธธ๋™', 50, 100, 'hong@sist.com', null );

-- ORA-02290: check constraint (SCOTT.CK_TBLCOLUMNLEVEL_KOR) violated
-- NUMBER(3) -999~999           CHECK( kor between 0 AND 100 )
INSERT INTO TBL_COLUMN_LEVEL ( empno, ename, deptno, kor, email, city )
VALUES ( 1111, 'ํ™๊ธธ๋™', 40, 101, 'hong@sist.com', null );

INSERT INTO TBL_COLUMN_LEVEL ( empno, ename, deptno, kor, email, city )
VALUES ( 1111, 'ํ™๊ธธ๋™', 40, 100, 'hong@sist.com', null );
-- 1 ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

-- ORA-00001: unique constraint (SCOTT.UK_TBLCOLUMNLEVEL_EMAIL) violated
INSERT INTO TBL_COLUMN_LEVEL ( empno, ename, deptno, kor, email, city )
VALUES ( 1112, '๊น€๋”œ๋™', 40, 44, 'hong@sist.com', null );

-- ORA-02290: check constraint (SCOTT.CK_TBLCOLUMNLEVEL_CITY) violated
-- ์„œ์šธ ๋Œ€๊ตฌ ๋Œ€์ „ ๋ถ€์‚ฐ
INSERT INTO TBL_COLUMN_LEVEL ( empno, ename, deptno, kor, email, city )
VALUES ( 1112, '๊น€๋”œ๋™', 40, 44, 'hong2@sist.com', 'ํฌํ•ญ' );

-- NOT NULL ์ œ์•ฝ == ํ•„์ˆ˜์ž…๋ ฅ.
-- ORA-01400: cannot insert NULL into ("SCOTT"."TBL_COLUMN_LEVEL"."ENAME")
INSERT INTO TBL_COLUMN_LEVEL ( empno, ename, deptno, kor, email, city )
VALUES ( 1112,  null, 40, 44, 'hong2@sist.com', '๋Œ€์ „' );

SELECT *
FROM tbl_column_level
-- ORA-02290: check constraint (SCOTT.CK_TBLCOLUMNLEVEL_CITY) violated
UPDATE tbl_column_level
SET city = 'ํฌํ•ญ'
WHERE empno = 1111;




2) ํ…Œ์ด๋ธ” ๋ ˆ๋ฒจ( out-line ๋ฐฉ์‹)
-- NOT NULL ์ œ์•ฝ์กฐ๊ฑด์€ ํ…Œ์ด๋ธ” ๋ ˆ๋ฒจ ๋ฐฉ์‹์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค. 

CREATE TABLE tbl_table_level
 (
    empno  number(4)        NOT NULL  
    , ename  varchar2(20)   NOT NULL
    , deptno number(2)  
    , kor  number(3)   
    , email varchar2(250) 
    , city varchar2(20)
    
    -- ์ œ์•ฝ์กฐ๊ฑด
    -- ๋ณตํ•ฉํ‚ค์„ค์ • = ์ปฌ๋Ÿผ๋ ˆ๋ฒจ X, ํ…Œ์ด๋ธ” ๋ ˆ๋ฒจ O
    , CONSTRAINT PK_TBLCOLUMNLEVEL_EMPNO PRIMARY KEY( empno ) 
    , CONSTRAINT FK_TBLCOLUMNLEVEL_DEPTNO FOREIGN KEY(deptno) REFERENCES dept(deptno)
    , CONSTRAINT CK_TBLCOLUMNLEVEL_KOR CHECK( kor BETWEEN 0 AND 100 )
    , CONSTRAINT UK_TBLCOLUMNLEVEL_EMAIL UNIQUE( email )
    , CONSTRAINT CK_TBLCOLUMNLEVEL_CITY CHECK( city IN ('์„œ์šธ', '๋ถ€์‚ฐ', '๋Œ€๊ตฌ', '๋Œ€์ „') )
    
 );
-- Table TBL_TABLE_LEVEL์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


๋ณตํ•ฉํ‚ค

๊ธ‰์—ฌ์ง€๊ธ‰ ํ…Œ์ด๋ธ” ์˜ˆ์‹œ

์ด ํ…Œ์ด๋ธ”์—์„œ๋Š” PK๊ฐ€ ์—†์Œ

 -- ๋ณตํ•ฉํ‚ค( ์ง€๊ธ‰์ผ + ์‚ฌ์›๋ฒˆํ˜ธ  )

์„ฑ๋Šฅ ๋–จ์–ด์ง


 UPDATE ๊ธ‰์—ฌ์ง€๊ธ‰ํ…Œ์ด๋ธ”
 SET ์ง€๊ธ‰์•ก = 3,800,00
 WHERE ๋ณตํ•ฉํ‚ค
    22.08.25 AND  1111
   --> ์—ญ์ •๊ทœํ™”   seq ์ปฌ๋Ÿผ ์ถ”๊ฐ€ 
 

 ์ง€๊ธ‰์ผ      ์‚ฌ์›๋ฒˆํ˜ธ     ์ง€๊ธ‰์•ก         ...  ์‹œํ€€์Šคseq(PK)
 22.08.25    1111        3,500,000            1
 22.08.25    1112        3,500,000            2
 22.08.25    1113        3,500,000            3
 :
 22.09.25    1111        3,500,000
 22.09.25    1112        3,500,000
 22.09.25    1113        3,500,000
 :
 22.10.25    1111        3,500,000
 22.10.25    1112        3,500,000
 22.10.25    1113        3,500,000

-- ์ œ์•ฝ ์กฐ๊ฑด ํ™•์ธ

SELECT *
FROM user_constraints
WHERE table_name = 'TBL_TABLE_LEVEL' ;

 

  
C : NN,   CK
P : PK
U : UK
R : FK

 

OWNER       CONSTRAINT_NAME                            CONSTRAINT_TYPE
SCOTT        SYS_C009056                                       C                        // ์ž๋™์œผ๋กœ ์ œ์•ฝ์กฐ๊ฑด๋ช…์ด ๋ถ€์—ฌ(์„ค์ •)  SYS_   NN
SCOTT        SYS_C009057                                       C
SCOTT        CK_TBLCOLUMNLEVEL_KOR              C
SCOTT        CK_TBLCOLUMNLEVEL_CITY             C
SCOTT        PK_TBLCOLUMNLEVEL_EMPNO         P
SCOTT        UK_TBLCOLUMNLEVEL_EMAIL           U
SCOTT        FK_TBLCOLUMNLEVEL_DEPTNO        
  
 -- ๋„๋ฉ”์ธ ๋ฌด๊ฒฐ์„ฑ ์œ„๋ฐฐ ( UK, CK )
 kor    0~100  111 X
 -- ๊ฐœ์ฒด ๋ฌด๊ฒฐ์„ฑ( PK ์œ„๋ฐฐ )
 empno 1111 INSERT O
 empno 1111 INSERT X  PK
 -- ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ ( FK ์œ„๋ฐฐ )
 deptno 50   INSERT X      dept.deptno ์ฐธ์กฐ( 10,20,30,40)
  
 --์ œ์•ฝ์กฐ๊ฑด ์ œ๊ฑฐ --
 -- 1) TBL_TABLE_LEVEL ํ…Œ์ด๋ธ”์˜ PK ์ œ์•ฝ ์กฐ๊ฑด ์ œ๊ฑฐ. 

ALTER TABLE  TBL_TABLE_LEVEL
DROP PRIMARY KEY;

ALTER TABLE  TBL_TABLE_LEVEL
DROP CONSTRAINT PK_TBLCOLUMNLEVEL_EMPNO;   -- FK X  [CASCADE]X
-- Table TBL_TABLE_LEVEL์ด(๊ฐ€) ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


• ์ œ์•ฝ์กฐ๊ฑด์€ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๊ธฐ์กด์˜ constraint๋ฅผ ์‚ญ์ œ ํ›„ ์žฌ ์ƒ์„ฑํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
• constraint๋ฅผ ์‚ญ์ œํ• ๋ ค๋ฉด, ์ง์ ‘ constraint๋ช…์„ ์‚ฌ์šฉํ•ด์„œ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ 
 ๋˜๋Š”
 constraint๊ฐ€ ํฌํ•จ๋œ [ํ…Œ์ด๋ธ”์„ ์‚ญ์ œ]ํ•˜๋ฉด ๊ทธ ํ…Œ์ด๋ธ”์— ์†ํ•œ constraint๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋œ๋‹ค.
• ๋ฌด๊ฒฐ์„ฑ constraint๋ฅผ ์‚ญ์ œํ•  ๋•Œ, ๊ทธ constraint๋Š” ๋” ์ด์ƒ ์„œ๋ฒ„์— ์˜ํ•ด์„œ ์ ์šฉ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—
  ***[data dictionary]***์—์„œ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค.
• primary key๋Š” ํ…Œ์ด๋ธ”๋‹น ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜๋ฏ€๋กœ ,์‚ญ์ œ์‹œ constraint๋ช…์„ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ primary key ์ œ์•ฝ์กฐ๊ฑด์ด ์‚ญ์ œ๋œ๋‹ค.
์˜ˆ) 

ALTER TABLE ํ…Œ์ด๋ธ”๋ช… 
   DROP PRIMARY KEY;


๋ฐฉ๋ฒ•1)

ALTER TABLE ํ…Œ์ด๋ธ”๋ช… 
DROP [CONSTRAINT constraint๋ช… | PRIMARY KEY | UNIQUE(์ปฌ๋Ÿผ๋ช…)]
[CASCADE];

 

* CASCADE์˜ต์…˜์€ ์ฐธ์กฐํ•˜๋Š” FOREIGN KEY๊ฐ€ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฐฉ๋ฒ•2)

DROP TABLE ํ…Œ์ด๋ธ”๋ช… CASCADE CONSTRAINTS;


ํ…Œ์ด๋ธ”๊ณผ ๊ทธ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜๋Š” foreign key๋ฅผ ๋™์‹œ์— ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฉ๋ฒ•3)

DROP TABLESPACE ํ…Œ์ด๋ธ”์ŠคํŽ˜์ด์Šค๋ช… 
INCLUDING CONTENTS
CASCADE CONSTRAINTS;


 -- 2) ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์™„๋ฃŒ ํ›„ ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€ --
 ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด์€ ๊ธฐ์กด ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ๋„ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค
 . ALTER TABLE ... ADD CONSTRAINT ๋ฌธ์€ ๊ธฐ์กด ํ…Œ์ด๋ธ”์— ์ œ์•ฝ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น๋ฌธ์ด๋‹ค. 
 ํ•˜์ง€๋งŒ, NOT NULL ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด์˜ ์ถ”๊ฐ€๋Š” 'NULL ํ—ˆ์šฉ' ์ƒํƒœ๋ฅผ 'NULL ์ž…๋ ฅ๋ถˆ๊ฐ€' ์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ
 ALTER TABLE ... MODIFY ๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.


ใ€ํ˜•์‹ใ€‘
   ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
   ADD [CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด๋ช…] ์ œ์•ฝ์กฐ๊ฑดํƒ€์ž… (์ปฌ๋Ÿผ๋ช…);

-- ๋ฌธ์ œ) ๋ชจ๋“  ์ œ์•ฝ์กฐ๊ฑด ์‚ญ์ œ ์ฟผ๋ฆฌ ์ž‘์„ฑ  
SCOTT   SYS_C009056  NN
SCOTT   SYS_C009057  NN   

SCOTT   CK_TBLCOLUMNLEVEL_KOR
SCOTT   CK_TBLCOLUMNLEVEL_CITY
SCOTT   UK_TBLCOLUMNLEVEL_EMAIL
SCOTT   FK_TBLCOLUMNLEVEL_DEPTNO  

ALTER TABLE tbl_table_level 
DROP CONSTRAINT CK_TBLCOLUMNLEVEL_KOR;

ALTER TABLE tbl_table_level 
DROP CONSTRAINT CK_TBLCOLUMNLEVEL_CITY;

ALTER TABLE tbl_table_level 
DROP CONSTRAINT UK_TBLCOLUMNLEVEL_EMAIL;

ALTER TABLE tbl_table_level 
DROP CONSTRAINT FK_TBLCOLUMNLEVEL_DEPTNO;

ALTER TABLE tbl_table_level
DROP CONSTRAINT SYS_C009056; -- NN

ALTER TABLE tbl_table_level
DROP CONSTRAINT SYS_C009057;   -- NN


-- ๊ณ ๋ ค~ 

ALTER TABLE TBL_TABLE_LEVEL
MODIFY empno NULL;
ALTER TABLE TBL_TABLE_LEVEL
MODIFY ename NULL;


-- ๋ฌธ์ œ) ๋ชจ๋“  ์ œ์•ฝ ์กฐ๊ฑด ๋‹ค์‹œ ์ถ”๊ฐ€ํ•ด ์ฃผ์„ธ์š”.   
--SCOTT   SYS_C009056   C        ์ž๋™์œผ๋กœ ์ œ์•ฝ์กฐ๊ฑด๋ช…์ด ๋ถ€์—ฌ(์„ค์ •)  SYS_   NN

ALTER TABLE TBL_TABLE_LEVEL
ADD CHECK( empno IS NOT NULL );


--SCOTT   SYS_C009057   C

ALTER TABLE TBL_TABLE_LEVEL
MODIFY ename NOT NULL;


--SCOTT   CK_TBLCOLUMNLEVEL_KOR   C

ALTER TABLE TBL_TABLE_LEVEL
ADD CONSTRAINT CK_TBLTABLELEVEL_KOR  CHECK( kor BETWEEN 0 AND 100 );


--SCOTT   CK_TBLCOLUMNLEVEL_CITY   C

ALTER TABLE TBL_TABLE_LEVEL
ADD CONSTRAINT CK_TBLTABLELEVEL_CITY  CHECK( city IN ('์„œ์šธ', '๋ถ€์‚ฐ', '๋Œ€๊ตฌ', '๋Œ€์ „') );


--SCOTT   PK_TBLCOLUMNLEVEL_EMPNO   P

ALTER TABLE TBL_TABLE_LEVEL
ADD CONSTRAINT PK_TBLTABLELEVEL_EMPNO  PRIMARY KEY( empno );


--SCOTT   UK_TBLCOLUMNLEVEL_EMAIL   U

ALTER TABLE TBL_TABLE_LEVEL
ADD CONSTRAINT UK_TBLTABLELEVEL_EMAIL  UNIQUE( email );


--SCOTT   FK_TBLCOLUMNLEVEL_DEPTNO   R 

ALTER TABLE TBL_TABLE_LEVEL
ADD CONSTRAINT FK_TBLTABLELEVEL_DEPTNO FOREIGN KEY(deptno) REFERENCES dept(deptno);


-- ์ œ์•ฝ์กฐ๊ฑด ๋น„ํ™œ์„ฑํ™”/ํ™œ์„ฑํ™”

ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
ENABLE | DISABLE  CONSTRAINT ์ œ์•ฝ์กฐ๊ฑด๋ช…



-- *** FK ์„ค์ • ํ˜•์‹. --
1) ใ€์ปฌ๋Ÿผ๋ ˆ๋ฒจ์˜ ํ˜•์‹ใ€‘
        ์ปฌ๋Ÿผ๋ช… ๋ฐ์ดํ„ฐํƒ€์ž… CONSTRAINT constraint๋ช…
   REFERENCES ์ฐธ์กฐํ…Œ์ด๋ธ”๋ช… (์ฐธ์กฐ์ปฌ๋Ÿผ๋ช…) 
             [ON DELETE CASCADE | ON DELETE SET NULL]
             

    deptno  NUMBER(2) CONSTRAINT  FK_TBLCOLUMNLEVEL_DEPTNO     
          REFERENCES dept(deptno) [ON DELETE CASCADE | ON DELETE SET NULL];


2) ใ€ํ…Œ์ด๋ธ”๋ ˆ๋ฒจ์˜ ํ˜•์‹ใ€‘
        ์ปฌ๋Ÿผ๋ช… ๋ฐ์ดํ„ฐํƒ€์ž…,
        ์ปฌ๋Ÿผ๋ช… ๋ฐ์ดํ„ฐํƒ€์ž…,
         ...
        CONSTRAINT constraint๋ช… FOREIGN KEY(์ปฌ๋Ÿผ)
   REFERENCES ์ฐธ์กฐํ…Œ์ด๋ธ”๋ช… (์ฐธ์กฐ์ปฌ๋Ÿผ๋ช…)
             [ON DELETE CASCADE | ON DELETE SET NULL]

    , deptno  NUMBER(2)
    , CONSTRAINT FK_TBLCOLUMNLEVEL_DEPTNO FOREIGN KEY(depnto)   
           REFERENCES dept (deptno ) [ON DELETE CASCADE | ON DELETE SET NULL];


   ์˜ต์…˜ 1) ON DELETE CASCADE  : ๋ถ€๋ชจํ–‰์ด ์‚ญ์ œ๋  ๋•Œ ์ฐธ์กฐํ•˜๋Š” ์ž์‹ํ–‰๋„ ๊ฐ•์ œ๋กœ ์‚ญ์ œ.
   ์˜ต์…˜ 2) ON DELETE SET NULL :          "          "       ํ–‰์˜ FK ๊ฐ’์„ NULL๋กœ ์„ค์ •.      
  
  ์˜ˆ) 
    ใ„ฑ. emp -> tbl_emp ์ƒ์„ฑ 
        dept -> tbl_dept์ƒ์„ฑ
        

       DROP TABLE tbl_emp PURGE; 
       DROP TABLE tbl_dept PURGE;
       CREATE TABLE tbl_emp AS (  SELECT * FROM emp  );
       CREATE TABLE tbl_dept AS (  SELECT * FROM dept  );


       NN ์ œ์•ฝ์กฐ๊ฑด ๋ณต์‚ฌ ๋˜๊ณ  ๊ทธ ์™ธ ๋ชจ๋“  ์ œ์•ฝ์กฐ๊ฑด์€ ๋ณต์‚ฌ ๋˜์ง€ ์•Š๋Š”๋‹ค. 
       
     ใ„ด. tbl_dept ํ…Œ์ด๋ธ”์˜ PK( deptno ) ์„ค์ •
         ๊ธฐ์กด ํ…Œ์ด๋ธ”์— ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€ํ•  ๋•Œ .

     ALTER TABLE tbl_dept
     ADD CONSTRAINT PK_TBLDEPT PRIMARY KEY( deptno );


     ใ„ท. tbl_emp   ํ…Œ์ด๋ธ”์˜ FK( deptno)  ์„ค์ •

     ALTER TABLE tbl_emp
     ADD CONSTRAINT PK_TBLEDP_EMPNO PRIMARY KEY( empno );
     
     ALTER TABLE tbl_emp
     ADD CONSTRAINT FK_TBLEDP_DEPTNO FOREIGN KEY( deptno ) REFERENCES tbl_dept( deptno );


     ใ„น. ์ œ์•ฝ ์กฐ๊ฑด ํ™•์ธ..
     
    ํ…Œ์ŠคํŠธ) 

    -- ORA-02292: integrity constraint (SCOTT.FK_TBLEDP_DEPTNO) violated - child record found
    DELETE tbl_dept
    WHERE deptno = 10;


    
    ใ…. FK ์ œ์•ฝ์กฐ๊ฑด ์‚ญ์ œ.

     ALTER TABLE tbl_emp
     DROP CONSTRAINT FK_TBLEDP_DEPTNO;


    ใ…‚. FK ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€ + ON DELETE CASCADE ์˜ต์…˜ ์ถ”๊ฐ€.

    ALTER TABLE tbl_emp
     ADD CONSTRAINT FK_TBLEDP_DEPTNO FOREIGN KEY( deptno ) 
     REFERENCES tbl_dept( deptno      )
     ON DELETE CASCADE;\


    ใ……. FK ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€ + ON DELETE SET NULL ์˜ต์…˜ ์ถ”๊ฐ€.

    ALTER TABLE tbl_emp
     ADD CONSTRAINT FK_TBLEDP_DEPTNO FOREIGN KEY( deptno ) 
     REFERENCES tbl_dept( deptno      )
     ON DELETE SET NULL;

 


 -- JOIN( ์กฐ์ธ )
1) ๊ฐ™๊ฑฐ๋‚˜ ์„œ๋กœ ๋‹ค๋ฅธ [๋‘ ๊ฐœ ์ด์ƒ]์˜ ํ…Œ์ด๋ธ”์—์„œ ์ปฌ๋Ÿผ์„ ๊ฒ€์ƒ‰ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋•Œ, where์ ˆ์— [join ์กฐ๊ฑด์„ ์ž‘์„ฑ]ํ•˜์—ฌ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•œ๋‹ค  
2) ์ƒ˜ํ”Œ ํ…Œ์ด๋ธ”   
  -- <1> ์ฑ… ํ…Œ์ด๋ธ”

  CREATE TABLE book(
       b_id     VARCHAR2(10)    NOT NULL PRIMARY KEY   -- ์ฑ…ID
      ,title      VARCHAR2(100) NOT NULL  -- ์ฑ… ์ œ๋ชฉ
      ,c_name  VARCHAR2(100)    NOT NULL     -- c ์ด๋ฆ„
  --    ,  price  NUMBER(7) NOT NULL
 );
 
 -- Table BOOK์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
INSERT INTO book (b_id, title, c_name) VALUES ('a-1', '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค', '์„œ์šธ');
INSERT INTO book (b_id, title, c_name) VALUES ('a-2', '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค', '๊ฒฝ๊ธฐ');
INSERT INTO book (b_id, title, c_name) VALUES ('b-1', '์šด์˜์ฒด์ œ', '๋ถ€์‚ฐ');
INSERT INTO book (b_id, title, c_name) VALUES ('b-2', '์šด์˜์ฒด์ œ', '์ธ์ฒœ');
INSERT INTO book (b_id, title, c_name) VALUES ('c-1', '์›Œ๋“œ', '๊ฒฝ๊ธฐ');
INSERT INTO book (b_id, title, c_name) VALUES ('d-1', '์—‘์…€', '๋Œ€๊ตฌ');
INSERT INTO book (b_id, title, c_name) VALUES ('e-1', 'ํŒŒ์›Œํฌ์ธํŠธ', '๋ถ€์‚ฐ');
INSERT INTO book (b_id, title, c_name) VALUES ('f-1', '์—‘์„ธ์Šค', '์ธ์ฒœ');
INSERT INTO book (b_id, title, c_name) VALUES ('f-2', '์—‘์„ธ์Šค', '์„œ์šธ');

COMMIT;


  -- <2> ๋‹จ๊ฐ€ ํ…Œ์ด๋ธ”

  CREATE TABLE danga(
      b_id  VARCHAR2(10)  NOT NULL  -- PK , FK
      ,price  NUMBER(7) NOT NULL    -- ์ฑ… ๊ฐ€๊ฒฉ
      
      ,CONSTRAINT PK_dangga_id PRIMARY KEY(b_id)
      ,CONSTRAINT FK_dangga_id FOREIGN KEY (b_id)
              REFERENCES book(b_id)
              ON DELETE CASCADE
);
-- Table DANGA์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

INSERT INTO danga (b_id, price) VALUES ('a-1', 300);
INSERT INTO danga (b_id, price) VALUES ('a-2', 500);
INSERT INTO danga (b_id, price) VALUES ('b-1', 450);
INSERT INTO danga (b_id, price) VALUES ('b-2', 440);
INSERT INTO danga (b_id, price) VALUES ('c-1', 320);
INSERT INTO danga (b_id, price) VALUES ('d-1', 321);
INSERT INTO danga (b_id, price) VALUES ('e-1', 250);
INSERT INTO danga (b_id, price) VALUES ('f-1', 510);
INSERT INTO danga (b_id, price) VALUES ('f-2', 400);

COMMIT;


-- ์™œ ? ์ฑ…์˜ ๋‹จ๊ฐ€ ํ…Œ์ด๋ธ”์ด ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ์„ ๊นŒ์š”? 
  ์ž๋ฐ”์ •์„ 2000.1.1 ์ดˆํŒ   25000
  ์ž๋ฐ”์ •์„ 2002.12.1 2ํŒ   30000
  :
  ์ž๋ฐ”์ •์„ 2022.12.1 10ํŒ   50000
    
  ์ถœํŒ์‚ฌ -> [ ์„œ์ (๊ณ ๊ฐ) ]-> ๊ณ ๊ฐ(์ผ๋ฐ˜ ์‚ฌ์šฉ์ž)
       ํŒ๋งค             ํŒ๋งค
 -- <3> ๊ณ ๊ฐ ํ…Œ์ด๋ธ”  

 CREATE TABLE gogaek(
      g_id       NUMBER(5) NOT NULL PRIMARY KEY 
      ,g_name   VARCHAR2(20) NOT NULL
      ,g_tel      VARCHAR2(20)
);
 
 INSERT INTO gogaek (g_id, g_name, g_tel) VALUES (1, '์šฐ๋ฆฌ์„œ์ ', '111-1111');
INSERT INTO gogaek (g_id, g_name, g_tel) VALUES (2, '๋„์‹œ์„œ์ ', '111-1111');
INSERT INTO gogaek (g_id, g_name, g_tel) VALUES (3, '์ง€๊ตฌ์„œ์ ', '333-3333');
INSERT INTO gogaek (g_id, g_name, g_tel) VALUES (4, '์„œ์šธ์„œ์ ', '444-4444');
INSERT INTO gogaek (g_id, g_name, g_tel) VALUES (5, '์ˆ˜๋„์„œ์ ', '555-5555');
INSERT INTO gogaek (g_id, g_name, g_tel) VALUES (6, '๊ฐ•๋‚จ์„œ์ ', '666-6666');
INSERT INTO gogaek (g_id, g_name, g_tel) VALUES (7, '๊ฐ•๋ถ์„œ์ ', '777-7777');

COMMIT;


 
 -- <4> ํŒ๋งค ํ…Œ์ด๋ธ” 

 CREATE TABLE panmai(
       id         NUMBER(5) NOT NULL PRIMARY KEY
      ,g_id       NUMBER(5) NOT NULL CONSTRAINT FK_PANMAI_GID
                     REFERENCES gogaek(g_id) ON DELETE CASCADE
      ,b_id       VARCHAR2(10)  NOT NULL CONSTRAINT FK_PANMAI_BID
                     REFERENCES book(b_id) ON DELETE CASCADE
      ,p_date     DATE DEFAULT SYSDATE
      ,p_su       NUMBER(5)  NOT NULL
);

INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (1, 1, 'a-1', '2000-10-10', 10);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (2, 2, 'a-1', '2000-03-04', 20);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (3, 1, 'b-1', DEFAULT, 13);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (4, 4, 'c-1', '2000-07-07', 5);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (5, 4, 'd-1', DEFAULT, 31);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (6, 6, 'f-1', DEFAULT, 21);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (7, 7, 'a-1', DEFAULT, 26);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (8, 6, 'a-1', DEFAULT, 17);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (9, 6, 'b-1', DEFAULT, 5);
INSERT INTO panmai (id, g_id, b_id, p_date, p_su) VALUES (10, 7, 'a-2', '2000-10-10', 15);

COMMIT;


 -- <5> ์ €์ž ํ…Œ์ด๋ธ”

CREATE TABLE au_book(
       id   number(5)  NOT NULL PRIMARY KEY
      ,b_id VARCHAR2(10)  NOT NULL  CONSTRAINT FK_AUBOOK_BID
            REFERENCES book(b_id) ON DELETE CASCADE
      ,name VARCHAR2(20)  NOT NULL
);
INSERT INTO au_book (id, b_id, name) VALUES (1, 'a-1', '์ €ํŒ”๊ฐœ');
INSERT INTO au_book (id, b_id, name) VALUES (2, 'b-1', '์†์˜ค๊ณต');
INSERT INTO au_book (id, b_id, name) VALUES (3, 'a-1', '์‚ฌ์˜ค์ •');
INSERT INTO au_book (id, b_id, name) VALUES (4, 'b-1', '๊น€์œ ์‹ ');
INSERT INTO au_book (id, b_id, name) VALUES (5, 'c-1', '์œ ๊ด€์ˆœ');
INSERT INTO au_book (id, b_id, name) VALUES (6, 'd-1', '๊น€ํ•˜๋Š˜');
INSERT INTO au_book (id, b_id, name) VALUES (7, 'a-1', '์‹ฌ์‹ฌํ•ด');
INSERT INTO au_book (id, b_id, name) VALUES (8, 'd-1', 'ํ—ˆ์ฒจ');
INSERT INTO au_book (id, b_id, name) VALUES (9, 'e-1', '์ดํ•œ๋‚˜');
INSERT INTO au_book (id, b_id, name) VALUES (10, 'f-1', '์ •๋ง์ž');
INSERT INTO au_book (id, b_id, name) VALUES (11, 'f-2', '์ด์˜์• ');

COMMIT;


--------------------------------------------------------------------
  ๋‹ค์ด์–ด๊ทธ๋žจ  -- ExERD


--------------------------------------------------------------------  
  
3) JOIN ์ข…๋ฅ˜
  1) EQUI JOIN  = Natural Join:  ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ถ”์ถœ
     ใ„ด ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์— ๊ด€๊ณ„๋˜๋Š” ์ปฌ๋Ÿผ๋“ค์˜ ๊ฐ’๋“ค์ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์กฐ์ธ ํ˜•ํƒœ.
     ใ„ด WHERE ์กฐ์ธ ์กฐ๊ฑด์—   =  ( ์ด์ฝœ ) ๋“ฑํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 
     ใ„ด PK     -    FK  ๊ด€๊ณ„ ์ด์šฉ
     ใ„ด USING ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ฝœ์กฐ์ธ๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถ”์ถœํ•œ๋‹ค. 
     
     ๋ฌธ์ œ) ์ฑ…ID(b_id), ์ฑ…์ œ๋ชฉ(title), ์ถœํŒ์‚ฌ(c_name), ๋‹จ๊ฐ€(price) ์ถœ๋ ฅ
     BOOK : b_id, title, c_name
     DANGA : b_id, price
     
     ๋ฐฉ๋ฒ•1 )  ๊ฐ์ฒด๋ช… ํ‘œ๊ธฐํ•˜๋Š” ๊ฒฝ์šฐ     

     SELECT book.b_id, title, c_name, price
     FROM book, danga
     WHERE  book.b_id = danga.b_id; 

-- ์กฐ์ธ ์กฐ๊ฑด์—   = ๋“ฑํ˜ธ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.   EQUI JOIN ์ด๋ผ๊ณ  ํ•œ๋‹ค.

     
     *** ๋ฐฉ๋ฒ•2 )  ๋ณ„์นญ( alias) ํ‘œ๊ธฐํ•˜๋Š” ๊ฒฝ์šฐ     

     SELECT b.b_id, title, c_name, price
     FROM book b, danga d
     WHERE  b.b_id = d.b_id;


     *** ๋ฐฉ๋ฒ•3 )  JOIN~ ON ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

     SELECT b.b_id, title, c_name, price
     FROM book b  JOIN danga d ON  b.b_id = d.b_id;


     ๋ฐฉ๋ฒ•4) USING ์ ˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

     SELECT b_id, title, c_name, price
     FROM book   JOIN danga    USING ( b_id );  -- USING ์ ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ฐ์ฒด๋ช…, ๋ณ„์นญ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.


     
     ๋ฐฉ๋ฒ• 5) NATURAL JOIN ๊ตฌ๋ฌธ ์‚ฌ์šฉ

SELECT b_id, title, c_name, price
FROM book NATURAL JOIN danga ; -- ๊ฐ์ฒด๋ช…์„ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.

EQUI JOIN


    (๋ฌธ์ œ) ์ฑ…์ฝ”๋“œ(b_id), ์ฑ…๋ช…(title), ์ˆ˜๋Ÿ‰(p_su), ๋‹จ๊ฐ€(price), ์„œ์ ๋ช…(g_name), ํŒ๋งค๊ธˆ์•ก(p_su*price) ์ถœ๋ ฅ

    
    b_id, title            : book
    b_id, price               : danga
    b_id, p_su, g_name        : panmai
    g_name                    : gogaek
   

    SELECT b.b_id, title, p_su,  price , g_name , p_su * price
    FROM book b JOIN danga d   ON b.b_id = d.b_id
                JOIN panmai p  ON b.b_id = p.b_id
                JOIN gogaek g  ON p.g_id = g.g_id;


   (๋ฌธ์ œ) ์œ„์˜ ์ฟผ๋ฆฌ๋ฅผ USING ์ ˆ ์‚ฌ์šฉํ•ด์„œ ์ˆ˜์ •

    SELECT b_id, title, p_su,  price , g_name , p_su * price
    FROM book  JOIN danga    USING(b_id)
                JOIN panmai    USING(b_id) 
                JOIN gogaek    USING(g_id) ;


   
     
  2) NON-EQUI JOIN 
     ใ„ด ๊ด€๊ณ„๋˜๋Š” ์ปฌ๋Ÿผ์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋˜๋Š” JOIN์˜ ํ˜•ํƒœ์ด๋‹ค
     ใ„ด WHERE ์กฐ์ธ์กฐ๊ฑด์ ˆ์— = X
             BETWEEN A AND B ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 
             
    (๋ฌธ์ œ) empno, ename, sal , grade         
     emp      : empno, ename, sal
     salgrade : grade           losal <=         <=  hisal
     

     SELECT empno, ename, sal , grade         
     FROM emp e JOIN salgrade s ON  sal BETWEEN losal AND hisal;


  
  3 + 5          + ์—ฐ์‚ฐ์ž :    ํ•ญ์˜ ๊ฐฏ์ˆ˜ ->   ์ดํ•ญ์—ฐ์‚ฐ์ž
                              ๊ธฐ๋Šฅ     ->    ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž( ๋ง์…ˆ์—ฐ์‚ฐ์ž )
                          
 

NON EQUI JOIN


  3) INNER JOIN 
      ใ„ด์กฐ์ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰"๋งŒ"  ๋ฐ˜ํ™˜ํ•˜๋Š” ์กฐ์ธ
      
      ํ…Œ์ŠคํŠธ)
      SELECT *
      FROM emp;
      
    ใ„ฑ) KING ์‚ฌ์›์˜ ๋ถ€์„œ๋ฒˆํ˜ธ   NULL ๋กœ ์ˆ˜์ •

    UPDATE emp
    SET   deptno = null
    WHERE empno = 7839; -- KING



    ใ„ด)  dept, emp  ์กฐ์ธํ•ด์„œ ์ถœ๋ ฅ.
    
   

SELECT d.deptno, dname, ename, job
    FROM dept d INNER JOIN emp e ON  d.deptno = e.deptno;  -- EQUI JOIN, INNER JOIN


    
    emp : KING ์‚ฌ์› ์ •๋ณด ๋ณด์ด์ง€ ์•Š๊ณ .  O
    dept : 40 ๋ถ€์„œ ์ •๋ณด ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.  O
    

INNER JOIN


  4) OUTER JOIN  *****
      ใ„ดJOIN ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ํ–‰์„ ๋ณด๊ธฐ ์œ„ํ•œ ์ถ”๊ฐ€์ ์ธ join์˜ ํ˜•ํƒœ์ด๋‹ค.
      ใ„ด '(+)' ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ

    SELECT d.deptno, dname, ename, job
    FROM dept d FULL JOIN emp e ON  d.deptno = e.deptno;

OUTER JOIN



  (๋ฌธ์ œ) ์ฑ…ID, ์ฑ…์ œ๋ชฉ, ํŒ๋งค์ˆ˜๋Ÿ‰, ๋‹จ๊ฐ€, ์„œ์ ๋ช…(๊ณ ๊ฐ), ํŒ๋งค๊ธˆ์•ก(ํŒ๋งค์ˆ˜๋Ÿ‰*๋‹จ๊ฐ€) ์ถœ๋ ฅ
  book  : b_id, title
  danga : price
  gogaek : g_name
  panami : p_su
  

  SELECT b.b_id, title, p_su, price, g_name
     , p_su * price ํŒ๋งค๊ธˆ์•ก
  FROM book b, danga d, gogaek g, panmai p
  WHERE b.b_id=d.b_id AND b.b_id = p.b_id AND p.g_id = g.g_id;
  --
  SELECT b.b_id, title, p_su, price, g_name
     , p_su * price ํŒ๋งค๊ธˆ์•ก
  FROM book b JOIN danga d  ON b.b_id=d.b_id
              JOIN panmai p ON  b.b_id = p.b_id
              JOIN gogaek g ON p.g_id = g.g_id 
  ;



   (๋ฌธ์ œ) ์ถœํŒ๋œ ์ฑ…๋“ค์ด ๊ฐ๊ฐ ์ด ๋ช‡๊ถŒ์ด ํŒ๋งค๋˜์—ˆ๋Š”์ง€ ์กฐํšŒ
    (    ์ฑ…ID, ์ฑ…์ œ๋ชฉ, ์ดํŒ๋งค๊ถŒ์ˆ˜, ๋‹จ๊ฐ€ ์ปฌ๋Ÿผ ์ถœ๋ ฅ )
    book : b_id, title
    danga : price
    panmai :p_su
    

   SELECT b_id, title , price, SUM( p_su ) ์ดํŒ๋งค๊ถŒ์ˆ˜
   FROM (    
            SELECT b.b_id, title, p_su , price
            FROM  book b JOIN  panmai p ON b.b_id = p.b_id
                         JOIN  danga  d  ON b.b_id = d.b_id
     ) t 
   GROUP BY  b_id, title, price


   
   (๋ฌธ์ œ) ๊ฐ€์žฅ ํŒ๋งค๊ถŒ์ˆ˜๊ฐ€ ๋งŽ์€ ๊ฒƒ ์กฐํšŒ -     a-1
     TOP-N ๋ฐฉ์‹
     RANK ํ•จ์ˆ˜      1/1/1
     

SELECT t.*, ROWNUM     
FROM(      
               SELECT b_id, title , price, SUM( p_su ) ์ดํŒ๋งค๊ถŒ์ˆ˜
               FROM (    
                        SELECT b.b_id, title, p_su , price
                        FROM  book b JOIN  panmai p ON b.b_id = p.b_id
                                     JOIN  danga  d  ON b.b_id = d.b_id
                 ) t 
               GROUP BY  b_id, title, price
               ORDER BY ์ดํŒ๋งค๊ถŒ์ˆ˜ DESC
   ) t
WHErE  ROWNUM = 1   ;


     

   --  ORA-00933: SQL command not properly ended
   SELECT *
   FROM ( 
           SELECT b_id, title , price, SUM( p_su ) ์ดํŒ๋งค๊ถŒ์ˆ˜
               , RANK() OVER(  ORDER BY  SUM( p_su )  DESC )  ์ˆœ์œ„
           FROM (    
                    SELECT b.b_id, title, p_su , price
                    FROM  book b JOIN  panmai p ON b.b_id = p.b_id
                                 JOIN  danga  d  ON b.b_id = d.b_id
             ) t 
           GROUP BY  b_id, title, price
   ) x
   WHERE ์ˆœ์œ„= 1;
   
   -- HAVING ์ˆœ์œ„ = 1;


   
   WITH
   SELECT 
   FROM 
   WHERE 
   GROUP BY
   HAVING
    
 1   1   a-1   00/10/10   10
2   2   a-1   00/03/04   20
3   1   b-1   22/10/12   13
4   4   c-1   00/07/07   5
5   4   d-1   22/10/12   31
6   6   f-1   22/10/12   21
7   7   a-1   22/10/12   26
8   6   a-1   22/10/12   17
9   6   b-1   22/10/12   5
10   7   a-2   00/10/10   15


  ๋ฌธ์ œ) book ํ…Œ์ด๋ธ”์—์„œ ํŒ๋งค๊ฐ€ ๋œ ์ ์ด ์—†๋Š” ์ฑ…์˜ ์ •๋ณด ์กฐํšŒ
  ๋ฌธ์ œ) book ํ…Œ์ด๋ธ”์—์„œ ํŒ๋งค๊ฐ€ ๋œ ์ ์ด ์žˆ๋Š” ์ฑ…์˜ ์ •๋ณด ์กฐํšŒ
   ( b_id, title, price  ์ปฌ๋Ÿผ ์ถœ๋ ฅ )
   
-- ์ด ์ฑ… : 9๊ถŒ
SELECT COUNT(*)   
FROM book;
-- ํŒ๋งค ํ…Œ์ด๋ธ”  : 6๊ถŒ
SELECT DISTINCT b_id
FROM panmai;
a-1
c-1
b-1
d-1
f-1
a-2

--
SELECT b.b_id, title, price
FROM book b JOIN danga  d ON b.b_id = d.b_id
WHERE b.b_id IN ( SELECT DISTINCT b_id FROM panmai  );
--
SELECT b.b_id, title, price
FROM book b JOIN danga  d ON b.b_id = d.b_id
WHERE b.b_id NOT IN ( SELECT DISTINCT b_id FROM panmai  );

 

WITH 
a AS (
    SELECT b.b_id, title, price
    FROM book b JOIN danga  d ON b.b_id = d.b_id
)
, b AS (
  SELECT DISTINCT b_id FROM panmai
)
SELECT a.b_id, title, price
FROM a JOIN b ON  a.b_id = b.b_id; -- EQUI JOIN ( INNER JOIN )



  5) SELF JOIN
  

  SELECT a.empno, a.ename, a.mgr , b.ename  -- , ์ง์†์ƒ์‚ฌename
  FROM emp a  LEFT JOIN emp b ON a.mgr = b.empno;

SELF (LEFT) JOIN


  

 [๋ฌธ์ œ9] ๊ฐ€์žฅ ๋งŽ์ด ํŒ๋งค๊ฐ€ ๋œ ์ฑ…์˜ ์ •๋ณด๋ฅผ ์กฐํšŒ( TOP-1 )
   ( ์ฑ…ID, ์ œ๋ชฉ, ๊ฐ€๊ฒฉ, ์ดํŒ๋งค๋Ÿ‰ )
   
[๋ฌธ์ œ10]  ๋งŽ์ด ํŒ๋งค๊ฐ€ ๋œ ์ฑ…์˜ ์ •๋ณด๋ฅผ ์กฐํšŒ( TOP-3 )
   ( ์ฑ…ID, ์ œ๋ชฉ, ๊ฐ€๊ฒฉ, ์ดํŒ๋งค๋Ÿ‰ ) 
  
  6) CROSS JOIN 
  7) ANTI  JOIN 
  8) SEMI  JOIN
  
  
  
  
  
  
  
  
  
  
 

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