[Day4] Oracle 4 [9/28]

 

 

-- SCOTT 0928(수) --

1. subquery 에 λŒ€ν•΄ μ„€λͺ…ν•˜μ„Έμš”
1) ν•˜λ‚˜μ˜ SQLλ¬Έμž₯의 μ ˆμ— λΆ€μ†λœ 또 λ‹€λ₯Έ SELECT λ¬Έμž₯.
2) 두 번의 질의λ₯Ό μˆ˜ν–‰ν•΄μ•Ό 얻을 수 μžˆλŠ” κ²°κ³Όλ₯Ό ν•œ 번의 질의둜 ν•΄κ²°ν•  수 μžˆλŠ” λ¬Έμž₯.
3) μ§ˆμ˜κ°€ λ―Έμ •λœ 값을 κ·Όκ±°λ‘œν•  λ•Œ μœ μš©ν•˜λ‹€.
4) ORDER BY μ ˆμ—λŠ” μ„œλΈŒμΏΌλ¦¬λ₯Ό μ‚¬μš©ν•  수 μ—†λ‹€.
( μ˜ˆμ™Έ: 인라인 λ·°μ—μ„œλŠ” μ‚¬μš©ν•  수 μžˆλ‹€. )
 
2. Inline View 에 λŒ€ν•΄ μ„€λͺ…ν•˜μ„Έμš” ?
μ„œλΈŒμΏΌλ¦¬μ˜ 일쒅.
FROM μ ˆμ— μžˆλŠ” μ„œλΈŒμΏΌλ¦¬.

3. Nested Subquery 에 λŒ€ν•΄ μ„€λͺ…ν•˜μ„Έμš” ?
WHERE μ ˆμ— μžˆλŠ” μ„œλΈŒμΏΌλ¦¬.

4. WITH 절 에 λŒ€ν•΄ μ„€λͺ…ν•˜μ„Έμš”.
DQL λ¬Έ ( select λ¬Έ )
1 WITH 절
1) μ‚¬μš©λ  μ„œλΈŒμΏΌλ¦¬ λΈ”λŸ­μ„ 미리 μ„ μ–Έν•˜μ—¬ 반볡 μ‚¬μš©.
2) ν˜•μ‹
with
쿼리이름1 AS ( μ„œλΈŒμΏΌλ¦¬ ),
쿼리이름2 AS ( μ„œλΈŒμΏΌλ¦¬ ),
쿼리이름3 AS ( μ„œλΈŒμΏΌλ¦¬ ),
:
:
3) μ„œλΈŒμΏΌλ₯Ό μ‚¬μš©ν•˜λ©΄ μ„±λŠ₯ μ €ν•˜λœλ‹€. (단점) + μ½”λ”© κ°„κ²°, 쉽닀. (μž₯점)
 
SELECT
FROM
WHeRE
GROUP BY
HAVING
ORDER BY


5. μŠ€ν‚€λ§ˆ( Schema ) λž€ ?

6. LIKE μ—°μ‚°μžμ— λŒ€ν•΄μ„œ μ„€λͺ…ν•˜μ„Έμš” ?
WHERE 쑰건절
WHERE 컬럼λͺ… LIKE 'νŒ¨ν„΄ μ™€μΌλ“œμΉ΄λ“œ % _' ESCAPE μ˜΅μ…˜
 
7. REGEXP_LIKE() ν•¨μˆ˜μ— λŒ€ν•΄μ„œ μ„€λͺ…ν•˜μ„Έμš” ?
μ •κ·œν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜λŠ” LIKE ν•¨μˆ˜
REGEXP_LIKE( 컬럼λͺ…, 'μ •κ·œν‘œν˜„μ‹ νŒ¨ν„΄', 'λͺ¨λ“œ i, c, m λ“±λ“±')

볡슡 + (μ•”κΈ°)
8. μ–΄μ œκΉŒμ§€ 배운 Oracle ν•¨μˆ˜λ₯Ό 적고 μ„€λͺ…ν•˜μ„Έμš” .
γ„±. NVL2(), NVL() comm null μ—°μ‚° => null
γ„΄. UPPER(), LOWER(), INITCAP()-첫문자만 λŒ€λ¬Έμž          UPPER('hr')
γ„·.
SUBSTR() - μ•žμ— 87을 κ°€μ Έμ˜€κΈ° μœ„ν•΄/ 문자둜 κ°€μ Έμ˜΄ (0,1) ,
TO_CHAR('λ‚ μ§œ', 숫자, '문자', ) - 문자둜 κ°€μ Έμ˜΄,
EXTRACT 81 μž…μ‚¬ 81/02/12 - 숫자둜 κ°€μ Έμ˜΄
* μ™Όμͺ½μ •λ ¬ 문자 / 였λ₯Έμͺ½μ •λ ¬ 숫자
 
γ„Ή. REGEXP_LIKE()
ㅁ. || CONCAT() // λ¬Έμžμ—΄ μ—°κ²°
λ“±λ“±
 
9. insa ν…Œμ΄λΈ”μ—μ„œ 사원듀이 μ†ν•œ λΆ€μ„œλͺ…을 μ€‘λ³΅λ˜μ§€ μ•Šκ²Œ μ•„λž˜μ™€ 같이
μ •λ ¬ν•΄μ„œ 좜λ ₯ν•˜μ„Έμš”.
[좜λ ₯κ²°κ³Ό]
BUSEO
---------------
κ°œλ°œλΆ€
κΈ°νšλΆ€
μ˜μ—…λΆ€
인사뢀
μžμž¬λΆ€
총무뢀
홍보뢀
 
SELECT DISTINCT buseo
FROM insa
ORDER BY buseo ASC; (주의!)
 
힌트) 81λ…„ μž…μ‚¬ν•œ 사원 정보 쑰회.
10. insa ν…Œμ΄λΈ”μ—μ„œ 70λ…„λŒ€μƒ λ‚¨μžμ‚¬μ›λ§Œ μ•„λž˜μ™€ 같이 μ£Όλ―Όλ“±λ‘λ²ˆν˜Έλ‘œ μ •λ ¬ν•΄μ„œ 좜λ ₯ν•˜μ„Έμš”.
[좜λ ₯κ²°κ³Ό]
NAME RRN
-------------------- --------------
문길수 721217-1******
κΉ€μΈμˆ˜ 731211-1******
κΉ€μ’…μ„œ 751010-1******
ν—ˆκ²½μš΄ 760105-1******
μ •ν•œκ΅­ 760909-1******
μ΅œμ„κ·œ 770129-1******
μ§€μž¬ν™˜ 771115-1******
홍길동 771212-1******
μ‚°λ§ˆλ£¨ 780505-1******
μž₯인철 780506-1******
λ°•λ¬Έμˆ˜ 780710-1******
μ΄μƒν—Œ 781010-1******
κΉ€μ •ν›ˆ 790304-1******
λ°•μ„Έμ—΄ 790509-1******
이기상 790604-1******
 
15개 행이 μ„ νƒλ˜μ—ˆμŠ΅λ‹ˆλ‹€.
SELECT name
-- , ssn rrn
-- , SUBSTR( ssn, 1, 1) // (ssn, 0, 1)도 κ°™μŒ, 주민번호 첫자리 κ°€μ Έμ˜€κΈ° // 이건 ν…ŒμŠ€νŠΈμš©μž„
, CONCAT( SUBSTR( ssn , 0, 8 ), '******' ) rrn 
FROM insa 
-- Oracle λ‚˜λ¨Έμ§€ κ΅¬ν•˜λŠ” μ—°μ‚°μž X, ν•¨μˆ˜ O MOD()
--WHERE SUBSTR( ssn, 1, 1) = '7' AND SUBSTR( ssn, 8, 1)  = '1' -- 1만 선택
a) WHERE SUBSTR( ssn, 1, 1) = '7' AND MOD( SUBSTR( ssn, 8, 1) , 2 ) = '1' -- 1,3,5,7,9 λ‚¨μž (2둜 λ‚˜λˆ΄μ„λ•Œ λ‚˜λ¨Έμ§€ 1)
b) WHERE ssn LIKE '7%' AND MOD( SUBSTR( ssn, 8, 1) , 2 ) = '1' -- LIKE μ—°μ‚°μž μ‚¬μš© 70λ…„λŒ€μƒμ΄λ©΄μ„œ λ‚¨μž
--WHERE ssn LIKE '7_____-1%'
WHERE REGEXP_LIKE( ssn, '^7\d{5}-[13579]\d{6}$')
ORDER BY ssn ASC;

11. insa ν…Œμ΄λΈ”μ—μ„œ 70λ…„λŒ€ 12월생 λͺ¨λ“  사원 μ•„λž˜μ™€ 같이 μ£Όλ―Όλ“±λ‘λ²ˆν˜Έλ‘œ μ •λ ¬ν•΄μ„œ 좜λ ₯ν•˜μ„Έμš”.

[좜λ ₯κ²°κ³Ό]
NAME SSN
-------------------- --------------
문길수 721217-1951357
κΉ€μΈμˆ˜ 731211-1214576
홍길동 771212-1022432
 
1) LIKE μ—°μ‚°μž
2) REGEXP_LIKE ν•¨μˆ˜
 

SELECT name , ssn
FROM insa
WHERE ssn LIKE '7_12%'
--WHERE REGEXP_LIKE( ssn, '^7\d12')
ORDER BY ssn ASC;

 
12. LIKE μ—°μ‚°μžμ˜ ESCAPE 에 λŒ€ν•΄μ„œ μ„€λͺ…ν•˜μ„Έμš”.
1) wildcard( %, _ )λ₯Ό 일반문자 처럼 μ“°κ³  싢은 κ²½μš°μ— ESCAPE μ˜΅μ…˜μ„ μ‚¬μš©ν•œλ‹€.
-- emp ν…Œμ΄λΈ”μ—μ„œ μ‚¬μ›λ²ˆν˜Έ 7369의 사원λͺ…을 S_MITH둜 μˆ˜μ •ν•˜λŠ” DML문을 μ‹€ν–‰ν•˜κ³ ,
SELECT *
FROM emp
WHERE empno = 7369;
-- DML + 컀밋, λ‘€λ°±
UPDATE emp
SET ename = 'SMITH'
WHERE empno = 7369;
COMMIT;

-- emp ν…Œμ΄λΈ”μ—μ„œ 사원λͺ…에 's_' λ¬Έμžμ—΄μ„ ν¬ν•¨ν•œ 사원 정보λ₯Ό μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±ν•˜μ„Έμš”.
-- γ„±. LIKE μ—°μ‚°μž
-- γ„΄. REGEXP_LIKE ν•¨μˆ˜

SELECT ename
FROM emp
WHERE REGEXP_LIKE(ename, 's_' , 'i');
--WHERE ename LIKE '%' || UPPER('s\_') || '%' ESCAPE '\';
--WHERE LOWER(ename) LIKE '%s_%';
WHERE ename LIKE '%%'; -- μ‹€ν–‰ O

예) dept ν…Œμ΄λΈ” 쑰회
SELECT *
FROM dept;
예) μƒˆλ‘œμš΄ λΆ€μ„œ ν•˜λ‚˜ ν…Œμ΄λΈ”μ— λ ˆμ½”λ“œ(ν–‰) μΆ”κ°€ DML - insert λ¬Έ
INSERT ν˜•μ‹)
INSERT INTO ν…Œμ΄λΈ”λͺ… [( 컬럼λͺ…,...)] VALUES ( μ»¬λŸΌκ°’, μ»¬λŸΌκ°’... );
 
INSERT INTO dept ( deptno, dname, loc ) VALUES ( 40, 'QC100%T', 'SEOUL' );
-- ORA-00001: unique constraint (SCOTT.PK_DEPT) violated
-- μœ μΌμ„± μ œμ•½μ‘°κ±΄ μœ„λ°°λ˜μ—ˆλ‹€.
-- PK_DEPT : PK 의 이름
-- deptno μ»¬λŸΌκ°’μ€ μ€‘λ³΅λ˜λ©΄ μ•ˆλœλ‹€.
-- PK μ œμ•½μ‘°κ±΄ = 1) U + NN
 
CREATE TABLE DEPT
       (DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,
	DNAME VARCHAR2(14) ,
	LOC VARCHAR2(13) ) ;
 
-- ******
-- νŒ€ν”„λ‘œμ νŠΈ : DB λͺ¨λΈλ§(섀계)
INSERT INTO dept ( deptno, dname, loc ) VALUES ( 100, 'QC100%T', 'SEOUL' );
-- ORA-01438: value larger than specified precision allowed for this column
 
INSERT INTO dept ( deptno, dname, loc ) VALUES ( 50, 'QC100%T', 'λŒ€ν•œλ―Όκ΅­_μ„œμšΈ_강남ꡬ' );
-- ORA-12899: value too large for column "SCOTT"."DEPT"."LOC" (actual: 29, maximum: 13)
-- ν•œκΈ€ ν•œ λ¬ΈμžλŠ” 3λ°”μ΄νŠΈ 처리. 27 + 2
 
 
INSERT INTO dept ( deptno, dname, loc ) VALUES ( 50, 'QC100%T', 'SEOUL' );
-- 컬럼 μˆœμ„œλŒ€λ‘œ μ»¬λŸΌκ°’μ„ ν• λ‹Ήν•  λ•ŒλŠ” 컬럼λͺ… μƒλž΅ κ°€λŠ₯
INSERT INTO dept VALUES ( 50, 'QC100%T', 'SEOUL' );
 
INSERT INTO dept ( dname, loc, deptno ) VALUES ( 'QC100%T', 'SEOUL' , 50 );
-- ORA-01722: invalid number
 
-- μ„€λͺ…) dname, loc ν•„μˆ˜ μž…λ ₯ 컬럼 X == 널 ν—ˆμš©.
INSERT INTO dept ( deptno, dname, loc ) VALUES ( 50 );
-- ORA-00947: not enough values μ»¬λŸΌκ°’μ΄ μΆ©λΆ„ν•˜μ§€ μ•Šλ‹€.   -> 
해결방법 
 
a) INSERT INTO dept ( deptno, dname, loc ) VALUES ( 50 , null, null );
b) INSERT INTO dept ( deptno ) VALUES ( 50 );
 
 
INSERT INTO dept ( deptno, dname, loc ) VALUES ( 50, 'QC100%T', 'SEOUL' );
 
COMMIT;
ROLLBACK;

SELECT *
FROM dept;
 
ν…Œμ΄λΈ” μ œμ•½μ‘°κ±΄ κ΄€λ ¨ 였λ₯˜κ°€ λ°œμƒν–ˆμ„λ•Œ!!!
-- ν…Œμ΄λΈ”μ˜ ꡬ쑰 확인
DESC dept;
 
 
 
이름                       λ„?                      μœ ν˜•
------               --------             ------------
 
DEPTNO          NOT NULL         NUMBER(2)                   // 숫자(μ •μˆ˜, μ‹€μˆ˜) 2자리 μ •μˆ˜
DNAME                                      VARCHAR2(14 CHAR)    // λ¬Έμžμ—΄
LOC                                            VARCHAR2(13 BYTE)     // λ¬Έμžμ—΄

널 ν—ˆμš©ν•œλ‹€. : μž…λ ₯ μ•ˆν•΄λ„ λœλ‹€.
νšŒμ›κ°€μž… : NOT NULL ν•„μˆ˜ μž…λ ₯ ν•­λͺ©
널 ν—ˆμš© O : μž…λ ₯ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.
2[,4] = μ‹€μˆ˜
[p]recision 정밀도 -> NUMBER()에 λ“€μ–΄κ°€λŠ”κ±°


문제) dept ν…Œμ΄λΈ”μ—μ„œ
λΆ€μ„œλͺ…에 '100%' λ¬Έμžμ—΄μ„ ν¬ν•¨ν•˜λŠ” λΆ€μ„œμ˜ 정보λ₯Ό κ²€μƒ‰ν•΄μ„œ 쑰회(좜λ ₯)ν•˜μ„Έμš”.
 
SELECT *
FROM dept
WHERE REGEXP_LIKE( dname, '100%' ); 
--WHERE dname LIKE '%100\%%' ESCAPE '\';
 
문제) dept ν…Œμ΄λΈ”μ—μ„œ λΆ€μ„œλͺ…에 '100%' λ¬Έμžμ—΄μ„ ν¬ν•¨ν•˜λŠ” λΆ€μ„œμ˜ 지역λͺ…(loc) μ»¬λŸΌκ°’ "λŒ€ν•œλ―Όκ΅­_μ„œμšΈ_강남ꡬ" μˆ˜μ •
UPDATE dept
SET loc = 'λŒ€ν•œλ―Όκ΅­_μ„œμšΈ_강남ꡬ'
WHERE REGEXP_LIKE( dname, '100%' );
-- ORA-12899: value too large for column "SCOTT"."DEPT"."LOC" (actual: 29, maximum: 13)

문제) 50 = deptno λ ˆμ½”λ“œ(ν–‰)λ₯Ό μ‚­μ œ.
DELETE FROM ν…Œμ΄λΈ”λͺ…
[WHERE 쑰건절]; μ—†μœΌλ©΄ ν…Œμ΄λΈ”μ˜ λͺ¨λ“  λ ˆμ½”λ“œλ₯Ό μ‚­μ œν•œλ‹€.
 
--DELETE FROM dept;
-- ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found
 
-- μ™œ? 10번 λΆ€μ„œλŠ” μ‚­μ œκ°€ λ˜μ§€ μ•Šμ„κΉŒμš”? : 참쑰관계이기 λ•Œλ¬Έμ—
 
parent deptno (μ°Έμ‘°) child
dept ν…Œμ΄λΈ” emp ν…Œμ΄λΈ”
λΆ€λͺ¨ν…Œμ΄λΈ”μ˜ λΆ€μ„œλ²ˆν˜Έ μžμ‹ν…Œμ΄λΈ”μ˜ λΆ€μ„œλ²ˆν˜Έ(μ™Έλž˜ν‚€,FK) μ»¬λŸΌμ—μ„œ μ°Έμ‘°

κ΄€κ³„ν˜• 데이터 λͺ¨λΈ
 
개체 - 관계(μ—°κ΄€μ„±)- 개체
λ¦΄λ ˆμ΄μ…˜
ν…Œμ΄λΈ”
λΆ€λͺ¨ν…Œμ΄λΈ” μžμ‹ν…Œμ΄λΈ”
dept emp
deptno(PK) deptno μ°Έμ‘°ν‚€, μ™Έλž˜ν‚€, FK
empno PK κ³ μœ ν‚€
 
 
DELETE FROM dept
WHERE deptno = 50;
 
COMMIT;
WHERE deptno IN ( 10, 20, 30 );
-- ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found
-- 무결성 μ œμ•½μ‘°κ±΄ μœ„λ°°λœλ‹€.
-- empν…Œμ΄λΈ”μ˜ deptno FK μ œμ•½μ‘°κ±΄ μœ„λ°° λ™μΌμ˜λ―Έ
-- 2개 ν–‰ 이(κ°€) μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.
 
WHErE deptno >= 40;
WHERE deptno = 40; -- 40, 50
 
DELETE FROM emp;
-- 14개 ν–‰ 이(κ°€) μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.
SELECT *
FROM dept;
 
ROLLBACK;
 
-------------------------------
10    ACCOUNTING   NEW YORK
20   RESEARCH        DALLAS
30   SALES               CHICAGO
40   OPERATIONS    BOSTON
 
문제) λΆ€μ„œλͺ… -> 'SALES' -> 'S%ALES' μˆ˜μ •
UPDATE dept
SET dname = 'S%ALES'
WHERE deptno = (
SELECT deptno
FROM dept
WHERE dname = 'SALES'
);
COMMIT;
 
SELECT deptno
FROM dept
WHERE dname = 'SALES';
 
문제) deptν…Œμ΄λΈ”μ—μ„œ λΆ€μ„œλͺ…에 '%' -> 제거
UPDATE dept
-- SET dname = REPLACE( dname , '%', '')
SET dname = REPLACE( dname , '%')         // μ œκ±°ν•œλ‹€λŠ”λœ»μ΄λΌ μ„Έλ²ˆμ§Έ μΈμžκ°’ μ•ˆμ€˜λ„ 됨
--[WHERE 쑰건절]
 
SELECT dname
FROM dept;
 
-- JAVA dname.replace() .replaceAll(X, Y)
-- ORACLE
 
CRUD μž‘μ—… == CREATE  READ   UPDATE  DELETE
                        insert     select   update    delete
 
 

 
13. emp ν…Œμ΄λΈ”μ—μ„œ
pay(sal+comm) 1000 이상~ 3000 μ΄ν•˜ λ°›λŠ” 30λΆ€μ„œμ›μ„ μ œμ™Έν•œ λͺ¨λ“  μ‚¬μ›λ“€λ§Œ ename을 κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•΄μ„œ μ‘°νšŒν•˜λŠ” 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”.
 
γ„±. with 절 μ‚¬μš© (
 
WITH
temp AS (
SELECT deptno, ename, sal + NVL( comm, 0 ) pay
FROM emp
WHERE deptno != 30
)
SELECT *
FROM temp
WHERE pay BETWEEN 1000 AND 3000
ORDER BY deptno ASC;
 
γ„΄. inline view μ‚¬μš©
 
SELECT *
FROM (
SELECT deptno, ename, sal + NVL( comm, 0 ) pay
FROM emp
WHERE deptno != 30
WHERE pay BETWEEN 1000 AND 3000
ORDER BY deptno ASC;
 
γ„·. 일반 쿼리 μ‚¬μš©.
SELECT deptno, ename, sal + NVL( comm, 0 ) pay
FROM emp
WHERE deptno != 30 AND ( sal + NVL( comm, 0 ) BETWEEN 1000 AND 3000 );
 

[좜λ ₯κ²°κ³Ό]
DEPTNO     ENAME        PAY
---------- ---------- ----------
10               MILLER      1300
10               CLARK        2450
20              JONES        2975
20              FORD         3000

 

14. insaν…Œμ΄λΈ”μ—μ„œ ssn μ»¬λŸΌμ„ ν†΅ν•΄μ„œ year, month, date, gender 좜λ ₯

[좜λ ₯κ²°κ³Ό]
SSN                                YEAR        MONTH       DATE     GENDER
----------                      ------         ----           -----        -----
771212-1022432             77                12               12             1
801007-1544236           80                10              07             1
770922-2312547            77                09              22            2
790304-1788896          79                03              04            1
811112-1566789              81                 11               12             1
:
60개 행이 μ„ νƒλ˜μ—ˆμŠ΅λ‹ˆλ‹€.
 
SELECT ssn
, SUBSTR( ssn, 0, 2) year
, SUBSTR( ssn, 3, 2 ) month
, SUBSTR( ssn, 5, 2 ) "DATE" -- date λ‚ μ§œ μžλ£Œν˜•(ν‚€μ›Œλ“œ) -> μ˜ˆμ•½μ–΄μ΄κΈ° λ•Œλ¬Έμ— "" λΆ™μ—¬μ£ΌκΈ°!
, SUBSTR( ssn, -7, 1 ) gender
FROM insa

15. emp ν…Œμ΄λΈ”μ—μ„œ μž…μ‚¬λ…„λ„ μ»¬λŸΌμ—μ„œ λ…„,μ›”,일 μ°Ύμ•„μ„œ 좜λ ₯
γ„±. 년도 찾을 λ•ŒλŠ” TO_CHAR() ν•¨μˆ˜ μ‚¬μš©
γ„΄. μ›” 찾을 λ•ŒλŠ” SUBSTR() ν•¨μˆ˜ μ‚¬μš©
γ„·. 일 찾을 λ•ŒλŠ” EXTRACT() ν•¨μˆ˜ μ‚¬μš©
 
DATE hiredate -> y,m,d
 
SELECT ename, hiredate
, TO_CHAR( hiredate, 'YYYY' ) year -- '1981'
, SUBSTR( hiredate, 4, 2) month -- '12'
, EXTRACT( DAY FROM hiredate ) "DATE" -- 17
FROM emp;
 
DELETE FROM emp
WHERE ename IN ('ADAMS', 'SCOTT');
COMMIT;
 
[좜λ ₯κ²°κ³Ό]
ENAME HIREDATE YEAR MONTH DATE
---------- -------- ---- -- --
SMITH 80/12/17 1980 12 17
ALLEN 81/02/20 1981 02 20
WARD 81/02/22 1981 02 22
JONES 81/04/02 1981 04 02
MARTIN 81/09/28 1981 09 28
BLAKE 81/05/01 1981 05 01
CLARK 81/06/09 1981 06 09
KING 81/11/17 1981 11 17
TURNER 81/09/08 1981 09 08
JAMES 81/12/03 1981 12 03
FORD 81/12/03 1981 12 03
MILLER 82/01/23 1982 01 23
 
12개 행이 μ„ νƒλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

16. emp ν…Œμ΄λΈ”μ—μ„œ 직속상사(mgr)κ°€ μ—†λŠ” μ‚¬μ›μ˜ 정보λ₯Ό μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±.
 
[좜λ ₯κ²°κ³Ό]
EMPNO ENAME MGR
---------- ---------- ----------
7839 KING 0
 
SELECT empno, ename, NVL(mgr, 0) MGR
FROM emp
WHERE mgr IS NULL; -- IS NULL, IS NOT NULL SQLμ—°μ‚°μž
-- WHERE mgr = null;
 
17. λͺ¨λ“  λ‘€( ROLE ) 확인
SELECT *
FROM dba_roles;

18. RESOUCE 둀의 λͺ¨λ“  κΆŒν•œ 확인
SELECT *
FROM role_sys_privs
WHERE role = 'RESOURCE';

19. scott 계정이 뢀여받은 λ‘€ 확인
SELECT *
FROM user_role_privs;

20. [YY와 RR의 차이점]에 λŒ€ν•΄μ„œ μ„€λͺ…ν•˜μ„Έμš” . ( λ‹€ ν•˜μ‹  뢄은 μ°Ύμ•„ λ³΄μ„Έμš” )
 
yyyy-MM-dd
4자리 년도
yy
2자리 년도
 
Ora_Help to_char 검색
γ€ν˜•μ‹γ€‘
TO_CHAR( date [,'fmt' [,'nls param']])  // λ‚ μ§œ(date)κ°€ μž…λ ₯되면 문자(TO_CHAR) 둜 λ°”κΏ”μ£ΌλŠ” ν•¨μˆ˜, nls param = μƒλž΅κ°€λŠ₯
 
 -- YY/MM/DD
-- RR/MM/DD NLS
-- 22/09/28 도ꡬ/ν™˜κ²½μ„€μ •/λ°μ΄ν„°λ² μ΄μŠ€/NLS - λ‚ μ§œν˜•μ‹ RR/MM/DD
SELECT SYSDATE , TO_CHAR( SYSDATE , 'CC') -- 21μ„ΈκΈ° == 2000λ…„λŒ€
FROM dual; -- [ dual ]
 
                                      
                     
  0~50   51~99
ex) '22/01/20'  '88/01/20'
RR 2022/01/20  1988/01/20
YY 2022/01/20  2088/01/20
 
μ˜€λŠ˜λ‚ μ§œ
SELECT SYSDATE
FROM dual;

 SYSDATE?

[SYSDATE ν•¨μˆ˜]
1) ν˜„μž¬ μ‹œμŠ€ν…œμ˜ λ‚ μ§œ + νƒ€μž„
2) 22/09/28 μ‹œκ°„,λΆ„,초 -> TO_CHAR()ν•¨μˆ˜
RR/MM/DD
 
java
Date d = new Date();
C c = C.getInstance();
LocalDateTime.now() of()
 

21. emp ν…Œμ΄λΈ”μ—μ„œ 사원이름에 'la' λ¬Έμžμ—΄μ„ ν¬ν•¨ν•˜λŠ” 사원 정보 좜λ ₯
γ„±. LIKE μ‚¬μš©
SELECT deptno, ename
FROM emp
WHERE LOWER( ename) LIKE '%la%';
WHERE ename LIKE '%' || UPPER('la') || '%';
 
γ„΄. REGEXP_LIKE() μ‚¬μš©
WHERE REGEXP_LIKE( ename, 'la', 'i');
 
[좜λ ₯κ²°κ³Ό]
DEPTNO ENAME
---------- ----------
30 BLAKE
10 CLARK

22. hr κ³„μ •μœΌλ‘œ 접속
employees ν…Œμ΄λΈ”μ—μ„œ first_name, last_name 이름 속에 'ev' λ¬Έμžμ—΄ ν¬ν•¨ν•˜λŠ” 사원 정보 좜λ ₯
 
[좜λ ₯κ²°κ³Ό]
FIRST_NAME LAST_NAME NAME NAME
-------------------- ------------------------- ---------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Kevin Feeney Kevin Feeney K[ev]in Feeney
Steven King Steven King St[ev]en King
Steven Markle Steven Markle St[ev]en Markle
Kevin Mourgos Kevin Mourgos K[ev]in Mourgos
 

HR

SELECT t.* , REPLACE( name, 'ev' , '<span color="red">ev</span>' )
FROM (
SELECT first_name, last_name
, first_name || ' ' || last_name name
FROM employees
) t
WHERE name LIKE '%ev%';

13ν–‰, 10μ—΄μ—μ„œ 였λ₯˜ λ°œμƒ
ORA-00923: FROM keyword not found where expected
FROM ν‚€μ›Œλ“œκ°€ ν•„μš”ν•œ μœ„μΉ˜μ— μ—†μŠ΅λ‹ˆλ‹€. -> *이 μ•„λ‹ˆλΌ t.*이라고 ν•΄μ•ΌλŒ
 
--------------------------------------------------------------------------------------------
 
 
[ dual ]
1. ν…Œμ΄λΈ”μ΄λ‹€.
2. SYS μ‚¬μš©μžκ°€ μ†Œμœ ν•˜κ³  μžˆλŠ” 였라클의 ν‘œμ€€ ν…Œμ΄λΈ”
3. 더미(dummy)ν…Œμ΄λΈ” - κ°€μ§œ, λͺ¨μ‘°μ˜
4. 였직 ν•œ ν–‰, ν•œ 컬럼
5. μ‚¬μš©μ²˜ : μΌμ‹œμ μœΌλ‘œ μ‚°μˆ μ—°μ‚°μ΄λ‚˜ λ‚ μ§œ μ—°μ‚°ν•  λ•Œ 주둜 μ‚¬μš©ν•œλ‹€.
6. μ‚¬μš©ν•  λ•Œ SYS.dual => dual 으둜 μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.
μŠ€ν‚€λ§ˆ.ν…Œμ΄λΈ”λͺ…
κ·Έ 이유 ? λͺ¨λ“  μ‚¬μš©μžκ°€ μ‚¬μš©ν•  수 μžˆλ„λ‘ PUBLIC SYNONYM(μ‹œλ…Έλ‹˜)으둜 μ„€μ •ν–ˆκΈ° λ•Œλ¬Έμ΄λ‹€.
 
DESC dual;
-> κ²°κ³Όκ°’ : DUMMY VARCHAR2(1)

SELECT * FROM dual; -> Xκ°’ (ν•œν–‰ ν•œμ»¬λŸΌ) 만 κ°€μ§€κ³ μžˆμŒ

--SYS--

SELECT *
FROM  dba_roles;

 
-- λͺ¨λ“  κΆŒν•œμ„ 가지고 μžˆλŠ” DBA 계정 --
SELECT* 
FROM arirang;
FROM scott.emp;  -- λͺ¨λ“  κΆŒν•œ κ°€μ§€κ³ μžˆλ”λΌλ„ μ ‘κ·Όν•  λ•Œ μŠ€ν‚€λ§ˆ.ν…Œμ΄λΈ”λͺ…μœΌλ‘œ 접근해야함!
FROM emp;   -- ORA-00942: table or view does not exist

--HR--

-- HR --
-- HR scott.emp ν…Œμ΄λΈ”μ— SELECT κΆŒν•œμ΄ μ—†κΈ° λ•Œλ¬Έμ—
SELECT *
FROM arirang;
FROM scott.emp; -- ORA-00942: table or view does not exist
FROM emp; -- ORA-00942: table or view does not exist


-- HR 계정 scott.emp SELECT κΆŒν•œ λΆ€μ—¬ ~ (scott κ³„μ •μ—μ„œ)

--SCOTT--

-- DCL문
GRANT SELECT ON emp TO hr;
-- Grant을(λ₯Ό) μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€.
 
 [ SYNONYM ]
- λ°μ΄ν„°λ² μ΄μŠ€ 객체에 λŒ€ν•œ μ†Œμœ κΆŒμ€ ν•΄λ‹Ή 객체λ₯Ό μƒμ„±ν•œ μ‚¬μš©μžκ°€ 가진닀.
- λ”°λΌμ„œ λ‹€λ₯Έ μ‚¬μš©μžκ°€ μ†Œμœ ν•œ 객체에 μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œλŠ” μ†Œμœ μžλ‘œλΆ€ν„° μ ‘κ·ΌκΆŒν•œμ„ λΆ€μ—¬ λ°›μ•„μ•Όν•œλ‹€. 
- λ‹€λ₯Έ μ‚¬μš©μžκ°€ μ†Œμœ ν•œ 객체 μ‘°νšŒν•  λ•Œ μ†Œμœ μžμ˜ IDλ₯Ό 객체 μ•žμ— μ μ–΄μ•Όν•œλ‹€. (ex) scott.emp)
-> μŠ€ν‚€λ§ˆ.객체λͺ… = λ²ˆκ±°λ‘œμ›€
 
 
해결법(Synonym) : μŠ€ν‚€λ§ˆ.객체λͺ… == 이름 μ •μ˜ν•˜λŠ” 것.
 
γ€ν˜•μ‹γ€‘
CREATE [PUBLIC] SYNONYM [schema.]synonymλͺ…
FOR [schema.]objectλͺ…; 

• λ””ν΄νŠΈλŠ” private μ‹œλ…Έλ‹˜μ΄λ‹€.
• PUBLIC μ‹œλ…Έλ‹˜μ€ λͺ¨λ“  μ‚¬μš©μžκ°€ μ ‘κ·Ό κ°€λŠ₯ν•œ μ‹œλ…Έλ‹˜μ„ μƒμ„±ν•œλ‹€.
• PUBLIC μ‹œλ…Έλ‹˜μ€ λͺ¨λ“  μ‚¬μš©μžκ°€ μ ‘κ·Ό κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— 생성 및 μ‚­μ œλŠ” 였직 DBA만이 ν•  수 μžˆλ‹€.
 

--SYS--

-- μ‹œμ†Œλ‹˜ μ‚­μ œ
γ€ν˜•μ‹γ€‘
	DROP [PUBLIC] SYNONYM synonymλͺ…;

DROP PUBLIC SYNONYM arirang;
-- SYNONYM ARIRANG이(κ°€) μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

 -- μ‹œλ…Έλ‹˜ 생성 쿼리 : DDL
 CREATE PUBLIC SYNONYM  arirang
  	   FOR scott.emp;
-- ORA-00955: name is already used by an existing object
 
 
SELECT *
FROM emp;             -- μ ‘μ†ν•œ μ‚¬μš©μžκ°€ scott
FROM scott.emp;    -- μŠ€ν‚€λ§ˆ.객체λͺ…(ν…Œμ΄λΈ”λͺ…)
 
 
--------------------------------------------------------------------------------------------
[ NLS ]
1. National Language Support
κ΅­κ°€ μ–Έμ–΄ 지원
2. NLS μ„€μ •μ˜ μš°μ„  μˆœμœ„
TO_CHAR( , [, nls_param ])
SESSION > CLIENT > SERVER
?
 
였라클 μ„œλ²„ ν΄λΌμ΄μ–ΈνŠΈ
미ꡭ 일본
예) ν†΅ν™”κΈ°ν˜Έ : λ‹¬λŸ¬$ <- μ—” DD/MM/YY
μ„Έμ…˜ : ν†΅ν™”κΈ°ν˜Έ \원
MM/DD/YY YY/MM/DD
--------------------------------------------------------------------------------------------
[ μ˜€λΌν΄μ—μ„œ sessionμ΄λž€ ? ]
1. μ„Έμ…˜(session)
였라클 <- 접속
SQL μ‹€ν–‰
였라클 -> 접속 μ’…λ£Œ

μ„Έμ…˜ : DB μ ‘μ†ν•˜μ—¬ 일련의 SQL μ‹€ν–‰ ν›„ DB μ’…λ£Œμ‹œμ κΉŒμ§€λ₯Ό 말함
  • 넀이버 λΈ”λŸ¬κ·Έ κ³΅μœ ν•˜κΈ°
  • 넀이버 λ°΄λ“œμ— κ³΅μœ ν•˜κΈ°
  • 페이슀뢁 κ³΅μœ ν•˜κΈ°
  • μΉ΄μΉ΄μ˜€μŠ€ν† λ¦¬ κ³΅μœ ν•˜κΈ°