[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 μ’ λ£μμ κΉμ§λ₯Ό λ§ν¨
'π¨βπ» Web Development > Oracle' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Day6] Oracle 6 - ALL, correlated subquery, λ¬Έμν¨μ, λ μ§ν¨μ, COALESCE, DECODE (0) | 2022.10.05 |
---|---|
[Day5] Oracle 5 - μ€λΌν΄ μ°μ°μ & ν¨μ (0) | 2022.09.29 |
[Day3] Oracle 3 - ν¨μ, LIKE, BETWEEN, RR/YY, UPDATE (0) | 2022.09.28 |
[Day2] Oracle 2 - SELECT, DCL(Privilege), DML, Role (0) | 2022.09.27 |
[Day1] Oracle 1 - κ°μ, κ³μ (0) | 2022.09.27 |
μ΅κ·ΌλκΈ