[Day9] Oracle 9 [10/7]

 

--1. emp ํ…Œ์ด๋ธ”์—์„œ job ๋ณ„๋กœ ์‚ฌ์›์ˆ˜ ๋ช‡ ๋ช… ์กฐํšŒ(์ถœ๋ ฅ) ํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ.
--
--      [์‹คํ–‰๊ฒฐ๊ณผ]
--       CLERK   SALESMAN  PRESIDENT    MANAGER    ANALYST
--    ---------- ----------   ----------        ----------     ----------
--         3                 4                    1                     3                   1

SELECT 
     COUNT( DECODE( job, 'CLERK', 'O') ) CLERK -- ์นด์šดํŒ…ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๊ฐ’์ด๋“  ์ƒ๊ด€ ์—†์–ด์„œ 'O'
     ,COUNT( DECODE( job, 'SALESMAN', 'O') ) SALESMAN
     ,COUNT( DECODE( job, 'PRESIDENT', 'O') ) PRESIDENT
     ,COUNT( DECODE( job, 'MANAGER', 'O') ) MANAGER
     ,COUNT( DECODE( job, 'ANALYST', 'O') ) ANALYST
FROM emp;


-- [ PIVOT/UNPIVOT ํ•จ์ˆ˜ ]
1) ์˜ค๋ผํด 11g ๋ถ€ํ„ฐ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜
2) ํ–‰๊ณผ ์—ด์„ ๋’ค์ง‘๋Š” ํ•จ์ˆ˜
3) ํ˜•์‹

SELECT * 
FROM (ํ”ผ๋ฒ— ๋Œ€์ƒ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฌธ)
PIVOT (๊ทธ๋ฃนํ•จ์ˆ˜(์ง‘๊ณ„์ปฌ๋Ÿผ) FOR ํ”ผ๋ฒ—์ปฌ๋Ÿผ IN( ๋ชฉ๋ก ํ”ผ๋ฒ—์ปฌ๋Ÿผ ๊ฐ’ AS ๋ณ„์นญ...))
[์ถœ์ฒ˜] [Oracle] ์˜ค๋ผํด PIVOT(ํ”ผ๋ฒ—)ํ•จ์ˆ˜|์ž‘์„ฑ์ž ๋„๋‹ˆ


    ใ„ฑ. ํ”ผ๋ฒ— ๋Œ€์ƒ ์ฟผ๋ฆฌ๋ฌธ

     SELECT job FROM emp;


    ใ„ด. ๊ทธ๋ฃนํ•จ์ˆ˜ COUNT( job ์ปฌ๋Ÿผ)    FOR IN( CLERK   SALESMAN  PRESIDENT    MANAGER    ANALYST )

SELECT *    
FROM ( SELECT job FROM emp ) -- ํ”ผ๋ด‡๋Œ€์ƒ 
PIVOT ( COUNT(job)  FOR job  IN ( 'CLERK', 'SALESMAN', 'PRESIDENT', 'MANAGER', 'ANALYST') );



--2. emp ํ…Œ์ด๋ธ”์—์„œ  [JOB๋ณ„๋กœ] ๊ฐ ์›”๋ณ„ ์ž…์‚ฌํ•œ ์‚ฌ์›์˜ ์ˆ˜๋ฅผ ์กฐํšŒ 
--  ใ„ฑ. COUNT(), DECODE() ์‚ฌ์šฉ

--JOB         COUNT(*)         1์›”         2์›”         3์›”         4์›”         5์›”         6์›”         7์›”         8์›”         9์›”        10์›”        11์›”        12์›”
----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
--CLERK              3          1          0          0          0          0          0          0          0          0          0          0          2
--SALESMAN           4          0          2          0          0          0          0          0          0          2          0          0          0
--PRESIDENT          1          0          0          0          0          0          0          0          0          0          0          1          0
--MANAGER            3          0          0          0          1          1          1          0          0          0          0          0          0
--ANALYST            1          0          0          0          0          0          0          0          0          0          0          0          1
--

    SELECT job  , COUNT(*)  
       , COUNT(  DECODE(  TO_CHAR( hiredate , 'MM' ) , '1', 'O'   )   )  "1์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 2, 'O'   )   )  "2์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 3, 'O'   )   )  "3์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 4, 'O'   )   )  "4์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 5, 'O'   )   )  "5์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 6, 'O'   )   )  "6์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 7, 'O'   )   )  "7์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 8, 'O'   )   )  "8์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 9, 'O'   )   )  "9์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 10, 'O'   )   )  "10์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 11, 'O'   )   )  "11์›”"
       , COUNT(  DECODE(  EXTRACT( MONTH FROM hiredate ) , 12, 'O'   )   )  "12์›”"
    FROM emp
    GROUP BY job;



--  ใ„ด. GROUP BY ์ ˆ ์‚ฌ์šฉ
--
--         ์›”        ์ธ์›์ˆ˜
------------ ----------
--         1          1
--         2          2
--         4          1
--         5          1
--         6          1
--         9          2
--        11          1
--        12          3
--
--8๊ฐœ ํ–‰์ด ์„ ํƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 

TO_CHAR & EXTRACT

SELECT TO_CHAR( hiredate, 'MM' ) ์›”,COUNT(*) ์ธ์›์ˆ˜
FROM emp
GROUP BY TO_CHAR( hiredate, 'MM' )
ORDER BY TO_CHAR( hiredate, 'MM' );
--
SELECT EXTRACT( MONTH FROM hiredate ) ์›”,COUNT(*) ์ธ์›์ˆ˜
FROM emp
GROUP BY EXTRACT( MONTH FROM hiredate )
ORDER BY EXTRACT( MONTH FROM hiredate );



 [๋ฌธ์ œ] ํ”ผ๋ด‡ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ.
 JOB               1์›”          2          3          4          5          6          7          8          9         10         11         12
--------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
CLERK              1          0          0          0          0          0          0          0          0          0          0          2
SALESMAN           0          2          0          0          0          0          0          0          2          0          0          0
PRESIDENT          0          0          0          0          0          0          0          0          0          0          1          0
MANAGER            0          0          0          1          1          1          0          0          0          0          0          0
ANALYST            0          0          0          0          0          0          0          0          0          0          0          1
--
-- ์•„๋ž˜ ์ฟผ๋ฆฌ๋ฅผ ํ”ผ๋ด‡ ๋Œ€์ƒ ์„œ๋ธŒ์ฟผ๋ฆฌ.

SELECT EXTRACT( MONTH FROM hiredate )
FROM emp;

--

SELECT *
FROM  (
    SELECT  job,  EXTRACT( MONTH FROM hiredate ) hire_month
    FROM emp
  )
PIVOT ( COUNT(hire_month)   FOR hire_month IN (1 AS "1์›”",2,3,4,5,6,7,8,9,10,11, 12) );


--3. emp ํ…Œ์ด๋ธ”์—์„œ ๊ฐ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌ ๋งŽ์ด ๋ฐ›๋Š” ์‚ฌ์› 2๋ช…์”ฉ ์ถœ๋ ฅ
--  ์‹คํ–‰๊ฒฐ๊ณผ)
--       SEQ      EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
------------ ---------- ---------- --------- ---------- -------- ---------- ---------- ----------
--         1       7839 KING       PRESIDENT            81/11/17       5000                    10
--         2       7782 CLARK      MANAGER         7839 81/06/09       2450                    10
--         1       7902 FORD       ANALYST         7566 81/12/03       3000                    20
--         2       7566 JONES      MANAGER         7839 81/04/02       2975                    20
--         1       7698 BLAKE      MANAGER         7839 81/05/01       2850                    30
--         2       7654 MARTIN     SALESMAN        7698 81/09/28       1250       1400         30
-- TOP-N, RANK ํ•จ์ˆ˜  

WITH 
 temp AS (
            SELECT EMPNO, ENAME ,     JOB ,             MGR, HIREDATE,        SAL ,      COMM,     DEPTNO
             , RANK() OVER( PARTITION BY deptno ORDER BY  sal + NVL(comm, 0)  DESC ) seq
            FROM emp
  )
SELECT *
FROM temp
WHERE seq <= 2;
SELECT seq, ename, job, mgr, hiredate, sal, comm, deptno
FROM (
           SELECT ename, job, mgr, hiredate, sal, comm, deptno
               , ROW_NUMBER() OVER( PARTITION BY deptno  ORDER BY sal + NVL(comm, 0) DESC )  seq
           FROM emp
        ) 
WHERE seq <= 2;





[๋ฌธ์ œ] emp ํ…Œ์ด๋ธ”์—์„œ 
   1๋“ฑ๊ธ‰   2๋“ฑ๊ธ‰        5๋“ฑ๊ธ‰
     2      3            1   
  ( ์กฐ๊ฑด : PIVOT ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ )      + ์กฐ์ธ  
  
  1) GROUP BY

  SELECT s.grade || '๋“ฑ๊ธ‰'  "๋“ฑ๊ธ‰"
        , COUNT(*) ์‚ฌ์›์ˆ˜
  FROM emp e, salgrade s
  --WHERE e.deptno = d.deptno   ์ด์ฝœ์กฐ์ธ
  WHERE e.sal BETWEEN s.losal AND s.hisal
  GROUP BY s.grade
  ORDER BY s.grade ASC; -- NON์ด์ฝœ์กฐ์ธ

-> ๊ฒฐ๊ณผ :


  2) COUNT, DECODE       -> PIVOT ํ•จ์ˆ˜  11g ์ถ”๊ฐ€

  SELECT COUNT( DECODE( grade , 1 , 'O' ) ) "1๋“ฑ๊ธ‰"
            ,COUNT( DECODE( grade , 2 , 'O' ) ) "2๋“ฑ๊ธ‰"
            ,COUNT( DECODE( grade , 3 , 'O' ) ) "3๋“ฑ๊ธ‰"
            ,COUNT( DECODE( grade , 4 , 'O' ) ) "4๋“ฑ๊ธ‰"
            ,COUNT( DECODE( grade , 5 , 'O' ) ) "5๋“ฑ๊ธ‰"
   FROM emp e, salgrade s
   WHERE e.sal BETWEEN s.losal AND s.hisal;

-> ๊ฒฐ๊ณผ :


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

 SELECT grade
    FROM emp e, salgrade s
    WHERE e.sal BETWEEN s.losal AND s.hisal


    ใ„ด. ํ˜•์‹

SELECT *
    FROM (
        SELECT grade
        FROM emp e, salgrade s
        WHERE e.sal BETWEEN s.losal AND s.hisal
    )
    PIVOT ( COUNT( grade ) FOR grade IN ( 1 AS "1๋“ฑ๊ธ‰", 2, 3, 4, 5  AS "5๋“ฑ๊ธ‰") );



 (๋ฌธ์ œ) emp ํ…Œ์ด๋ธ”์—์„œ ๋…„๋„๋ณ„ ์ž…์‚ฌ์‚ฌ์›์ˆ˜๋ฅผ ์กฐํšŒ
 1) COUNT, DECODE

 SELECT 
    COUNT( DECODE( TO_CHAR( hiredate, 'YYYY' ) , 1980, 'O' ) ) "1980๋…„๋„"
    , COUNT( DECODE( TO_CHAR( hiredate, 'YYYY' ) , 1981, 'O'  ) ) "1981๋…„๋„"
    , COUNT( DECODE( TO_CHAR( hiredate, 'YYYY' ) , 1982, 'O' ) ) "1982๋…„๋„"
 FROM emp

-> ๊ฒฐ๊ณผ:

 

 2) GROUP BY 

 SELECT TO_CHAR( hiredate, 'YYYY' ), COUNT(*)
 FROM emp
 GROUP BY TO_CHAR( hiredate, 'YYYY' )
 ORDER BY TO_CHAR( hiredate, 'YYYY' );

 

-> ๊ฒฐ๊ณผ : 

 


 3) PIVOT
 ํ”ผ๋ด‡๋Œ€์ƒ

 SELECT TO_CHAR( hiredate, 'YYYY' )
 FROM emp


  --

  SELECT *
  FROM (  SELECT TO_CHAR( hiredate, 'YYYY' ) y FROM emp  )
  PIVOT (  COUNT( y ) FOR y  IN ( 1980, 1981, 1982 ) );

-> ๊ฒฐ๊ณผ : 

* ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์•ˆ๋จ!! (1980, 1981, 1982)์— ๋„ฃ๋Š”๊ฒŒ ์•ˆ๋œ๋‹ค๋Š” ๋ง์ž„!

-- ํ”ผ๋ด‡ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ ( ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ์ค‘ ... )--
1. ํ…Œ์ด๋ธ” ์ƒ์„ฑ : TBL_PIVOT
2.       ์ปฌ๋Ÿผ  : no, name , jumsu     ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™
                            kor , eng, mat
-- ํ…Œ์ด๋ธ” ์„ค๊ณ„๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๋‹ค.

-- ์ •๊ทœํ™” ์ž˜๋จ. ( ์ œ 1 ์ •๊ทœํ™” ์œ„๋ฐฐ ) DB ๋ชจ๋ธ๋ง
--1    ํ™๊ธธ๋™  90
--2    ํ™๊ธธ๋™  89
--3    ํ™๊ธธ๋™  99
--
--4    ํ™๊ธธ๋™  90  89   99

CREATE TABLE TBL_PIVOT
(
    no NUMBER NOT NULL  PRIMARY KEY -- ๊ณ ์œ ํ•œํ‚ค PK
    , name VARCHAR2(20) NOT NULL
    , jumsu NUMBER(3)  -- NULL ํ—ˆ์šฉ
)


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

INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 1, '๋ฐ•์˜ˆ๋ฆฐ', 90 );  -- kor
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 2, '๋ฐ•์˜ˆ๋ฆฐ', 89 );  -- eng
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 3, '๋ฐ•์˜ˆ๋ฆฐ', 99 );  -- mat
 
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 4, '์•ˆ์‹œ์€', 56 );  -- kor
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 5, '์•ˆ์‹œ์€', 45 );  -- eng
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 6, '์•ˆ์‹œ์€', 12 );  -- mat 
 
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 7, '๊น€๋ฏผ', 99 );  -- kor
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 8, '๊น€๋ฏผ', 85 );  -- eng
INSERT INTO TBL_PIVOT ( no, name, jumsu ) VALUES ( 9, '๊น€๋ฏผ', 100 );  -- mat

COMMIT;


[TBL_PIVOT ํ…Œ์ด๋ธ”]
1   ๋ฐ•์˜ˆ๋ฆฐ   90  k   1
2   ๋ฐ•์˜ˆ๋ฆฐ   89  e   2
3   ๋ฐ•์˜ˆ๋ฆฐ   99  m   0
4   ์•ˆ์‹œ์€   56     1
5   ์•ˆ์‹œ์€   45    2
6   ์•ˆ์‹œ์€   12
7   ๊น€๋ฏผ   99
8   ๊น€๋ฏผ   85
9   ๊น€๋ฏผ   100
 
 -- ์งˆ๋ฌธ) ํ”ผ๋ด‡
๋ฒˆํ˜ธ ์ด๋ฆ„ ๊ตญ,์˜,์ˆ˜
1 ๋ฐ•์˜ˆ๋ฆฐ  90 89 99
2 ์•ˆ์‹œ์€  56 45 12
3 ๊น€๋ฏผ    99 85 100

 

-- ํ’€์ด.
  1) ํ”ผ๋ด‡๋Œ€์ƒ *** 
  IN ( ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™  )
  

  SELECT *
  FROM (
          SELECT   TRUNC( (no-1)/3 )  + 1  no
                , name
                , jumsu
                , DECODE( MOD( no, 3), 1, '๊ตญ์–ด', 2, '์˜์–ด', 0, '์ˆ˜ํ•™' ) subject -- ๊ณผ๋ชฉ
          FROM tbl_pivot
  )
  PIVOT(  MAX(jumsu)  FOR subject   IN ('๊ตญ์–ด',  '์˜์–ด',  '์ˆ˜ํ•™'))
  ORDER BY no ASC;

-> ๊ฒฐ๊ณผ : 



-- ๋‹ค๋ฅธ๋ฐฉ๋ฒ•

SELECT *
FROM (
    SELECT name, jumsu
    , ROW_NUMBER() OVER(PARTITION BY name ORDER BY no) r -- subejct
    FROM tbl_pivot
    )
PIVOT( SUM(jumsu)   FOR  r  IN ( 1 AS "๊ตญ", 2"์˜", 3"์ˆ˜"));

dbms_random ํŒจํ‚ค์ง€
-- ์ž๋ฐ” - ์ž„์˜์˜ ์ˆ˜(๋‚œ์ˆ˜)     0.0 <=   Math.radnom()   < 1.0
-- Oracle :  dbms_random ํŒจํ‚ค์ง€ == ๊ด€๋ จ ํ•จ์ˆ˜, ํ”„๋กœ์‹œ์ € ๋“ฑ๋“ฑ
--           PL/SQL  6๊ฐ€์ง€ ์ข…๋ฅ˜ : ํŒจํ‚ค์ง€(package)

SELECT 
          dbms_random.value a  -- 0.0 <=  ์‹ค์ˆ˜    < 1.0
           , dbms_random.value( 0, 100) b -- 0 <=  ์‹ค์ˆ˜  < 100
          -- (๋ฌธ์ œ)   1~45  ๋กœ๋˜ ๋ฒˆํ˜ธ
          , FLOOR(dbms_random.value( 0, 45)) +1  c --- 1<=  ์ •์ˆ˜  <=45
          
           dbms_random.string('U',5) -- ๋Œ€๋ฌธ์ž 5๊ฐœ
           , dbms_random.string('L',5) -- LOwer๋ฌธ์ž 5๊ฐœ
           , dbms_random.string('A',5) -- ๋Œ€,์†Œ๋ฌธ์ž 5๊ฐœ
            , dbms_random.string('P',5)  -- ์•Œ(๋Œ€,์†Œ) + ํŠน์ˆ˜๋ฌธ์ž
FROM dual;


-- ( ๋ฌธ์ œ ) SMS ์ธ์ฆ๋ฒˆํ˜ธ  ์ˆซ์ž 6์ž๋ฆฌ 
     -- ์„œ๋ฒ„ -> ํด๋ผ์ด์–ธํŠธ ์ „์†ก
     -- TRIM () ์•ž์œ„์˜ ๊ณต๋ฐฑ  ์ œ๊ฑฐ X  ๋ฌธ์ž ์ œ๊ฑฐ O
     -- LTRIM()  left
     -- RTRIM()  right
--     0.[134124]06960968277106437169248375591966

SELECT   SUBSTR( dbms_random.value , 3 , 6 )    SMS6์ž๋ฆฌ
       ,  LTRIM( 0.13412406960968277106437169248375591966, '0.' )
       ,  TRUNC( dbms_random.value( 100000, 1000000)) -- 100000 <= ์‹ค์ˆ˜ < 999999
       , TRUNC( dbms_random.value * 100000 )
FROM dual;



-- [์˜ค๋ผํด ์ž๋ฃŒํ˜•( data type ) ์ •๋ฆฌ]  datatype ๊ฒ€์ƒ‰ -- 

--VARCHAR2 
--NUMBER
--DATE, TIMESTAMP


1) CHAR 
  ใ„ฑ) ๊ณ ์ •๊ธธ์ด ๋ฌธ์ž ์ŠคํŠธ๋ง         <-> ๊ฐ€๋ณ€๊ธธ์ด
  ใ„ด) ['a']['b']['c'][' '][' '][][][][]  ๋‚จ์€ ๊ณต๊ฐ„์€ blank๋กœ ์ฑ„์›Œ์ง„๋‹ค.
  ใ„ท) ['ใ…']['ใ… ']['ใ…Š']'ใ…‡' ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ.
  ใ„น) DB ์„ค์ •์— ๋”ฐ๋ผ 1๋ฌธ์ž๊ฐ€ 1~4๋ฐ”์ดํŠธ. 
  ใ…) ํ˜•์‹
    CHAR[(SIZE [BYTE |  CHAR])] ๋””ํดํŠธ 1๋ฐ”์ดํŠธ        , ์ตœ๋Œ€๊ฐ’ 2000 ๋ฐ”์ดํŠธ 
  ใ……)
      CHAR(3) == CHAR( 3 BYTE )
      CHAR    == CHAR(1 BYTE ) == CHAR( 1 )
      CHAR( 3 CHAR ) -- 3๋ฌธ์ž๋ฅผ ์ €์žฅ
      
   ใ…ˆ) ํ…Œ์ŠคํŠธ

   CREATE TABLE tbl_char(
      aa CHAR   -- char(1) == char(1 byte)
      , bb CHAR(3)-- char(3 byte)
      , cc CHAR(3 CHAR)
   );

 

   DESC tbl_char;
   --
   INSERT INTO tbl_char ( aa, bb, cc ) VALUES ( 'a', 'abc', '์„ธ๊ธ€์ž' );
   
   -- ORA-12899: value too large for column "SCOTT"."TBL_CHAR"."AA" (actual: 3, maximum: 1)
   -- INSERT INTO tbl_char ( aa, bb, cc ) VALUES ( 'ํ•œ', 'abc', '์„ธ๊ธ€์ž' );
   
   -- ORA-12899: value too large for column "SCOTT"."TBL_CHAR"."BB" (actual: 9, maximum: 3)
   --INSERT INTO tbl_char ( aa, bb, cc ) VALUES ( 'a', '์„ธ๊ธ€์ž', '์„ธ๊ธ€์ž' );
                                               --   ['b'][blank][blank] ๊ณ ์ •๊ธธ์ด 3byte ์œ ์ง€
   INSERT INTO tbl_char ( aa, bb, cc ) VALUES ( 'a', 'b', '์„ธ๊ธ€์ž' );
   -- 1 ํ–‰ ์ด(๊ฐ€) ์‚ฝ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
   COMMIT;

tbl_char


2) NCHAR = U[N]ICODE + CHAR  
          'a' -> 1 ๋ฐ”์ดํŠธ
          '๊ฐ€' -> 3 ๋ฐ”์ดํŠธ
          ๋ชจ๋“  ๋ฌธ์ž -> 2๋ฐ”์ดํŠธ ์ฒ˜๋ฆฌ.

    SELECT VSIZE('a'), VSIZE('๊ฐ€')      
    FROM dual;


   ใ„ฑ) NCHAR[( SIZE )]
   ใ…ˆ) ํ…Œ์ŠคํŠธ
     

CREATE TABLE tbl_nchar(
          aa NCHAR   -- char(1)  
          , bb NCHAR(3)-- char(3 byte)
          , cc CHAR(3)
       );
    -- Table TBL_NCHAR์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

    INSERT INTO tbl_nchar ( aa, bb, cc ) VALUES ( '๊ฐ€', '์„ธ๊ธ€์ž', 'abc' );
    INSERT INTO tbl_nchar ( aa, bb, cc ) VALUES ( 'a', 'bac', 'abc' );
    --                                                 ['b'][blank][blank]  ๊ณ ์ •๊ธธ์ด , ์ตœ๋Œ€ 2000๋ฐ”์ดํŠธ
     INSERT INTO tbl_nchar ( aa, bb, cc ) VALUES ( 'a', 'b', 'abc' );

tbl_nchar


3) NVARCHAR2(size)    // ํ•œ๊ธ€ 3๋ฐ”์ดํŠธ -> 20๋ฐ”์ดํŠธ๋ฉด 6๊ธ€์ž / ์˜์–ด๋ฉด VARCHAR
   VARCHAR2(size[BYTE ¦ CHAR])  ์˜ ์‹œ๋…ธ๋‹˜์ด VARCHAR ์ž๋ฃŒํ˜•
   
    VAR + CHAR( SIZE [BYTE|CHAR])  ์ตœ๋Œ€๊ฐ’ 4000๋ฐ”์ดํŠธ
    ๊ฐ€๋ณ€๊ธธ์ด
    
    char(5) == char(5 byte)          ๊ณ ์ •๊ธธ์ด    ['a'][blank][blank][blank][blank]    
    varchar2(5) == varchar2(5 byte)  ๊ฐ€๋ณ€๊ธธ์ด    ['a']
                                     ๊ฐ€๋ณ€๊ธธ์ด    ['a']['b']

   ๊ณ ์ •๊ธธ์ด(char) / ๊ฐ€๋ณ€๊ธธ์ด( varchar2 )
   
   ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ :  ๋ชจ๋“  ์‚ฌ๋žŒ  14์ž๋ฆฌ  ( ๊ณ ์ •๊ธธ์ด ) 00000-00000 char/nchar
                 ์˜ˆ) rrn char(14)

   ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ :   ๊ฐ€๋ณ€๊ธธ์ด  varchar2 == varchar 
                           nvarchar2(20) 20๋ฌธ์ž
                           varchar2(20)   20๋ฐ”์ดํŠธ   ํ•œ๊ธ€6๊ธ€์ž ์ตœ๋Œ€
                           varchar2(20 char)

   ์ˆซ์ž,์•Œ,ํ•œ๊ธ€
char,nchar(2000) / varchar2,nvarchar2,varchar(4000) / LONG( 2GB )

 

4) LONG - ๋ฌธ์ž ๊ฐ€๋ณ€๊ธธ์ด, 2GB ์ง€์›

5)NUMBER[(p[,s])]
    p : precision  ์ „์ฒด ์ž๋ฆฟ์ˆ˜  1~38
    s : scale      ์†Œ์ˆซ์ ์ดํ•˜ ์ž๋ฆฟ์ˆ˜   -84~127   
 
    ์˜ˆ) NUMBER( p ) == NUMBER( p, 0 )
        NUMBER      == NUMBER(38, 127)
        NUMBER( p, s )
        

  CREATE TABLE tbl_number(
      name  nvarchar2(10)-- char X,nchar X,nvarchar2, varchar2, long X
      , kor number(3)   -- 3์ž๋ฆฌ ์ •์ˆ˜    -999~999
      , eng number(3)   -- ์ฒดํฌ ์ œ์•ฝ์กฐ๊ฑด 0~ 100 ์ •์ˆ˜
      , mat number(3)
      , tot number(3,0)  -- ์ •์ˆ˜
      , avg  number(5,2) -- ์‹ค์ˆ˜
   );

 

   INSERT INTO tbl_number VALUES ('ํ™๊ธธ๋™', 90, 89, 100, null , null );
   
   -- kor = 90.525 == 91
   INSERT INTO tbl_number VALUES ('๊น€๊ธธ๋™', 90.125, 80, 78, null , null );
   
   INSERT INTO tbl_number VALUES ('๋ฐ•๊ธธ๋™', 90, -80, 111, null , null );
   
   UPDATE tbl_number
   SET   tot = kor + eng + mat , avg = (kor+eng+mat)/3
   
   -- WHERE
   -- ORA-01438: value larger than specified precision allowed for this column
   --                  ํ—ˆ์šฉ๋œ ์ •๋ฐ€๋„ ๋ณด๋‹ค  ๋” ํฐ ๊ฐ’...
   UPDATE tbl_number
   SET     avg =  12345.67
   
   -- PL/SQL ๋™์ ์ฟผ๋ฆฌ...
   UPDATE tbl_number
   SET    kor = 0
   WHERE kor <0 OR kor > 100;
   
   UPDATE tbl_number
   SET    eng = 0
   WHERE eng <0 OR eng > 100;
   
   UPDATE tbl_number
   SET    mat = 0
   WHERE mat <0 OR mat > 100;
   
   COMMIT;
   SELECT *
   FROM tbl_number;



   0.01234 ->  number(4,5)  -> 0.01234

 

tbl_number



5) FLOAT[(p)] - ๋‚ด๋ถ€์  NUMBER ์ฒ˜๋Ÿผ... X

6)  DATE  
   - ๋‚ ์งœ + ์‹œ๊ฐ„ ์ •๋ณด
   - ๊ณ ์ •๊ธธ์ด  ( 7๋ฐ”์ดํŠธ )
   

  SELECT SYSDATE   -- 22/10/07
   , TO_CHAR( SYSDATE, 'YYYY.MM.DD TS' )
  FROM dual;



DECODE ํ•จ์ˆ˜ -> CASE ํ•จ์ˆ˜
  =              ~    

7) TIMESTAMP[(n)]
  - DATE ํ˜•์˜ ํ™•์žฅ๋œ ํ˜•ํƒœ
    ๋‚ ์งœ+์‹œ๊ฐ„           + ms , ํƒ€์ž„์กด ๋“ฑ๋“ฑ

  TIMESTAMP = TIMESTAMP(0~9)
              00.000000
            
            ์˜ˆ)  ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ์ผ : [ Date ], Timestamp X
8) 
INTERVAL YEAR[(n)] TO MONTH n=2  ๋…„๊ณผ ์›”์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ ์งœ๊ฐ’์˜ ๊ธฐ๊ฐ„์„ ์ €์žฅ 
INTERVAL DAY[(n1)] TO SECOND[(n2)] n1=2, n2=6  ๋‘ ๋‚ ์งœ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ์ผ, ์‹œ, ๋ถ„, ์ดˆ, ๋ฐ€๋ฆฌ์ดˆ๋กœ ์ €์žฅ 

9) 2์ง„ ๋ฐ์ดํ„ฐ   0/1
  ใ„ฑ.  ์ด๋ฏธ์ง€ ์ €์žฅ 01010101010101011010101011  -> ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ X
  ใ„ด.  ์„œ๋ฒ„ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ๋งŒ ์ €์žฅ
RAW(size)        2000 ๋ฐ”์ดํŠธ  
LONG RAW         2GB


2GB 

10) LOB ( [L]arge [OB]ject )  --์ •๋…--
: 2๊ธฐ๊ฐ€ ๋ณด๋‹ค ํฐ ๋ฐ์ดํ„ฐ (๋ฌธ์ž๋“  ์ˆซ์ž๋“ )


 DB ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋‚ด๋ถ€์— ์ €์žฅ
 B  + LOB    - Binary(2๊ธฐ๊ฐ€ ๋„˜๋Š” 2์ง„ ๋ฐ์ดํ„ฐ)
 C  + LOB     - Char ๊ณ ์ •๊ธธ์ด + 1~4 ๋ฐ”์ดํŠธ (2๊ธฐ๊ฐ€ ๋„˜๋Š” CHAR ์ž๋ฃŒํ˜•)
 NC + LOB    - NChar ๊ณ ์ •๊ธธ์ด + ์œ ๋‹ˆ์ฝ”๋“œ (2๊ธฐ๊ฐ€ ๋„˜๋Š” NCHAR ์ž๋ฃŒํ˜•)


 BFILE  (    ์™ธ๋ถ€์— ์ €์žฅ  )

 ์˜ˆ) ๊ฒŒ์‹œํŒ ๊ธ€ ๋‚ด์šฉ content ์ปฌ๋Ÿผ :  nchar  2000๋ฐ”์ดํŠธ   ๊ณ ์ •๊ธธ์ด X  (๋ƒ‰๋ฌด)
                                 nvarchar2 4000 ๋ฐ”์ดํŠธ
                                 long     2GB
                                 [ CLOB / ( NCLOB ) ]  2GB ์ด์ƒ

 - ROWNUMBER ์˜์‚ฌ์ปฌ๋Ÿผ ( TOP- N ๋ฐฉ์‹ )
 - ROWID ์˜์‚ฌ์ปฌ๋Ÿผ = ROW(ํ–‰, ๋ ˆ์ฝ”๋“œ) + ID(๊ณ ์œ ํ•œํ‚ค)
 SELECT ROWID, dept.*
 FROM dept;
 
 --------------------------------------------------------------------------
 -- ORA-00937: not a single-group group function

-- [COUNT ํ•จ์ˆ˜ ] 
 ใ€ํ˜•์‹ใ€‘
   COUNT([* ¦ DISTINCT ¦ ALL] ์ปฌ๋Ÿผ๋ช…) [ [OVER] (analytic ์ ˆ)]

 --  ์ •๋ ฌ๋œ ๋ˆ„์ ๋œ ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•˜๋”๋ผ~ 

 SELECT name, basicpay
   ,  COUNT(*) OVER( ORDER BY basicpay ASC ) -- ์งˆ์˜ํ•œ ํ–‰์˜ ๋ˆ„์ ๋œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜
 FROM insa;


  -- ๋ถ€์„œ๋ณ„๋กœ ๋ˆ„์ ๋œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜

  SELECT name, basicpay, buseo
   ,  COUNT(*) OVER( PARTITION BY buseo  ORDER BY basicpay ASC ) -- ์งˆ์˜ํ•œ ํ–‰์˜ ๋ˆ„์ ๋œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜
 FROM insa;


 
 -- [SUM]
 ใ€ํ˜•์‹ใ€‘

   SUM ([DISTINCT ¦ ALL] expr)
               [OVER (analytic_clause)]  -- ๋ˆ„์ ๋œ ํ•ฉ\


 --   ORA-00937: not a single-group group function             

 SELECT DISTINCT buseo
        --, name         
         , SUM(basicpay) OVER( ORDER BY buseo )  s
 FROM insa  
 ORDER BY s ASC;


-------------------------------------------------------------------------
*** [ DB ๋ชจ๋ธ๋ง ] + PL/SQL ***
- ํ…Œ์ด๋ธ”(table) ์ƒ์„ฑ + ์ˆ˜์ •  + ์‚ญ์ œ ๋“ฑ๋“ฑ
-- ํšŒ์›์ •๋ณด๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํ…Œ์ด๋ธ” ์ƒ์„ฑ
์ปฌ๋Ÿผ(์—ด) /  ์ปฌ๋Ÿผ๋ช… /        ์ž๋ฃŒํ˜•  / ํฌ๊ธฐ                    / * ํ•„์ˆ˜์ž…๋ ฅํ•ญ๋ชฉ
--           --
์•„์ด๋””     id              ๋ฌธ์ž 10  ๊ฐ€๋ณ€   nvarchar2(10)    NOT NULL   ๊ณ ์œ ํ‚ค(PK)
์ด๋ฆ„       name            ๋ฌธ์ž 10  ๊ฐ€๋ณ€   nvarchar2(10)    NOT NULL
๋‚˜์ด       age             ์ˆซ์ž(์ •์ˆ˜) 3     number(3)          NULL
์ „ํ™”๋ฒˆํ˜ธ   tel              ๋ฌธ์ž์—ด 13 ๊ณ ์ •  char(13)         NOT NULL
์ƒ์ผ       birth           ๋‚ ์งœ            date               NULL
๊ธฐํƒ€       etc             ๋ฌธ์ž    ๊ฐ€๋ณ€        nvarchar2(100)  NULL
--
ํ…Œ์ด๋ธ”์„ ๋งŒ๋“œ๋Š” ๊ฐ€์žฅ ๋‹จ์ˆœํ•˜๋ฉด์„œ๋„ ์ผ๋ฐ˜์ ์ธ ๋ช…๋ น ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 
ใ€ํ˜•์‹ใ€‘
    -- ์ž„์‹œ ํ…Œ์ด๋ธ” --

    CREATE [GLOBAL TEMPORARY] TABLE [schema.] table {relational_table ¦ 
                                                     object_table ¦
                                                     XML_Type_table}


ใ€relational_table์˜ ํ˜•์‹ใ€‘
   [(relational_properties) [ON COMMIT {DELETE ¦ PRESERVE} ROWS]
   [physical_properties] [table_properties];
ใ€object_table์˜ ํ˜•์‹ใ€‘
   OF [schema.]object_type [object_table_substitution]
    [(object_properties) [ON COMMIT {DELETE ¦ PRESERVE} ROWS]
    [OID_clause] [OID_index_clause] [physical_properties] [table_properties];
ใ€XMP_Type_table์˜ ํ˜•์‹ใ€‘
   OF XMLTYPE [(object_properties) [XMLTYPE XML_Type_storage] [XML_Schema_spec]
    [XML_Type_virtual_columns] [ON COMMIT {DELETE ¦ PRESERVE} ROWS]
    [OID_clause] [OID_index_clause] [physical_properities] [table_properities]

ใ€๊ฐ„๋‹จํ•œํ˜•์‹ใ€‘
    CREATE [GLOBAL TEMPORARY] TABLE [schema.] table
      ( 
        ์—ด์ด๋ฆ„  ๋ฐ์ดํ„ฐํƒ€์ž… [DEFAULT ํ‘œํ˜„์‹] [์ œ์•ฝ์กฐ๊ฑด] 
       [,์—ด์ด๋ฆ„  ๋ฐ์ดํ„ฐํƒ€์ž… [DEFAULT ํ‘œํ˜„์‹] [์ œ์•ฝ์กฐ๊ฑด] ] 
       [,...]  
      ); 

CREATE TABLE scott.tbl_member (

      id               nvarchar2(10)    NOT NULL   PRIMARY KEY
      ,  name          nvarchar2(10)    NOT NULL
      , age            number(3)                                  -- DEFAULT 0         
      , tel            char(13)         NOT NULL
      , birth          date               
      ,  etc           nvarchar2(100)              
)
--  Table SCOTT.TBL_MEMBER์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


 
1) ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ” ํ™•์ธ.

SELECT * 
FROM tabs
WHERE table_name LIKE '%MEMBER%'; 

DESC tbl_member;



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

ใ€ํ˜•์‹ใ€‘

     DROP TABLE [schema.]table [CASCADE CONSTRAINTS] [PURGE];

• ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•˜๋ฉด ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์— ๋Œ€ํ•ด ์ƒ์„ฑ๋œ ์ธ๋ฑ์Šค๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋œ๋‹ค.
• ์‚ญ์ œ๋œ ํ…Œ์ด๋ธ”๊ณผ ๊ด€๋ จ๋œ ๋ทฐ์™€ ์‹œ๋…ธ๋Š„์€ 'invaild' ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.
• ์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค๋‚˜ ์œ ์ผ ํ‚ค๋ฅผ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์—์„œ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•  ์ˆ˜ ์—†๋‹ค. 
์ด ๊ฒฝ์šฐ์—๋Š” ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ๋จผ์ € ์‚ญ์ œํ•œ ํ›„์— ์‚ญ์ œํ•˜๊ฑฐ๋‚˜, CASCADE CONSTRAINTS ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด์„ ๋™์‹œ์— ์‚ญ์ œํ•˜๋ฉด ๋œ๋‹ค.

--DROP TABLE emp;
--DROP TABLE dept;
--ORA-02449: unique/primary keys in table referenced by foreign keys

 DROP TABLE scott.tbl_member  PURGE ;    -- [PURGE]; ์ œ๊ฑฐํ•˜๋‹ค. 
 -- PURGE ๋ณต์› X , ์™„์ „ํžˆ ํ…Œ์ด๋ธ” ์‚ญ์ œ
 

 CREATE TABLE scott.tbl_member (

      id               nvarchar2(10)    NOT NULL   PRIMARY KEY
      ,  name          nvarchar2(10)    NOT NULL
      , age            number(3)                                  -- DEFAULT 0         
  --    , tel            char(13)         NOT NULL
      , birth          date               
  --    ,  etc           nvarchar2(100)              
)



 DESC tbl_member;
1. ๊ธฐ์กด tbl_memberํ…Œ์ด๋ธ”์—์„œ  ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ ์ถ”๊ฐ€. 
   ( ์—ฐ๋ฝ์ฒ˜, ๊ธฐํƒ€ ์ปฌ๋Ÿผ ์ถ”๊ฐ€ )
   ํ…Œ์ด๋ธ” ์ˆ˜์ •
CREATE TABLE
DROP TABLE
ALTER TABLE   
 ใ€ํ˜•์‹ใ€‘์ปฌ๋Ÿผ์ถ”๊ฐ€
   ALTER TABLE ํ…Œ์ด๋ธ”๋ช…
   ADD (์ปฌ๋Ÿผ๋ช… datatype [DEFAULT ๊ฐ’]
       [,์ปฌ๋Ÿผ๋ช… datatype]...);
--

INSERT INTO tbl_member VALUES ('admin','ํ™๊ธธ๋™', 20, '2022.02.01' );
COMMIT;

SELECT * 
FROM tbl_member;



-- ORA-01758: table must be empty to add mandatory (NOT NULL) column
--             NOT NULL ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ํ…Œ์ด๋ธ”์ด ๋น„์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. 
-- ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ ์ถ”๊ฐ€ ์‹คํŒจ...

ALTER TABLE  tbl_member
ADD (
       tel            char(13)       NOT NULL
     , etc           nvarchar2(100)     
    );


    
 
 
 
 
 
 
 
 

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