[Day5] Oracle 5 [9/29]
---볡μ΅---
-- SCOTT 0929(λͺ©) --
--1. μ€λ₯ λ©μμ§μ λν΄μ μ€λͺ
νμΈμ .
-- γ±. ORA-01438: value larger than specified precision allowed for this column
deptno NUMBER(2) 2μ리 μ μ
NUMBER(p[,s])
INSERT ( deptno ) VALUES ( 100 );
UPDATE
SET detpno = 100
-- γ΄. ORA-00001: unique constraint (SCOTT.PK_DEPT) violated
PK_DEPT μ μ μΌμ± μ μ½μ‘°κ±΄ μλ°°λλ€.
INSERT ( deptno ) VALUES ( 40 );
-- γ·. ORA-00923: FROM keyword not found where expected
SELECT ????,???
FROM μ μ, λ€ κ΅¬λ¬Έ μλͺ»λμλ€.
-- γΉ. ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found
FK_DEPTNO μ λ¬΄κ²°μ± μ μ½μ‘°κ±΄ μλ°°λλ€.
μμ λ μ½λ(ν) μ°Ύμλ€.
κ΄κ³ν λ°μ΄ν° λͺ¨λΈ + DBMS = RDBMS(μ€λΌν΄)
μμκ΄κ³
parent child
DEPT EMP
deptno(PK) κ³ μ ν€ empno(PK)
deptno(FK)
10,20.30,40 10(3), 20(3), 30(6) 40(X)
DELETE dept
WHERE deptno = 10; μμ λΆκ°λ₯
WHERE deptno = 40; μμ κ°λ₯
--
--2. RRκ³Ό YYμ μ°¨μ΄μ μ λν΄μ μ€λͺ
νμΈμ .
-- 2000λ
λ 2022/ 21μΈκΈ°
-- YY '2089.01.12' '2011.04.21'
-- RR '1989.01.12' '2011.04.21'
50~99 μ΄μ λ
λ 1900λ
λ
0~49 νμ¬λ
λ 200λ
λ
--3. JAVA : 5 % 2 μ½λ©μ μ€λΌν΄λ‘ λλ¨Έμ§ κ΅¬ν΄μ μΆλ ₯νλ 쿼리λ₯Ό μμ±νμΈμ.
-- μ°μ°μ X
λλ¨Έμ§ κ΅¬νλ ν¨μ 2κ°
SELECT MOD( 5,2 ) , REMAINDER( 5,2 )
μ μ FLOOR() λ°μ¬λ¦Ό ROUND()
FROM dual;
--4. dept ν
μ΄λΈμ μ¬μ©ν΄μ κ° DMLλ¬Έμ νμμ μ κ³ , 쿼리 μμ±νκ³ μ€ννμΈμ.
-- γ±. μλ‘μ΄ λΆμλ₯Ό μΆκ°(insert)νλ 쿼리 μμ± λ° νμΈ
-- 1) ( 50, 'QC', 'SEOUL' )
INSERT INTO ν
μ΄λΈλͺ
[( 컬λΌλͺ
... )] VALUES ( '컬λΌκ°', 'λ¬Έμ', ....);
-- 2) ( 60, T100%, SEOUL )
-- 3) ( 70, T100T, BUSAN )
-- γ΄. 60λ² λΆμμ λΆμλͺ
, μ§μλͺ
μ μμ
-- ( μλ λΆμλͺ
λ€μ 'X' λ¬Έμμ΄ μΆκ°ν΄μ μμ , μ§μλͺ
μ DAEGU λ‘ μμ )
INSERT INTO dept VALUES ( 50, 'SALES', 'POHANG');
SELECT *
FROM dept;
UPDATE dept
SET dname = CONCAT( dname , 'X'), loc = 'DAEGU' ||
WHERE deptno = 60;
μ§λ¬Έ)
———————————————
UPDATE dept
SET dname = 'S%ALES'
WHERE dname = 'SALES';
———————————————
μ 쿼리λ μλλμ?
-- μ¬μλ²νΈ, λΆμλ²νΈ(PK)μΌλ‘ μμ , μμ
UPDATE ??
SET email = ???
WHERE ename = 'νκΈΈλ'
*μ£Όμ : μ
λ°μ΄νΈ ν λ λΆμλ²νΈ(νλΌμ΄λ¨Έλ¦¬ν€)λ‘ ν΄μ€μΌ λ€λ₯Έ λ°μ΄ν° μ건λ리면μ μμ κ°λ₯νκΈ° λλ¬Έμ WHERE μ μ 쿼리 μ μλ κ²
-- γ·. LIKE μ°μ°μλ₯Ό μ¬μ©ν΄μ λΆμλͺ
μ '%' λ¬Έμκ° ν¬ν¨λ λΆμ μ 보 μ‘°ννλ 쿼리 μμ±
WHERE dname LIKE '%\%%' ESCAPE '\';
-- γΉ. REGEXP_LIKE() ν¨μλ₯Ό μ¬μ©ν΄μ λΆμλͺ
μ '%' λ¬Έμκ° ν¬ν¨λ λΆμ μ 보 μ‘°ννλ 쿼리 μμ±
WHERE REGEXP_LIKE( dname , '%' );
-- γ
. λΆμλ²νΈ 50, 60, 70λ² λΆμ μμ
DELETE FROM dept
WHERE deptno IN ( 50, 60, 70 ); -- μ μ§ λ³΄μ
-- PL/SQL
-- for( int i=40 ; i <= 70; i+= 10){
-- DELETE FROM dept WHERE deptno >= i;
-- }
--
--
--5. νκΈ ν λ¬Έμμ μνλ²³ ν λ¬Έμκ° λͺ λ°μ΄νΈμΈμ§ μΆλ ₯νλ 쿼리 μμ±( X )
VSIZE()
--
--6. insa ν
μ΄λΈμμ λ¨μλ 'X', μ¬μλ 'O' λ‘ μ±λ³(gender) μΆλ ₯νλ 쿼리 μμ±
--
-- NAME SSN GENDER
-- --------- -------------- ------
-- νκΈΈλ 771212 -1022432 X
-- μ΄μμ 801007 -1544236 X
-- μ΄μμ 770922 -2312547 O
-- κΉμ ν 790304 -1788896 X
-- νμλ΄ 811112 -1566789 X
-- μ’μ μ½λ©μ μλμ§λ§ μ΄λ κ²λ κ°λ₯ --
SELECT name, ssn
, 'X' gender
FROM insa
WHERE MOD( SUBSTR( ssn, -7, 1), 2 ) = 1
UNION - SET μ°μ°μ
SELECT name, ssn
, 'O' gender
FROM insa
WHERE MOD( SUBSTR( ssn, -7, 1), 2 ) = 0;
-- 2λ²μ§Έ --
SELECT t.name, t.ssn
, REPLACE( REPLACE( t.gender , '1', 'X' ) , '0', 'O') gender
-- if( t.gender == 1 ) 'X' νλ‘κ·Έλ¨ μΈμ΄μ μ μ°¨μ μΈ λ¬Έλ²( PL ) + SQL PL/SQL
-- else 'O'
FROM (
-- μΈλΌμΈ λ·°( Inline View )
SELECT name, ssn
, MOD(SUBSTR( ssn, -7, 1 ),2) gender
FROm insa
) t;
-- 3λ²μ§Έ --
SELECT name, ssn
-- , MOD( SUBSTR( ssn, -7, 1), 2 ) gender
, NVL2( NULLIF( MOD( SUBSTR( ssn, -7, 1), 2 ), 1 ) , 'O', 'X') gender
FROM insa;
-- NULL μ²λ¦¬ ν¨μ ? NVL, NVL2, *** NULLIF() ***
NULLIF( A, B)
A=B NULL
A!=B A
ex)
SELECT ename
, NULLIF( ename , 'SMITH' ) // λΉκ΅νλ κ° (enameκ³Ό SMITH)μ΄ κ°μΌλ©΄ NULL λ€λ₯΄λ©΄ ename
FROM emp;
μ λ ¬
-- μΌμͺ½ μ λ ¬ : λ¬Έμ
-- μ€λ₯Έμͺ½ μ λ ¬: μ«μ
SELECT '1', 1
FROM dual;
--7. insa ν
μ΄λΈμμ 2000λ
μ΄ν μ
μ¬μ μ 보 μ‘°ννλ 쿼리 μμ±
-- NAME IBSADATE
-- -------------------- --------
-- μ΄λ―Έμ± 00/04/07
-- μ¬μ¬ν΄ 00/05/05
-- κΆμλ―Έ 00/06/04
-- μ κ΄μ 00/07/07
-- λ¬Έμ --
SELECT name, ibsadate(λ¬Έμμ΄μ)
-- , TO_CHAR(λ μ§,μ«μ,λ¬Έμ) -> νμμ λ¬Έμ
-- , TO_CHAR( ibsadate, 'YYYY') ib_year
FROM insa
WHERE TO_CHAR( ibsadate, 'YYYY') >= '2000'; // λ¬Έμμ΄ λΉκ΅
ORDER BY ibsadate ASC;
-- μ«μλ‘ λΉκ΅νλ€λ©΄?--
SELECT name, ibsadate
-- , EXTRACT( YEAR FROM ibsadate) ib_year
FROM insa
WHERE EXTRACT( YEAR FROM ibsadate) >= 2000;
ORDER BY ibsadate ASC;
--8-1. Oracleμ λ μ§λ₯Ό λνλ΄λ μλ£ν 2κ°μ§λ₯Ό μ μΌμΈμ.
-- γ±. DATE
-- γ΄. TIMESTAMP
--8-2. νμ¬ μμ€ν
μ λ μ§/μκ° μ 보λ₯Ό μΆλ ₯νλ 쿼리λ₯Ό μμ±νμΈμ.
-- SYSDATE ν¨μ : λ μ§ + μκ° , '22/09/29'
SELECT SYSDATE "(γ±)" --νμ¬ μμ€ν
μ λ μ§ + μκ°(μκ°,λΆ,μ΄)
, CURRENT_DATE "(γ΄)" --νμ¬ μΈμ
(session)μ λ μ§ + μκ° (μΈμ
: λ‘κ·ΈμΈνκ³ λμ΄μ§λκΉμ§)
, CURRENT_TIMESTAMP "(γ·)" -- + ms ns
FROM dual;
--
--9. HR κ³μ μΌλ‘ μ μν΄μ
-- SELECT * FROM scott.dept;
-- μμ 쿼리λ₯Ό μ€ννλ©΄ ORA-00942: table or view does not exist μ€λ₯κ° λ°μνλ€. ( κΆν X )
1) SYSλ‘ κ°μ GRANT SELECT ON emp TO HR; // scott emp ν
μ΄λΈ κΆν λΆμ¬ λ¨Όμ νκ³
-- κ·Έλμ HR κ³μ μμλ scott.dept ν
μ΄λΈμ SELECTν μ μλλ‘ κΆνλΆμ¬ λ° arirang μ΄λ μ΄λ¦μΌλ‘ μλ
Έλ( SYNONYM )μ μμ±ν΄μ
2) SYSμμ SYNONYM μμ±
-- HR κ³μ μμλ SELECT * FROM arirang 쿼리λ₯Ό μ¬μ©ν μ μλλ‘ μ€μ νκ³ ν
μ€νΈνλ λͺ¨λ 쿼리λ₯Ό μμλλ‘ μμ±νμΈμ.
dual ν
μ΄λΈ ? public μλ
Έλ( synonym )
μ€ν€λ§.κ°μ²΄λͺ
scott.emp -> λͺ¨λ μ¬μ©μ κ°λ¨νκ² μ¬μ©ν μ μλλ‘ λ³μΉ (arirang)
--10. emp ν
μ΄λΈμμ μ¬μλͺ
(ename)μ κ²μμ΄( 'e' ) λ¬Έμ μ
λ ₯ λ₯Ό ν¬ν¨ν μ¬μμ κ²μν΄μ μλμ κ°μ΄ μΆλ ₯.
--
-- ENAME SEARCH_ENAME
-- --------------------
-- ALLEN ALL[E]N
-- JONES JON[E]S
-- BLAKE BLAK[E]
-- TURNER TURN[E]R
-- JAMES JAM[E]S
-- MILLER MILL[E]R
:λ°μΈλλ³μ (μ½λ‘ λΆμ΄μλ λ³μ)
γ±. bind variable
γ΄. μΈμ
(session)μ΄ μ μ§λλ λμ μ¬μ©ν μ μλ λ³μ
- μλ λ¬Έμ -
SELECT ename
, REPLACE( ename , 'E', '[E]' ) SEARCH_ENAME
FROM emp
WHERE REGEXP_LIKE(ename , :searchWord , 'i');
WHERE ename LIKE '%E%';
- μ
λ ₯λ°κΈ° -
SELECT ename
, REPLACE( ename , UPPER(:searchWord) , '[' || UPPER(:searchWord) || ']' ) SEARCH_ENAME
FROM emp
WHERE REGEXP_LIKE(ename , :searchWord , 'i');
WHERE ename LIKE '%E%';
--
--11. UPDATE λ¬Έμ WHERE 쑰건μ μ΄ μλ κ²½μ°λ μ΄λ»κ² λλ ? λͺ¨λ λ μ½λ(ν) μμ
--11-2. DELETE λ¬Έμ WHERE 쑰건μ μ΄ μλ κ²½μ°λ μ΄λ»κ² λλ ? λͺ¨λ λ μ½λ(ν) μμ
--11-3. INSERT λ¬Έμ λν΄μ μ€λͺ
νμΈμ.
DMLλ¬Έμ ν¬ν¨λ¨ -> 컀λ°, λ‘€λ°±ν΄μΌ λ°μλλ€!
--11-4. dept ν
μ΄λΈμ λΆμλ²νΈ : 50, λΆμλͺ
: QC λ‘ μλ‘μ΄ λΆμλ₯Ό μ μ₯νμΈμ.
--
--12. DML( INSERT, UPDATE, DELETE ) λ¬Έ μ€ν ν λ°λμ 무μμ ν΄ μ£Όμ΄μΌ νλ ? 컀λ°, λ‘€λ°± (μ€μ!!!!!!!!!!!!!!!!)
--
--13. SQL μ§ν© μ°μ°μμ μ’
λ₯μ μ€λͺ
μ νμΈμ
-- UNION [ALL] , INTERSECT , MINUS
μ§ν©μ°μ°μ 쑰건
컬λΌμ λμΌ
μλ£ν λμΌ
--13-2. insa ν
μ΄λΈμμ
-- γ±. μΆμ μ§μ(city)κ° μΈμ²μΈ μ¬μμ μ 보(name,city,buseo)λ₯Ό μ‘°ννκ³
-- γ΄. λΆμ(buseo)κ° κ°λ°λΆμΈ μ¬μμ μ 보(name,city,buseo)λ₯Ό μ‘°νν΄μ
-- λ κ²°κ³Όλ¬Όμ ν©μ§ν©(UNION)μ μΆλ ₯νλ 쿼리λ₯Ό μμ±νμΈμ.
-- ( 쑰건 : SET(μ§ν©) μ°μ°μ μ¬μ© )
--14. insa ν
μ΄λΈμμ μ£Όλ―Όλ²νΈλ₯Ό μλμ κ°μ΄ '-' λ¬Έμλ₯Ό μ κ±°ν΄μ μΆλ ₯
-- [μ€νκ²°κ³Ό]
-- NAME SSN SSN_2
-- νκΈΈλ 770423-1022432 7704231022432
-- μ΄μμ 800423-1544236 8004231544236
-- μ΄μμ 770922-2312547 7709222312547
SELECT SUBSTR(ssn, 1, 6) || SUBSTR( ssn, -7 )
, REPLACE( ssn, '-' , '')
, REPLACE( ssn, '-' )
FROM insa;
1. μ°μ°μ( Operator ) - Where μ μμ μ¬μ©.
= != <> ^= > < >= <=
WHERE deptno = 10;
WHERE deptno != 10;
WHERE ename = 'SMITH';
WHERE sal >= 1000 AND sal <= 2000;
WHERE sal BETWEEN 1000 AND 2000;
WHERE hiredate >= '1981.01.1' AND hiredate <= '1981.12.31' -- 81λ
λ μ
μ¬
BETWEEN AND
IN
LIKE
IS NULL
ANY, ALL, SOME, EXISTS : WHERE μλΈμΏΌλ¦¬ X
UNION ν©μ§ν© -- 첫 쿼리μ λ 쿼리μμ μ€λ³΅λμ§ μκ² λͺ¨λ νμ μΆμΆ
UNION ALL ν©μ§ν© -- 첫 쿼리μ λ 쿼리μμ μ€λ³΅μ νμ©ν λͺ¨λ νμ μΆμΆ
INTERSECT κ΅μ§ν©
MINUS μ°¨μ§ν©
20 7369 SMITH CLERK
20 7566 JONES MANAGER ***
20 7902 FORD ANALYST
+
20 7566 JONES MANAGER ***
30 7698 BLAKE MANAGER
10 7782 CLARK MANAGER
=
10 7782 CLARK MANAGER
20 7369 SMITH CLERK
20 7566 JONES MANAGER ***
20 7902 FORD ANALYST
30 7698 BLAKE MANAGER
SELECT deptno, empno, ename, job
FROM emp
WHERE deptno = 20
SELECT empno -- empno
FROM emp
UNION
SELECT deptno, dname --dname --deptno
FROM dept;
SELECT name, city, buseo
FROM insa
WHERE city = 'μΈμ²' AND buseo = 'κ°λ°λΆ';
SELECT name, city, buseo
FROM insa
WHERE city = 'μΈμ²'
INTERSECT
SELECT name, city, buseo
FROM insa
WHERE buseo = 'κ°λ°λΆ';
SELECT 5 + 3
, 5 - 3
, 5 * 3
--, 5 / 3 -- 1.66666666666666666666666666666666666667
-- , 5 % 3 -- ORA-00911: invalid character -> λλ¨Έμ§λ MOD λ‘ ν΄μ€μΌν¨
--, MOD( 5, 3)
-- , 5 / 0 -- ORA-01476: divisor is equal to zero // 0μΌλ‘ λλ μ μμ
--, MOD( 5, 0 ) -- 5
, 5 + 'A' -- ORA-01722: invalid number
FROM dual;
FROM emp; -- emp ν κ°―μ λ§νΌ 8
FROM dept; -- dept ν κ°―μ λ§νΌ 8
μμ κ²°κ³Όκ° νμΈνλ ν μ΄λΈ
SELECT 5+3
FROM dual;
2. μ€λΌν΄ ν¨μ
1). ROUND( number ) : μ«μκ°μ [νΉμ μμΉ]μμ λ°μ¬λ¦Όνμ¬ λ¦¬ν΄νλ€
νμ)
ROUND( n [ ,m ] )
SELECT 3.141592
, ROUND( 3.141592 ) a -- m μλ΅λ κ²½μ°μλ μμμ 첫 λ²μ§Έ μ리 λ°μ¬λ¦Ό.
, ROUND( 3.141592, 0 ) b -- m =0 κ²½μ°μλ μμμ 첫 λ²μ§Έ μ리 λ°μ¬λ¦Ό.
, ROUND( 3.141592, 2 ) c -- m+1 μμΉμμ λ°μ¬λ¦Ό
, ROUND( 3.141592, -1 ) d
, ROUND( 12345 , -1 ) e -- μΌ
, ROUND( 12345 , -2 ) f -- μ
, ROUND( 12345 , -3 ) g -- λ°±
FROM dual;
> TRUNC μ FLOOR λ μ μν¨μμ μ°¨μ΄μ ?
2) TRUNC( number ) -- νΉμ μμΉμμ μ μ
νμ)
TRUNC( n [ ,m ] )
SELECT 3.141592
, TRUNC( 3.141592 ) a -- m μλ΅λ κ²½μ°μλ μμμ 첫 λ²μ§Έ μ리 μ μ
, TRUNC( 3.141592, 0 ) b -- m =0 κ²½μ°μλ μμμ 첫 λ²μ§Έ μ리 μ μ.
, TRUNC( 3.141592, 2 ) c -- m+1 μμΉμμ μ μ
, TRUNC( 3.141592, -1 ) d
, TRUNC( 12345 , -1 ) e -- μΌ
, TRUNC( 12345 , -2 ) f -- μ
, TRUNC( 12345 , -3 ) g -- λ°±
FROM dual;
3) CEIL : νΉμ μμΉX , μμ«μ 첫 λ²μ§Έ μ리μμλ§ μ¬λ¦Ό ( μ μλ₯Ό λ¦¬ν΄ )
SELECT CEIL( 3.141592 ), FLOOR( 3.59 )
FROM dual;
4) FLOOR : νΉμ μμΉX , μμ«μ 첫 λ²μ§Έ μ리μμλ§ μ μ
5) MOD
6) ABS -- Math.abs()
SELECT 100, ABS( 100 ), ABS( -100 )
FROM dual;
7) SIGN() μ«μκ°μ λΆνΈμ λ°λΌ 1 , 0 , -1 μ κ°μ 리ν΄νλ€.
νμ) SIGN( n )
n μ΄ μμμ΄λ©΄ 1
μμμ΄λ©΄ -1
0μ΄λ©΄ 0
SELECT SIGN( 100), SIGN( -100 ), SIGN( 0 )
FROM dual;
8) POWER() 2^3
9) SQRT() μ κ³±κ·Ό f
SELECT POWER(2,3) , POWER( 2, -3)
, SQRT( 4) , SQRT(2)
FROM dual;
10) SIN(), COS(), TAN(), LOG(a,b), LN() μμ°λ‘κ·Έ λ±λ±
[λ¬Έμ ]
μ΄κ²μκΈμ : 235 / ννμ΄μ§ : 10
μ΄νμ΄μ§μ ? 23νμ΄μ§ + 1νμ΄μ§( 5κ²μκΈ ) = 24νμ΄μ§
SELECT CEIL( 235/10 ) "μ΄νμ΄μ§μ" -- 23.5 νμ΄μ§
FROM dual;
-> λλ¨Έμ§κ° λͺμ΄λ 무쑰건 μ¬λ €μ€μΌν¨! (κ²μκΈ νλλΌλ λ¨μΌλ©΄ νμ΄μ§ +1)
[λ¬Έμ ] emp ν
μ΄λΈμμ κ° μ¬μλ€μ sal κ° νκ· sal λ³΄λ€ λ§μΌλ©΄ "λ§λ€", λ³΄λ€ μ μΌλ©΄ "μ λ€"λΌκ³ μΆλ ₯
empno, ename, sal, avg_sal, νκ°
λ§λ€
μ λ€
[νμ΄]
SELECT t.*
, 'νκ· λ³΄λ€ SALκ° ' || REPLACE( REPLACE( SIGN( t.sal - t.avg_sal ), -1, 'μ λ€' ) , 1 , 'λ§λ€' ) νκ° -- 0 'O' 1 'X' NULLIF
FROM (
SELECT empno, ename, sal
-- , AVG( sal )
, ( SELECT ROUND( AVG(sal) , 2 ) FROM emp ) avg_sal
FROM emp
) t ;
--
μλλ‘λ κ°λ₯!
SELECT empno, ename, sal , 'νκ· λ³΄λ€ SALκ° λ§λ€'
FROM emp
WHERE sal > ( SELECT ROUND( AVG(sal) , 2 ) FROM emp )
UNION
SELECT empno, ename, sal , 'νκ· λ³΄λ€ SALκ° μ λ€'
FROM emp
WHERE sal < ( SELECT ROUND( AVG(sal) , 2 ) FROM emp );
UNION
SELECT empno, ename, sal , 'νκ· λ³΄λ€ SALκ° κ°λ€'
FROM emp
WHERE sal = ( SELECT ROUND( AVG(sal) , 2 ) FROM emp );
------------------------------------------------------------------------------------
[ λ¬Έμ ν¨μ ]
1) UPPER(), LOWER(), INITCAP()
SELECT ename
, LOWER( ename )
, INITCAP( ename ) // 첫λ²μ§Έλ§ λλ¬Έμλ‘
, UPPER( ename )
FROM emp;
2) LENGTH() : λ¬Έμμ΄μ κΈΈμ΄λ₯Ό λ°ννλ ν¨μ
SELECT DISTINCT job , LENGTH( job )
FROM emp;
-- String name ="Abc";
-- name.length()
3) CONCAT : ||λ κ°μ μν
CONCAT( dname , 'X')
4) SUBSTR == substring()λ κ°μ μν
SUBSTR( λ¬Έμμ΄, μμμμΉ, κΈΈμ΄ )
SELECT name, ssn
, SUBSTR( ssn, 0, 6 )
, SUBSTR( ssn, -7 )
, SUBSTR( ssn, -7, 1 ) μ±λ³
, SUBSTR( ssn, 0, 2 ) λ
λ
, SUBSTR( ssn, 3, 2 ) μ
, SUBSTR( ssn, 5, 2 ) μΌ
FROM insa;
5) INSTR - λ¬Έμμ΄ μ€ μ§μ λ λ¬Έμκ°μ μμΉλ₯Ό λ°ννλ ν¨μ
-- JAVA: indexOf(), lastIndexOf() -1
νμ)
INSTR ( string, substring [, position [,occurrence] ] )
0 μ°Ύλλ¬Έμμ΄μ΄ μλ€.
μ°Ύμ μμΉ κ° 1λΆν° μμνλ€.
ex) λΆμλͺ
μ T λ¬Έμμ΄μ΄ μλ μ§ μ²΄ν¬ + μΈλ±μ€λ°ν, μμΌλ©΄ 0
SELECT dname
, INSTR( dname , 'T' )
FROM dept;
λ λ²μ§Έ Cμ μμΉλ₯Ό μ°Ύκ³ μΆλ€λ©΄...
SELECT dname
, INSTR( dname , 'O' , 2 )
FROM dept;
--
SELECT 'corporate floor'
, INSTR( 'corporate floor', 'or' ) -- 첫 λ²μ§Έ or 2
, INSTR( 'corporate floor', 'or' , 3 ) -- 3λ²μ§Έ μμΉμμ or μ°Ύλλ€ 5
, INSTR( 'corporate floor', 'or' , 3 , 2 ) -- 14
-- lastIndexOf()
, INSTR( 'corporate floor', 'or' , -3 , 2 ) -- 2
FROM dual;
-- μ νλ²νΈ ν
μ΄λΈ μμ± : TBL_TEL
-- λ²νΈ 컬λΌ
CREATE TABLE
ALTER TABLE
DROP TABLE
-- ν
μ΄λΈ μμ± νμΈ
SELECT *
FROM tabs;
INSERT INTO "SCOTT"."TBL_TEL" (NO, TEL) VALUES ('1', '02)123-4567');
INSERT INTO "SCOTT"."TBL_TEL" (NO, TEL) VALUES ('2', '054)7899-5645');
INSERT INTO "SCOTT"."TBL_TEL" (NO, TEL) VALUES ('3', '031)9874-2932');
COMMIT;
SELECT *
FROM tbl_tel;
[λ¬Έμ ] tbl_tel ν
μ΄λΈμμ μ νλ²νΈ
μ§μλ²νΈ
μ λ² μμ리
μ λ² λ€μ리
μΆλ ₯.
SELECT no, tel
, INSTR( tel, ')') ")μμΉ"
, INSTR( tel, '-') "-μμΉ"
, SUBSTR( tel, 1, INSTR( tel, ')')-1 ) "μ§μλ²νΈ"
, SUBSTR( tel, INSTR( tel, ')') + 1 , INSTR( tel, '-') - INSTR( tel, ')') -1 ) "μμ리"
, SUBSTR( tel, INSTR( tel, '-') + 1 ) "λ·μ리"
FROM tbl_tel;
[λ¬Έμ ]
1)
UPDATE tbl_tel
SET tel = REPLACE( tel, ')', '-' );
COMMIT;
2)
SELECT *
FROM tbl_tel;
3) μ§μλ²νΈ, μμ리, λ·μ리
-- ORA-00904: "T"."F_BAR": invalid identifier
SELECT t.no, t.tel
--, t.f_bar
, SUBSTR( t.tel, 1, t.f_bar-1) "μ§μλ²νΈ"
, SUBSTR( t.tel, t.f_bar+1, t."s_bar"- t.f_bar-1) "μμ리"
, SUBSTR( t.tel, t."s_bar"+1 ) "λ€μ리"
FROM (
SELECT no, tel
, INSTR( tel, '-') f_bar
, INSTR( tel, '-', -1) "s_bar"
FROM tbl_tel
) t ;
6) RPAD / LPAD
μ§μ λ κΈΈμ΄μμ λ¬Έμκ°μ μ±μ°κ³ λ¨μ 곡κ°(μ°/μ’)μ νΉμ κ°μΌλ‘ μ±μ°λ ν¨μ
γνμγ
RPAD (expr1, n [, expr2] )
μ)
SELECT ename, sal + NVL( comm, 0) pay
, LPAD( '\' || ( sal + NVL( comm, 0)), 10 , '*' ) -- Left μΌμͺ½ λ¨λ κ³³μ νΉμ κ°μΌλ‘ μ±μ°κ² λ€.
, RPAD( '\' || ( sal + NVL( comm, 0)), 10 , '*' )
FROM emp;
μ) HR
select last_name, RPAD(' ',salary/1000, '*') "Salary"
from employees
where department_id = 80
order by last_name, "Salary";
100 κΈ°μ€μΌλ‘ λ³νκ° μΆλ ₯ κ·Έλν
SELECT ename, sal
, ROUND( sal/ 100 ) "#κ°―μ"
, RPAD( ' ', ROUND( sal/ 100 ) + 1, '#' )
FROM emp;
7) ASCII( char ) μ§μ ν μ«μ λλ λ¬Έμμ ASCII μ½λκ°μ 리ν΄νλ ν¨μ
SELECT ASCII( 'A' ) -- 65
, ASCII( 'a' ) -- 65
, ASCII( 0 ) -- '0' -> 48
FROM dual;
[λ¬Έμ ] ename μ λ λ²μ§Έ λ¬Έμμ ASCII μ μΆλ ₯νλ 쿼리 μμ±νμ.
SELECT ename
, SUBSTR( ename, 2, 1 )
, ASCII(SUBSTR( ename, 2, 1 ))
FROM emp;
------------------------------------------------------------------------------------
[ λ μ§ ν¨μ ]
1) ROUND( date ) *** μ μ€λ₯Ό κΈ°μ€μΌλ‘ λ μ§λ₯Ό λ°μ¬λ¦Όνμ¬ λ¦¬ν΄νλ€.
νμ)
ROUND( date [, fmt ] )
SELECT
SYSDATE
, ROUND( SYSDATE ) -- μ μ€ X
, ROUND( SYSDATE, 'DD' ) -- μ μ€ X (μμλ κ°μ)
-- X μμΌ , ROUND( SYSDATE, 'DAY' ) -- (μμΌμ΄κΈ°λλ¬Έμ X)
, ROUND( SYSDATE, 'MONTH' ) -- [15μΌ] 29μΌ -> 10/1
, ROUND( SYSDATE, 'YEAR' ) -- [6μ] 9μ -> 23/01/01
FROM dual;
2) TRUNC( date ) *** μκ°.λΆ.μ΄,MS 00:00:00.000 μμΌμ§λ¬λ? = μκ° νμμμ΄ λ μ§λ§ μ¬μ©ν λ
SELECT CURRENT_TIMESTAMP
, TRUNC( CURRENT_TIMESTAMP )
FROM dual;
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
1) COUNT ν¨μ : ν(λ μ½λ)μ κ°―μλ₯Ό 리ν΄νλ€.
COUNT(*) : NULL κ°μ ν¬ν¨ν κ°―μλ₯Ό 리ν΄νλ€.
νμ)
COUNT( [*|DISTINCT|ALL] 컬λΌλͺ
) [ OVER μ ]
SELECT COUNT( empno ), COUNT( deptno ) , COUNT( sal ) , COUNT( comm ) -- NULL ν¬ν¨X κ°―μ
, COUNT( * )
FROM emp;
SELECT COUNT(*) "10λ² μ¬μμ"
FROM emp
WHERE deptno = 10;
2) SUM() : NULLμ μ μΈν ν©μ 리ν΄νλ€.
νμ) SUM( [DISTINCT|ALL] expr ) [OVER μ ]
emp ν μ΄λΈμμ μ΄ sal ν©
SELECT SUM( sal ) -- 24925
FROM emp;
λ¬Έμ ] emp ν
μ΄λΈμμ μ¬μμ κΈ°λ³ΈκΈ(sal)μ νκ·
SELECT COUNT(*) FROM emp; -- 12
-- ORA-00937: not a single-group group function ννλ‘μ νΈ μλ¬ μ§λ¬Έ *****
-- 볡μν ν¨μ
-- λ¨μΌν ν¨μ Single-row ν¨μ
-- SELECT SUM(sal) / ( SELECT COUNT(*) FROM emp )// μλ¨! κ·Έλ₯ λ°λ‘ COUNT(*)λΌκ³ νλ©΄λ¨
[μλ¬ μμΈ]
-- ORA-00937: not a single-group group function ννλ‘μ νΈ μλ¬ μ§λ¬Έ *****
SELECT SUM(sal) / ( SELECT COUNT(*) FROM emp )
FROM emp;
2850
2450 볡μνν¨μ(κ·Έλ£Ήν¨μ) SUM 2XXXX
5000
1500
-- ORA-00937: not a single-group group function *******
-- κ·Έλ£Ήν¨μλ μΌλ°μ»¬λΌνκ³ μ¬μ© X
SELECT SUM(sal) , deptno // κ°μ΄ λͺ»μ!
FROM emp
[λ¬Έμ ] emp ν
μ΄λΈ μ΄ μ¬μμ μΆλ ₯
SELECT COUNT(*), SUM( sal + NVL( comm, 0 ) )
FROM emp;
[λ¬Έμ ] emp ν
μ΄λΈμμ κ° λΆμλ³ μ¬μμ μΆλ ₯
SELECT *
FROM dept;
SELECT '10' , COUNT( * )
FROM emp
WHERE deptno = 10
UNION ALL
SELECT '20' ,COUNT( * )
FROM emp
WHERE deptno = 20
UNION ALL
SELECT '30' , COUNT( * )
FROM emp
WHERE deptno = 30
UNION ALL
SELECT '40' ,COUNT( * )
FROM emp
WHERE deptno = 40
UNION ALL
SELECT '' ,COUNT( * )
FROM emp ;
-- GROUP BY μ + μ΄ν©, λͺλͺ
, νκ· , μ§κ³ν¨μ
-- λΆμλ³ μ¬μμ μ‘°ν
SELECT deptno, COUNT(*)
FROM emp
GROUP BY deptno
ORDER BY deptno ASC;
3) AVG() NULLκ°μ μ μΈν νμ νκ· μ 리ν΄
γνμγ
AVG( [DISTINCT ¦ ALL] 컬λΌλͺ
)
[ [OVER] (analytic μ )]
SELECT SUM( sal ) , COUNT(*)
, SUM( sal ) / COUNT(*) avg -- νκ·
, AVG( sal )
FROM emp;
λ€λ₯Έ μ΄μ : νκ· κ°μ λΌ λ sal μ΄ nullμΌ κ²½μ° μ μλ₯Ό μ μΈνκ³ SUMμ΄ λκΈ° λλ¬Έμ COUNTμμ nλͺ
μ λΉΌμ€μΌν¨
[ λ¬Έμ ] 컀미μ
μ νκ· κ³μ° *****
-- μ ? NULL μ μΈν νκ· ,ν©,λΆμ°,νμ€νΈμ°¨ λ±λ±
SELECT SUM( comm )
, ROUND( SUM( comm ) / COUNT(*) , 2 ) -- μμμ 3λ²μ§Έ μ리 λ°μ¬λ¦Ό
, SUM( comm ) / COUNT( comm )
, AVG( comm ) comm_avg
--SELECT comm
FROM emp;
[ λ¬Έμ ] emp ν
μ΄λΈμμ κΈ°λ³ΈκΈμ΄ κ°μ₯ λ§μ΄/μ κ² λ°λ κΈμ‘?
γνμγ
MAX ([{DISTINCT ¦ ALL}] expr)
[OVER(analytic_clause)]
SELECT MAX(sal) , MIN(sal)
FROM emp;
SELECT STDDEV( sal ) -- νμ€νΈμ°¨ ?
, VARIANCE( sal ) -- λΆμ° ?
FROM emp;
[λ¬Έμ ] emp ν
μ΄λΈμμ κΈμ¬(sal)λ₯Ό κ°μ₯ λ§μ΄ λ°λ μ¬μμ μ¬μλ²νΈ,μ¬μλͺ
, λΆμλ²νΈ, sal μ‘°ν
-- μλΈμΏΌλ¦¬λ κ΄νΈ μ¬μ©.
SELECT empno, ename, deptno, sal
FROM emp
WHERE sal = ( SELECT MAX(sal) FROM emp );
-- ORA-00934: group function is not allowed here
--SELECT empno, ename, deptno, sal
--FROM emp
--WHERE sal = MAX(sal); // μλ¨~!
[λ¬Έμ ] MAX(Sal), Min(sal)
7839 KING 10 5000
7369 SMITH 20 800
SELECT empno, ename, deptno, sal
FROM emp
WHERE sal IN ( (SELECT MIN(sal) FROM emp), (SELECT MAX(sal) FROM emp) );
WHERE sal = ( SELECT MIN(sal) FROM emp ) OR sal = ( SELECT MAX(sal) FROM emp );
μλλ κ°λ₯
SELECT 'μ΅μ ', empno, ename, deptno, sal
FROM emp
WHERE sal = ( SELECT MIN(sal) FROM emp )
UNION ALL
SELECT 'μ΅κ³ ', empno, ename, deptno, sal
FROM emp
WHERE sal = ( SELECT MAX(sal) FROM emp );
μ΅κ·ΌλκΈ