[Day2] Oracle 2 [9/26]

 

[볡슡]

- PL/SQL : 절차적인 μ–Έμ–΄ (였라클만)

- 계정 유무 확인

 γ„±. scott κ³„μ • μ‘΄μž¬ μœ λ¬΄ ν™•μΈν•˜λŠ” DQL μž‘μ„±
        1) κ΄€λ¦¬μžλ‘œ 둜그인 
        SELECT *
        FROM all_users;
        
        μ ‘두사 ( λ·°View )
        all_              users  : λͺ¨λ“  μ‚¬μš©μž μ •λ³΄λ₯Ό μ‘°νšŒ
        dba_              users  : λͺ¨λ“  μ‚¬μš©μž μ •λ³΄λ₯Ό μ‘°νšŒ +  λΉ„λ°€λ²ˆν˜Έ ν¬ν•¨ λ° μΆ”κ°€ μ •λ³΄
        user_              users
        
   γ„΄.  μ‘΄μž¬ν•˜λ©΄ κ³„μ • μ‚­μ œν•˜κ³ 
        μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ κ³„μ • μƒμ„±ν•˜λŠ” DDL μž‘μ„±
        
        DROP USER scott CASCADE;
        
   γ„·.  scott κ³„μ •μœΌλ‘œ 접속할 λ•Œ ν•„μš”ν•œ κΆŒν•œ λΆ€μ—¬ν•˜λŠ” DCL μž‘μ„±

      GRANT CREATE SESSION TO scott; -- κΆŒν•œ λΆ€μ—¬
      GRANT CONNECT TO scott;  -- λ‘€ λΆ€μ—¬

 

default ν…Œμ΄λΈ” μŠ€νŽ˜μ΄μŠ€λŠ” κ³„μ •λ§ˆλ‹€ λ‹€λ₯΄λ‹€! (λ³΄μ•ˆλ•Œλ¬Έμ—) 

 

SID : μ‹œμŠ€ν…œ μ‹λ³„μž / μ—¬λŸ¬ 였라클이 port 1521 μ°Ύμ•„μ˜€λ„λ‘ κ΅¬λΆ„ν•΄μ£ΌλŠ” 것 (XE)

1. λͺ¨λ“  μ‚¬μš©μž 계정 쑰회(검색) : DQL ( select )

[select λ¬Έ μ‚¬μš© ]
1) μ‘°νšŒ λŒ€μƒ( subquery, ν•˜λ‚˜ μ΄μƒμ˜ ν…Œμ΄λΈ”, λ·°(materialized view) )으둜 λΆ€ν„° 데이터 κ°€μ Έμ˜€λŠ”λ° μ‚¬μš©ν•˜λŠ” λ¬Έ.
2) μ‘°νšŒ λŒ€μƒμ˜ μ†Œμœ μž, 쑰회 λŒ€μƒμ˜ SELECT κΆŒν•œ
3) SELECT ANY TABLE μ‹œμŠ€ν…œκΆŒν•œμ„ 가지고 있으면 λͺ¨λ“  ν…Œμ΄λΈ”, λ·°λ₯Ό 쑰회 κ°€λŠ₯
4) select ν˜•μ‹)
 
γ€ν˜•μ‹γ€‘
[subquery_factoring_clause] subquery [for_update_clause];

【subquery ν˜•μ‹γ€‘
{query_block ¦
subquery {UNION [ALL] ¦ INTERSECT ¦ MINUS }... ¦ (subquery)}
[order_by_clause]

【query_block ν˜•μ‹γ€‘
SELECT [hint] [DISTINCT ¦ UNIQUE ¦ ALL] select_list
FROM {table_reference ¦ join_clause ¦ (join_clause)},...
[where_clause]
[hierarchical_query_clause]
[group_by_clause]
[HAVING condition]
[model_clause]

【subquery factoring_clauseν˜•μ‹γ€‘
WITH {query AS (subquery),...}
 
5)(μ•”κΈ°) SELECT λ¬Έ = 절+절+절+절... 7절
처리 μˆœμ„œλ₯Ό μ μœΌμ„Έμš”.
 
 
1 [WITH] μƒλž΅ κ°€λŠ₯
6 SELECT 쑰회컬럼,,,,, *
2 FROM 쑰회 λŒ€μƒ(μ„œλΈŒμΏΌλ¦¬,ν…Œμ΄λΈ”,λ·°)
3 [WHERE]
4 [GROUP BY]
5 [HAVING]
7 [ORDER BY]

5. SELECT λ¬Έ μ‚¬μš©λ˜λŠ” ν‚€μ›Œλ“œ : DISTINCT, ALL , AS
 

--[ Ora_Help ]
;
SELECT *
FROM user_users; -- ν˜„μž¬ μ ‘μ†ν•œ μ‚¬μš©μž 정보 + Access μ‚¬μš©μž 정보

FROM View;

FROM dba_users; -- SYS( DBA )
USERNAME USER_ID PASSWORD ACCOUNT_STATUS μΆ”κ°€ 컬럼 정보
SYS 0 OPEN 21/02/05 SYSTEM TEMP
SYSTEM 5 OPEN 21/02/05 SYSTEM TEMP
ANONYMOUS 35 OPEN 14/11/25 SYSAUX TEMP


FROM all_users; -- ν…Œμ΄λΈ”, λ·°

USERNAME USER_ID CREATED 컬럼λͺ…
XS$NULL 2147483638 14/05/29
SCOTT 51 22/09/26
APEX_040000 47 14/05/29
APEX_PUBLIC_USER 45 14/05/29

-- NLS λ‚ μ§œν˜•μ‹ RR YY 차이점(년도)/MM(μ›”)/DD(일)
-- SCOTT 51 22/09/23


PRIVILEGE == κΆŒν•œ
ROLE ( λ‘€, μ—­ν•  ) == PRIVILEGE GROUP κΆŒν•œμ˜ κ·Έλ£Ή(집합)
 
DATABASE SCHEMA ( μŠ€ν‚€λ§ˆ ) ==
1. SCOTT 계정을 μƒμ„±ν•˜λ©΄ μžλ™μœΌλ‘œ SCOTTμ΄λΌλŠ” μ΄λ¦„μ˜ μŠ€ν‚€λ§ˆκ°€ μƒμ„±λœλ‹€.
생성계정λͺ… == μŠ€ν‚€λ§ˆλͺ… ( 동일 )
2. μŠ€ν‚€λ§ˆλŠ” μƒμ„±λœ 계정과 κ΄€λ ¨λœ λͺ¨λ“  객체듀을 μŠ€ν‚€λ§ˆλΌν•œλ‹€.


USER 생성
λ‹€μŒκ³Ό 같은 ν˜•μ‹μœΌλ‘œ USERλ₯Ό μƒμ„±ν•œλ‹€.
γ€ν˜•μ‹γ€‘
CREATE USER μ‚¬μš©μžλͺ…
IDENTIFIED BY λΉ„λ°€λ²ˆν˜Έ λ˜λŠ” IDENTIFIED EXTERNALLY
[DEFAULT TABLESPACE ν…Œμ΄λΈ”μŠ€νŽ˜μ΄μŠ€λͺ…]
[TEMPORARY TABLESPACE ν…Œμ΄λΈ”μŠ€νŽ˜μ΄μŠ€λͺ…]
[PROFILE ν”„λ‘œνŒŒμΌλͺ…]
[QUOTA ν• λ‹ΉλŸ‰ ON ν…Œμ΄λΈ”μŠ€νŽ˜μ΄μŠ€λͺ… λ˜λŠ” UNLIMITED ON ν…Œμ΄λΈ”μŠ€νŽ˜μ΄μŠ€λͺ…]
[PASSWORD EXPIRE]
[ACCOUNT LOCK λ˜λŠ” UNLOCK];


[ **** 계정 μˆ˜μ • *** ]
λ³€κ²½ν•  수 μžˆλŠ” USER 속성
• ALTER USER 문을 μ‚¬μš©ν•˜μ—¬ user속성을 λ³€κ²½ν•  수 μžˆλ‹€.
• USER의 변경사항은 ν˜„μž¬μ˜ SESSIONμ—λŠ” 영ν–₯을 주지 μ•Šκ³  λ‹€μŒμ— μ ‘μ†ν•œ SESSIONλΆ€ν„° 영ν–₯을 λ°›λŠ”λ‹€.
• ALTER USER 문을 μ‚¬μš©ν•˜μ—¬ user속성을 λ³€κ²½ν•  수 μžˆλŠ” 속성은 λ‹€μŒκ³Ό κ°™λ‹€.
ν•­λͺ© 의미
password USER의 passwordλ₯Ό λ³€κ²½ν•  수 있음
OS 인증 USERλ₯Ό OS인증/Oracle Server 인증으둜 λ³€κ²½ν•  수 있음
DEFAULT TABLESPACE DEFAULT TABLESPACEλ₯Ό λ³€κ²½ν•  수 있음
TEMPORARY TABLESPACE TEMPORARY TABLESPACEλ₯Ό λ³€κ²½ν•  수 있음
TABLESPACEλ‹Ή QUOTA TABLESPACEλ‹Ή ν• λ‹ΉλŸ‰μ„ λ³€κ²½ν•  수 있음
λ§Œμ•½, ν• λ‹ΉλŸ‰μ„ 0으둜 ν•˜λ©΄ κΈ°μ‘΄ object에 λŒ€ν•œ μ†Œμœ λŠ” κ°€λŠ₯ν•˜μ§€λ§Œ
μ°¨ν›„ μΆ”κ°€ 할당을 받지 λͺ»ν•¨
PROFILE DBAκ°€ RESOURCEλ₯Ό λ³€κ²½ν•˜κ³  싢을 λ•Œ USER의 PROFILE을 변경함
DEFAULT ROLE USER의 업무가 바뀐 κ²½μš°μ—λŠ” 그에 ν•΄λ‹Ήν•˜λŠ” DEFAULT ROLE둜 변경함

-- SYS 접속 ( λΉ¨κ°• ν…Œλ‘λ¦¬ )
USER μ‚­μ œ
γ€ν˜•μ‹γ€‘
DROP USER μ‚¬μš©μžλͺ…
[CASCADE]; 사전적 의미 : μž‘μ€ 폭포, 폭포처럼 흐λ₯΄λ‹€. **** [Cascade]SS
ν•΄λ‹Ή κ³„μ •μ˜ μ†Œμœ ν•œ λͺ¨λ“  객체(μŠ€ν‚€λ§ˆ) 같이 μ‚­μ œ.

DROP USER scott CASCADE;
-- ORA-01940: cannot drop a user that is currently connected
-- ORA-01922: CASCADE must be specified to drop 'SCOTT'
-- User SCOTT이(κ°€) μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

• userμ‚­μ œμ‹œ ν˜„μž¬ DB에 접속쀑인 μ‚¬μš©μžλŠ” μ‚­μ œν•  수 μ—†λ‹€.
ν˜„μž¬ 접속쀑인 μ‚¬μš©μžλŠ” κ·Έ μ‚¬μš©μžμ˜ SESSION이 λλ‚˜μ•Ό ν•œλ‹€.

# sqlplus / as sysdba
SQL> DROP USER kim cascade;
User dropped.


-- SCOTT 계정 생성
CREATE USER scott IDENTIFIED BY tiger;
-- User SCOTT이(κ°€) μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μƒνƒœ: μ‹€νŒ¨ -ν…ŒμŠ€νŠΈ μ‹€νŒ¨:
ORA-01045: user SCOTT lacks CREATE SESSION privilege; logon denied

-- CREATE SESSION κΆŒν•œ λΆ€μ—¬. DCL( grant )


κΆŒν•œ( privilege )의 2가지 μ’…λ₯˜
1) μ‹œμŠ€ν…œ κΆŒν•œ : 객체 생성,μˆ˜μ •, μ‚­μ œν•˜λŠ” κΆŒν•œ
2) 객체 κΆŒν•œ : 객체의 λ‚΄μš©μ„ μΆ”κ°€,μˆ˜μ •,μ‚­μ œ 검색 κΆŒν•œ


Grant κΆŒν•œ( Create Session ) TO λͺ¨λ“  μ‚¬μš©μž 계정; λŒ€μƒ( μ‚¬μš©μž, λ‘€ )
λ‘€( Role )                                          PUBLIC

GRANT CREATE SESSION TO scott;


--0 λͺ¨λ“  μ‹œμŠ€ν…œ κΆŒν•œ λͺ©λ‘μ„ 확인(쑰회) - view
SELECT *
FROM system_privilege_map;
FROM dba_sys_privs; (dba 뢙은건 κ΄€λ¦¬μžλ§Œ μ‚¬μš©κ°€λŠ₯!)

-- λ‘€( ROLE ) μ΄λž€ ? --
1) 효과적으둜 κΆŒν•œμ„ κ΄€λ¦¬ν•˜κΈ° μœ„ν•œ 그룹을 둀이라고 ν•œλ‹€.
2) μ‚¬μš©μž λ‹€μˆ˜, κΆŒν•œ λ‹€μˆ˜ -> κΆŒν•œ λΆ€μ—¬, κΆŒν•œ 제거
 

예) 우리 νšŒμ‚¬μ— μ‹ μž…μ‚¬μ› 10λͺ… μž…μ‚¬ + κΆŒν•œ ( 20 )
계정 생성
a + grant μ‹ μž…λ‘€ to a μ˜μ—…λΆ€ μ‹ μž…λ‘€(제거) + μ˜μ—…λ‘€(λΆ€μ—¬)
b μ˜μ—…λΆ€
c 생산뢀
d 총무뢀
: + grant μ‹ μž…λ‘€ to z

:


μ‹ μž…λ‘€ = μ—­ν• ==λ‘€== Role μ—κ²Œ 20 κΆŒν•œ λΆ€μ—¬
 
μ˜μ—…λ‘€ = 30κ°œκΆŒν•œ κ·Έλ£Ή

μ‹ μž…ROLE 생성
GRANT a,b,c,d,... TOμ‹ μž…λ‘€(Role) ;
GRNAT μ‹ μž…λ‘€ TO PUBLIC;
 
 
+ GRANT CREATE SESSION TO student_role; // 둀에 κΆŒν•œ λΆ€μ—¬
+ GRANT student_role TO kim;                         // μ‚¬μš©μžμ—κ²Œ 둀을 λΆ€μ—¬
+ ALTER ROLE student_role                             // λ‘€ μˆ˜μ • 
+ REVOKE student_role FROM kim;               // μ‚¬μš©μžλ‘œλΆ€ν„° λ‘€ 회수
+ DROP ROLE student_role;                          // λ‘€ μ‚­μ œ
 
둀쑰회
 
 
였라클 μ„€μΉ˜ν•˜λ©΄ μžλ™μœΌλ‘œ 미리 μ •μ˜λœ 둀이 있음!
-> ( μ‹œν—˜ ) μ˜€λΌν΄μ„ μ„€μΉ˜ν•˜λ©΄ μžλ™μœΌλ‘œ μ„€μΉ˜λœ λ‘€ ν™•μΈν•˜λŠ” 쿼리 μž‘μ„±ν•˜μ„Έμš”. ( 32 개 )
SELECT *
FROM dba_roles; -- ν…Œμ΄λΈ”λͺ… λ˜λŠ” λ·°λͺ… λ˜λŠ” μ„œλΈŒμΏΌλ¦¬;
-- 였λ₯˜λ°œμƒ : ORA-00942: table or view does not exist
SELECT *
FROM user_users;
FROM dba_users; --dba_users 쑴재X μΌλ°˜κ³„μ •μΈ SCOTT은 dba_XXX μ‚¬μš©ν•  수 μ—†λ‹€.


[ scott.sql 파일 ]
Rem μ£Όμ„μ²˜λ¦¬
-- μ£Όμ„μ²˜λ¦¬

SET λͺ…λ Ήμ–΄ : μ‹œμŠ€ν…œμ˜ ν™˜κ²½ μ„€μ •ν•˜λŠ” λͺ…λ Ήμ–΄

SET TERMOUT OFF : μ‹€ν–‰ κ²°κ³Όλ₯Ό 화면에 좜λ ₯μ—¬λΆ€
SET ECHO OFF : μ‹€ν–‰λœ 슀크립트 ν™”λ©΄ O, X

SET TERMOUT ON
SET ECHO ON


 
GRANT CONNECT,RESOURCE,UNLIMITED TABLESPACE -- λ‘€
TO SCOTT
IDENTIFIED BY tiger;
-- DB μ—°κ²°(접속) O

-- (μ‹œν—˜) SCOTT 계정 정보 확인( 쑰회 )ν•˜λŠ” 쿼리~
SELECT *
FROM user_users;
 
-- SCOTT --
-- (μ‹œν—˜) SCOTT이 μ†Œμœ ν•œ ν…Œμ΄λΈ”(table) λͺ©λ‘ 쑰회. - X
SELECT *
FROM tabs;
 
-- [ DCL λ¬Έ ]
CREATE TABLE ν…Œμ΄λΈ”λͺ…;
ALTER TABLE ν…Œμ΄λΈ”λͺ…;
DROP TABLE DEPT;
-- ORA-00942: table or view does not exist

-- λΆ€μ„œ(dept)ν…Œμ΄λΈ” 생성
CREATE TABLE DEPT
(
DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY, -- λΆ€μ„œλ²ˆν˜Έ PK 컬럼( κ³ μœ ν•œ 컬럼 )
DNAME VARCHAR2(14) , -- λΆ€μ„œλͺ… 컬럼
LOC VARCHAR2(13) -- 지역λͺ… 컬럼
) ;
-- Table DEPT이(κ°€) μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
 
 
 
 
 
-- λΆ€μ„œ 정보 μΆ”κ°€( DML : insert ) λ°˜λ“œμ‹œ commit, rollback ν•΄μ•Ό λœλ‹€. ( μ•”κΈ° )

-- INSERTλ¬Έ ν˜•μ‹μ μœΌμ„Έμš”.)
INSERT INTO ν…Œμ΄λΈ”λͺ… [( 컬럼λͺ…... )] VALUES ( μ»¬λŸΌκ°’... );
COMMIT;
--

INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');
1 ν–‰ 이(κ°€) μ‚½μž…λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
1 ν–‰ 이(κ°€) μ‚½μž…λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
1 ν–‰ 이(κ°€) μ‚½μž…λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
1 ν–‰ 이(κ°€) μ‚½μž…λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
COMMIT;
컀밋 μ™„λ£Œ.

--( μ‹œν—˜ ) dept ν…Œμ΄λΈ”μ˜ ꡬ쑰λ₯Ό ν™•μΈν•˜λŠ” 쿼리 μž‘μ„±ν•˜μ„Έμš”..
DESC ν…Œμ΄λΈ”λͺ…;
DESC dept;
DESC[RIBE] dept;
이름 널? μœ ν˜•
------ -------- ------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)

-- ( μ‹œν—˜ ) dept ν…Œμ΄λΈ”μ˜ λΆ€μ„œ 정보λ₯Ό 쑰회(검색)ν•˜λŠ” 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”.
SELECT * -- λͺ¨λ“  컬럼
FROM dept; ν…Œμ΄λΈ”λͺ… , λ·°λͺ…, μ„œλΈŒμΏΌλ¦¬

-- ( μ‹œν—˜ ) dept ν…Œμ΄λΈ”μ˜ λΆ€μ„œ 정보λ₯Ό 쑰회(검색)ν•˜λŠ” 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”. ( λΆ€μ„œλ²ˆν˜Έ, λΆ€μ„œλͺ… 만 쑰회 )
SELECT deptno, dname -- 컬럼λͺ…, 컬럼λͺ…,..
FROM dept;
SELECT dname -- 컬럼λͺ…, 컬럼λͺ…,..
FROM dept;


-- 관계

-- 사원(emp) ν…Œμ΄λΈ”
CREATE TABLE EMP
(
EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY, -- μ‚¬μ›λ²ˆν˜Έ PK (κ³ μœ ν•œ 컬럼)
ENAME VARCHAR2(10), -- 사원λͺ…
JOB VARCHAR2(9), -- 작
MGR NUMBER(4), -- μ‚¬μˆ˜(κ΄€λ¦¬μž), 직속상사
HIREDATE DATE, -- μž…μ‚¬μΌμž
SAL NUMBER(7,2), -- κΈ°λ³ΈκΈ‰
COMM NUMBER(7,2), -- μˆ˜λ‹Ή
DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT -- λΆ€μ„œλ²ˆν˜Έ DEPT(deptno) μ°Έμ‘° FK, μ°Έμ‘°ν‚€
);
-- Table EMP이(κ°€) μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10);
-- 였λ₯˜ : ORA-01858: a non-numeric character was found where a numeric was expected
-- 숫자λ₯Ό κΈ°λŒ€ν•˜λŠ” 곳에 μˆ«μžκ°€ μ•„λ‹Œ 문자λ₯Ό μ°Ύμ•˜λ‹€.
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,to_date('13-7-87','dd-mm-yyyy' )-85,3000,NULL,20);
숫자 7788
문자, λ¬Έμžμ—΄, λ‚ μ§œ ''


INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,to_date('13-7-87','dd-mm-yyyy')-51,1100,NULL,20);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10);
COMMIT;
--
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,to_date('13-JUL-87')-51,1100,NULL,20)
였λ₯˜ 보고 -
ORA-01858: a non-numeric character was found where a numeric was expected
--
ROLLBACK; -- DML( INSERT ) λͺ¨λ‘ μ·¨μ†Œ
DELETE FROM emp;
-- (μ‹œν—˜) λͺ¨λ“  사원 정보 쑰회.
SELECT *
FROM emp;

 


CREATE TABLE BONUS
(
ENAME VARCHAR2(10) ,
JOB VARCHAR2(9) ,
SAL NUMBER,
COMM NUMBER
) ;
-- Table BONUS이(κ°€) μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

-- κΈ‰μ—¬λ“±κΈ‰ ν…Œμ΄λΈ”
CREATE TABLE SALGRADE
(
GRADE NUMBER, -- λ“±κΈ‰ 1~5
LOSAL NUMBER,
HISAL NUMBER
);
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
COMMIT;

-- ( μ‹œν—˜ ) κΈ°λ³ΈκΈ‰μ˜ λ“±κΈ‰ν…Œμ΄λΈ” μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±ν•˜μ„Έμš”.
SELECT *
FROM salgrade;

-- ( μ‹œν—˜ ) SCOTT이 μ†Œμœ ν•œ λͺ¨λ“  ν…Œμ΄λΈ” λͺ©λ‘ μ‘°νšŒν•˜λŠ” 쿼리( DQL ) μž‘μ„±ν•˜μ„Έμš”.
SELECT *
FROM tabs;

 
-- SCOTT : DEPT, EMP, BONUS, SALGRADE 4개의 ν…Œμ΄λΈ” 생성 + 데이터 μΆ”κ°€

-- 1) λͺ¨λ“  μ‚¬μ›μ˜ 정보λ₯Ό 쑰회
-- (쑰건: μ‚¬μ›λ²ˆν˜Έ, 사원λͺ…, μž…μ‚¬μΌμž 쑰회 )
DESC emp;
-- 컬럼λͺ… 확인 : EMPNO, ENAME, HIREDATE
SELECT empno, ename, hiredate
FROM emp;

-- 2) λͺ¨λ“  μ‚¬μ›μ˜ 정보λ₯Ό 쑰회
-- (쑰건: μ‚¬μ›λ²ˆν˜Έ, 사원λͺ…, μž…μ‚¬μΌμž, κΈ°λ³ΈκΈ‰, μ»€λ―Έμ…˜ 쑰회 )
SELECT empno, ename, hiredate, sal, comm
FROM emp;

-- 3) λͺ¨λ“  μ‚¬μ›μ˜ 정보λ₯Ό 쑰회 ( μ‹œν—˜ )
-- (쑰건 : μ‚¬μ›λ²ˆν˜Έ, 사원λͺ…, μž…μ‚¬μΌμž, κΈ°λ³ΈκΈ‰, μ»€λ―Έμ…˜ 쑰회 )
-- (쑰건 : μ›”κΈ‰ = κΈ°λ³ΈκΈ‰ + μ»€λ―Έμ…˜ )

-- SELECT ν‚€μ›Œλ“œ : distinct, all, as
SELECT empno, ename, hiredate, sal AS κΈ°λ³ΈκΈ‰, comm μˆ˜λ‹Ή
, NVL2( comm, comm, 0 )
, sal + NVL2( comm, comm, 0 ) AS pay
, NVL2( sal + comm, sal + comm, sal ) AS "별 μΉ­"
FROM emp;

-- 3-2) null 의미 ? null μ—†λ‹€ X, λ―Έμ •
-- μ£Όμ§„μ˜ λͺΈλ¬΄κ²Œ μ–Όλ§ˆμ˜ˆμš” ? λͺΈλ¬΄κ²Œ μΈ‘μ • X, λͺΈλ¬΄κ²Œκ°€ μ–Όλ§ˆμΈμ§€ λͺ°λΌμš”.
-- μ˜€λΌν΄μ—μ„œ NULL은 μƒλž΅λœ, μ•Œλ €μ§€μ§€ μ•Šμ€, ν˜Ήμ€ μ μš©ν•  수 μ—†λŠ” 값을 μ˜λ―Έν•˜λŠ” μ˜ˆμ•½μ–΄
-- 3-3) null μ—°μ‚° κ°’ = null
-- 3-4) null 처리 방법 ? μ»€λ―Έμ…˜μ΄ λ―Έμ •(null) -> 0 라고 처리
-- null μ²˜λ¦¬ν•˜λŠ” ν•¨μˆ˜( function ) :
-- 1) NVL
-- 2) NVL2 ***
NVL2(a, b, c)
a 값이 null 이 μ•„λ‹ˆλ©΄ b
a 값이 null 이면 c
-- 3) NULLIF
-- 4) COALESCE


-- λͺ¨λ“  μ‚¬μš©μž 계정 정보λ₯Ό 확인
SELECT *
FROM dba_users;
-- ORA-00942: table or view does not exist

-- SCOTT ( νŒŒλž‘ )--
-- λͺ¨λ“  ν…Œμ΄λΈ” λͺ©λ‘ 쑰회 쿼리 μž‘μ„±
SELECT *
FROM user_tables; -- == FROM tabs; λ·°( View )


-- ( μ‹œν—˜ ) emp ν…Œμ΄λΈ”μ—μ„œ
-- λͺ¨λ“  사원 정보λ₯Ό 쑰회(검색)
-- μ‚¬μ›λ²ˆν˜Έ, 사원λͺ…, μž…μ‚¬μΌμž, κΈ‰μ—¬( κΈ°λ³ΈκΈ‰ + μ»€λ―Έμ…˜ )
--- (μΆ”κ°€ 문제) μ •λ ¬ - μ˜€λ¦„μ°¨μˆœ, λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬
-- 1,2,3,4
-- a,b,c,d
-- γ„±,γ„΄,γ„·,γ„Ή
-- pay(κΈ‰μ—¬)λ₯Ό 많이 λ°›λŠ” 사원뢀터 좜λ ₯.(쑰회) : κΈ‰μ—¬ λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬
-- sal(κΈ°λ³ΈκΈ‰) "

SELECT empno, ename, hiredate , sal, comm
, sal + NVL2( comm, comm, 0 ) pay
-- , NVL2( sal + comm, sal+comm, sal ) pay
-- -- , NVL() comm null 처리
-- , sal + NVL( comm, 0 ) pay
-- , NVL( sal+comm, sal ) pay
FROM emp
ORDER BY sal ; -- ASC 기본은 μ˜€λ¦„μ°¨μˆœ μ •λ ¬.
-- ORDER BY sal ASC;
-- ORDER BY sal DESC; -- λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬
-- FROM scott.emp; --μŠ€ν‚€λ§ˆ.객체λͺ…

-- ( μ‹œν—˜ ) emp ν…Œμ΄λΈ”μ—μ„œ
-- 각 λΆ€μ„œλ²ˆν˜Έλ‘œ μ˜€λ¦„μ°¨μˆœ 정렬을 ν•˜μ„Έμš”.
-- λΆ€μ„œλ²ˆν˜Έ , 사원λͺ…, μž…μ‚¬μΌμž, μ›”κΈ‰(pay) 좜λ ₯.
SELECT deptno, ename, hiredate, sal + NVL(comm, 0) pay
FROM scott.emp
ORDER BY deptno ASC, hiredate DESC;

ORDER BY 1 ASC;
ORDER BY deptno ASC;

-- 첫 번째 μ •λ ¬ : λΆ€μ„œλ²ˆν˜Έ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ (1μ°¨ μ •λ ¬)
-- 두 번째 μ •λ ¬ : 같은 λΆ€μ„œλ²ˆν˜Έ λ‚΄μ—μ„œ μž…μ‚¬μΌμžλ₯Ό λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ ( 2μ°¨ μ •λ ¬ )


-- (문제) emp ν…Œμ΄λΈ”μ—μ„œ
-- λΆ€μ„œλ²ˆν˜Έλ‘œ μ˜€λ¦„μ°¨μˆœ 정렬을 ν•œ ν›„ μ›”κΈ‰(pay) κΈ°μ€€μœΌλ‘œ λ‚΄λ¦Όμ°¨μˆœ 정렬을 ν•˜μ„Έμš”.
-- ( deptno, ename, hiredate, pay 컬럼 )
SELECT deptno, ename, hiredate, sal + NVL(comm, 0) pay
FROM emp
ORDER BY deptno , pay DESC;

-- SELECT DISTINCT, AS , ALL

SELECT ALL job --λ³„κ°œμ˜ λΆ„λͺ…ν•œ, κ΅¬λ³„λ˜λŠ”
FROM emp;

-- 사원듀이 μ†ν•΄μžˆλŠ” [ job μ’…λ₯˜ ] 쑰회 좜λ ₯. + μ˜€λ¦„μ°¨μˆœ μ •λ ¬
-- 쀑볡 제거 ν•΄μ„œ 1번 좜λ ₯
SELECT DISTINCT job --λ³„κ°œμ˜ λΆ„λͺ…ν•œ, κ΅¬λ³„λ˜λŠ”
FROM emp
ORDER BY job ASC;
-- X
SELECT DISTINCT job , ename --λ³„κ°œμ˜ λΆ„λͺ…ν•œ, κ΅¬λ³„λ˜λŠ”
FROM emp;

-- ( μ‹œν—˜ ) emp ν…Œμ΄λΈ”μ—μ„œ
-- 10번 λΆ€μ„œμ›λ“€λ§Œ 쑰회(좜λ ₯) -- 쑰건절
SELECT deptno, ename, hiredate, mgr
FROM emp
WHERE deptno = 10; -- 였라클 κ°™λ‹€ = μ—°μ‚°μž
WHERE deptno == 10; -- 304ν–‰, 15μ—΄μ—μ„œ 였λ₯˜ λ°œμƒ, ORA-00936: missing expression
WHERE 쑰건절;



-- SYS -
GRANT CONNECT,RESOURCE,UNLIMITED TABLESPACE -- λ‘€
TO SCOTT
IDENTIFIED BY tiger;
-- Grant을(λ₯Ό) μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€.

CREATE USER
DROP USER
ALTER USER SCOTT DEFAULT TABLESPACE USERS;
-- User SCOTT이(κ°€) λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
ALTER USER SCOTT TEMPORARY TABLESPACE TEMP;

-- SYS --
SELECT *
FROM dba_users;

-- ( μ‹œν—˜ ) HR 계정 μˆ˜μ • - λΉ„λ°€λ²ˆν˜Έ lion μˆ˜μ •
CREATE USER;
DROP USER ;


ALTER USER hr IDENTIFIED BY lion;
-- User HR이(κ°€) λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
-- ACCOUNT_STATUS : LOCKED (잠금) -> hr κ³„μ •μ˜ 잠금 ν•΄μ œ
-- ORA-28000: the account is locked

ALTER USER hr ACCOUNT UNLOCK;
ALTER USER hr IDENTIFIED BY lion ACCOUNT UNLOCK;





**Rem : μ£Όμ„μ²˜λ¦¬
  • 넀이버 λΈ”λŸ¬κ·Έ κ³΅μœ ν•˜κΈ°
  • 넀이버 λ°΄λ“œμ— κ³΅μœ ν•˜κΈ°
  • 페이슀뢁 κ³΅μœ ν•˜κΈ°
  • μΉ΄μΉ΄μ˜€μŠ€ν† λ¦¬ κ³΅μœ ν•˜κΈ°