[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;
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' );
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
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)
);
'๐จโ๐ป Web Development > Oracle' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day11] Oracle 11 - PURGE, ์ ์ฝ์กฐ๊ฑด2, JOIN (0) | 2022.10.12 |
---|---|
[Day10] Oracle 10 - MODIFY, DROP COLUMN, Table copy, sub query(insert, update, delete), MERGE, Constraint (0) | 2022.10.12 |
[Day8] Oracle 8 - RANK, HAVING, ROLLUP, CUBE (0) | 2022.10.06 |
[Oracle/Table] ์ค๋ผํด ํ ์ด๋ธ (0) | 2022.10.06 |
[Day7] Oracle 7 - JOIN, TON-N (ROW_NUMBER), CASE (0) | 2022.10.06 |
์ต๊ทผ๋๊ธ