[Day3] Oracle 3 [9/27]

 

--1. μš©μ–΄μ •λ¦¬
-- γ„±. Data : 정보(자료)
-- γ„΄. DataBase : Data + BASE , 데이터 집합(μ €μž₯μ†Œ)
-- γ„·. DBMS : λ°μ΄ν„°λ² μ΄μŠ€ 관리 μ‹œμŠ€ν…œ , μ†Œν”„νŠΈμ›¨μ–΄
-- γ„Ή. DBA :λ°μ΄ν„°λ² μ΄μŠ€ κ΄€λ¦¬μž ( SYS, SYSTEM )
-- ㅁ. λ‘€(ROLE) : λ‹€μˆ˜ μ‚¬μš©μž, λ‹€μˆ˜ κΆŒν•œ + 효율적으둜 κ΄€λ¦¬ν•˜κΈ° μœ„ν•œ κΆŒν•œμ˜ κ·Έλ£Ή
-- γ…‚. SID( μ „μ—­ λ°μ΄ν„°λ² μ΄μŠ€ 이름 ) : XE
-- γ……. 데이터 λͺ¨λΈ : 데이터λ₯Ό μ €μž₯ν•˜λŠ” 방식을 데이터 λͺ¨λΈ
-- γ…‡. μŠ€ν‚€λ§ˆ(Schema) : 계정 생성 + μŠ€ν‚€λ§ˆ( λͺ¨λ“  객체( ν…Œμ΄λΈ”, λ·°, ν”„λ‘œμ‹œμ €, 트리거, μ‹œν€€μŠ€ ... ) )
scott scott
μŠ€ν‚€λ§ˆ.ν…Œμ΄λΈ”λͺ…
[scott.]emp
 
-- γ…ˆ. R+DBMS : κ΄€κ³„ν˜• + DBMS
개체(ν…Œμ΄λΈ”) + 관계(μ—°κ΄€μ„±) + 개체(ν…Œμ΄λΈ”)
dept μ†Œμ†κ΄€κ³„ emp
deptno(PK) κ³ μœ ν‚€ deptno(FK) μ°Έμ‘°ν‚€
 
 
 

--2. μ„€μΉ˜λœ [μ˜€λΌν΄μ„ μ‚­μ œ]ν•˜λŠ” 절차λ₯Ό [검색]ν•΄μ„œ μƒμ„Ένžˆ μ μœΌμ„Έμš”. ***
γ„±. μ„œλΉ„μŠ€ 쀑지
γ„΄. un,de 파일 μ‹€ν–‰ , ν”„λ‘œκ·Έλž¨μΆ”κ°€μ œκ±°
γ„·. oracle 폴더 μ‚­μ œ
γ„Ή. λ ˆμ§€μŠ€ν„°λ¦¬ μ‚­μ œ.
 
--3. SYS κ³„μ •μœΌλ‘œ μ ‘μ†ν•˜μ—¬ λͺ¨λ“  μ‚¬μš©μž 정보λ₯Ό μ‘°νšŒν•˜λŠ” 쿼리(SQL)을 μž‘μ„±ν•˜μ„Έμš”.
SELECT *
FROM user_users;
FROM all_users;
FROM dba_users;

--4. SCOTT 계정 + μ†Œμœ  객체(ν…Œμ΄λΈ”) λ₯Ό μ‚­μ œν•˜λŠ” 쿼리(SQL)을 μž‘μ„±ν•˜μ„Έμš”.
γ„±. 계정 생성
CREATE USER scott IDENTIFIED BY tiger;
γ„΄. 계정 μˆ˜μ • - λΉ„λ°€λ²ˆν˜Έ μˆ˜μ •, 잠금 ν•΄μ œ
ALTER USER scott IDENTIFIED BY tiger;
γ„·. 계정 μ‚­μ œ
DROP USER scott CASCADE;

--5. μœ„μ˜ 4λ²ˆμ— μ˜ν•΄μ„œ
-- SCOTT 계정이 μ‚­μ œλ˜μ—ˆμœΌλ©΄ SCOTT 계정을 tiger λΉ„λ°€λ²ˆν˜Έλ‘œ μƒμ„±ν•˜λŠ” 쿼리(SQL)을 μž‘μ„±ν•˜μ„Έμš”.
 
--6. 였라클이 μ œκ³΅ν•˜λŠ” 기본적인 λ‘€(role)의 μ’…λ₯˜λ₯Ό μ μ–΄μ„Έμš”..
CONNECT,
RESOURCE
:
 
Ora_Help
 
--6-2. SCOTT κ³„μ •μ—κ²Œ κΆŒν•œμ„ λΆ€μ—¬ν•˜λŠ” 쿼리(SQL)을 μž‘μ„±ν•˜μ„Έμš”.
DCL - GRANT, REVOKE
 
GRANT κΆŒν•œ, λ‘€ TO 계정, λ‘€
GRANT CONNECT, RESOURCE TO scott;

--7-1. SCOTT κ³„μ •μ—κ²Œ scott.sql νŒŒμΌμ„ μ°Ύμ•„μ„œ emp, dept, bonus, salgrade ν…Œμ΄λΈ”μ„ 생성 및 데이터 μΆ”κ°€ν•œ 과정을 μž‘μ„±ν•˜μ„Έμš”.
-- scott.sql 파일
 

--7-2. 각 ν…Œμ΄λΈ”μ— μ–΄λ–€ 정보λ₯Ό μ €μž₯ν•˜λŠ”μ§€ μ»¬λŸΌμ— λŒ€ν•΄ 정보( 컬럼λͺ…, μžλ£Œν˜• )λ₯Ό μ„€λͺ…ν•˜μ„Έμš”.
-- γ„±. emp
DESC emp;
-- γ„΄. dept
-- γ„·. bonus
-- γ„Ή. salgrade

--8. SCOTT 계정이 μ†Œμœ ν•˜κ³  μž‡λŠ” λͺ¨λ“  ν…Œμ΄λΈ”μ„ μ‘°νšŒν•˜λŠ” 쿼리(SQL)을 μž‘μ„±ν•˜μ„Έμš”.
SELECT *
FROM dba_tables; -- DBA(SYS) ORA-00942: table or view does not exist
FROM all_tables;
FROM user_tables;
FROM tabs; -- μ•½μ–΄

--9. SQL*Plus λ₯Ό μ‚¬μš©ν•˜μ—¬ SYS둜 μ ‘μ†ν•˜μ—¬ μ ‘μ†ν•œ μ‚¬μš©μž ν™•μΈν•˜κ³ , λͺ¨λ“  μ‚¬μš©μž 정보λ₯Ό μ‘°νšŒν•˜κ³ 
-- μ’…λ£Œν•˜λŠ” λͺ…령문을 μž‘μ„±ν•˜μ„Έμš”.
>sqlplus sys/ss123$ AS SYSDBA SQLPlus λͺ…λ Ήμ–΄
SQL>SELECT *
2 FROM dba_users; SQL
3

--10. κ΄€κ³„ν˜• 데이터 λͺ¨λΈμ˜ 핡심 ꡬ성 μš”μ†Œ
γ„±. 개체
γ„΄. 속성
γ„·. 관계

--11. Oracle SQL Developer μ—μ„œ 쿼리(SQL)을 μ‹€ν–‰ν•˜λŠ” 방법을 λͺ¨λ‘ μ μœΌμ„Έμš”.
γ„±. Ctrl + Enter
γ„΄. 쿼리 μ„ νƒœ(select)ν•œ ν›„ F5
γ„·. F9

--12. 였라클 μ£Όμ„μ²˜λ¦¬ 방법 2가지λ₯Ό μ μœΌμ„Έμš”.
γ„±.
γ„΄.

--13. 자료 사전( Data [Dictionary] ) μ΄λž€? X
 
--14. SQL μ΄λž€ ? κ΅¬μ‘°ν™”λœ 질의 μ–Έμ–΄

--15. SQL의 μ’…λ₯˜μ— λŒ€ν•΄ μƒμ„Ένžˆ μ μœΌμ„Έμš”.
γ„±. DQL select 데이터 검색(쑰회)
γ„΄. DDL c,a,d 데이터 μ •μ˜
γ„·. DCL g,r
γ„Ή. DML i,u,d
ㅁ. TCL c,r,sp

--16. select 문의 7 개의 절과 처리 μˆœμ„œμ— λŒ€ν•΄μ„œ μ μœΌμ„Έμš”. **** μ€‘μš”
1 WITH
6 SELECT 
2 FROM
3 WHERE
4 GROUP BY
5 HAVING
7 ORDER BY 
 
W / S / F / W / G / H / O

  
--17. emp ν…Œμ΄λΈ”μ˜ ν…Œμ΄λΈ” ꡬ쑰(μ»¬λŸΌμ •λ³΄)λ₯Ό ν™•μΈν•˜λŠ” 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”.
DESC emp;
 
--18. employees ν…Œμ΄λΈ”μ—μ„œ μ•„λž˜μ™€ 같이 좜λ ₯λ˜λ„λ‘ 쿼리 μž‘μ„±ν•˜μ„Έμš”.
 
FIRST_NAME LAST_NAME NAME
-------------------- ------------------------- ----------------------------------------------
Samuel McCain Samuel McCain
Allan McEwen Allan McEwen
Irene Mikkilineni Irene Mikkilineni
Kevin Mourgos Kevin Mourgos
Julia Nayer Julia Nayer

SELECT first_name, last_name

, first_name || ' ' || last_name AS "NAME"
, CONCAT( CONCAT( first_name, ' ' ), last_name ) name

FROM employees;



--19. μ•„λž˜ λ·°(View)에 λŒ€ν•œ μ„€λͺ…을 μ μœΌμ„Έμš”.
-- γ„±. dba_tables
-- γ„΄. all_tables
-- γ„·. user_tables == tabs

dba_XXX
all_XXX
user_XXX
 

--20. HR κ³„μ •μ˜ 생성 μ‹œκΈ°μ™€ [μž κΈˆμƒνƒœ]λ₯Ό ν™•μΈν•˜λŠ” 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”.
-- SYS
SELECT ename
, UPPER( ename )
, LOWER( ename )
, INITCAP( ename )      // 첫문자만 λŒ€λ¬Έμž
FROM emp;

select username, account_status, created
FROM Dba_users
WHERE UserName = UPPER( 'hr' );
 
 
 
 
--21. emp ν…Œμ΄λΈ”μ—μ„œ 작, μ‚¬μ›λ²ˆν˜Έ, 이름, μž…μ‚¬μΌμžλ₯Ό μ‘°νšŒν•˜λŠ” 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”.

SELECT empno, ename, hiredate, job
FROM emp;

--22. emp ν…Œμ΄λΈ”μ—μ„œ μ•„λž˜μ™€ 같은 쑰회 κ²°κ³Όκ°€ λ‚˜μ˜€λ„λ‘ 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”.
( sal + comm = pay )
 
null 의미 ?
null 처리 ν•¨μˆ˜ ?
 
SELECT EMPNO, ENAME, SAL, NVL( COMM , 0) COMM
, SAL + NVL2( comm, comm, 0) PAY
FROM emp;
 
EMPNO ENAME SAL COMM PAY
---------- ---------- ---------- ---------- ----------
7369 SMITH 800 0 800
7499 ALLEN 1600 300 1900
7521 WARD 1250 500 1750
7566 JONES 2975 0 2975
7654 MARTIN 1250 1400 2650
7698 BLAKE 2850 0 2850
7782 CLARK 2450 0 2450
7839 KING 5000 0 5000
7844 TURNER 1500 0 1500
7900 JAMES 950 0 950
7902 FORD 3000 0 3000

EMPNO ENAME SAL COMM PAY
---------- ---------- ---------- ---------- ----------
7934 MILLER 1300 0 1300

12개 행이 μ„ νƒλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

이해 X -> μ•”κΈ° (μ½”λ”© )

--23. empν…Œμ΄λΈ”μ—μ„œ
-- 각 λΆ€μ„œλ³„λ‘œ μ˜€λ¦„μ°¨μˆœ 1μ°¨ μ •λ ¬ν•˜κ³  κΈ‰μ—¬(PAY)λ³„λ‘œ 2μ°¨ λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•΄μ„œ μ‘°νšŒν•˜λŠ” 쿼리λ₯Ό μž‘μ„±ν•˜μ„Έμš”.
SELECT deptno, EMPNO, ENAME, SAL, NVL( COMM , 0) COMM
, SAL + NVL2( comm, comm, 0) pay
FROM emp
ORDER BY 1 , 6 DESC;
ORDER BY deptno , pay DESC;

--24. 계정을 μƒμ„±ν•˜λŠ” 쿼리 ν˜•μ‹μ— λŒ€ν•΄ μ μœΌμ„Έμš”.
CREATE USER ~ DDL
 
--25. μƒμ„±λœ κ³„μ •μ˜ λΉ„λ°€λ²ˆν˜Έ , μž κΈˆμ„ ν•΄μ œν•˜λŠ” 쿼리 ν˜•μ‹μ— λŒ€ν•΄μ„œ μ μœΌμ„Έμš”.
ALTER USER ~ DDL
 
--26. DB에 λ‘œκ·ΈμΈν•  수 μžˆλŠ” κΆŒν•œμ„ λΆ€μ—¬ν•˜λŠ” 쿼리 ν˜•μ‹μ— λŒ€ν•΄μ„œ μ μœΌμ„Έμš”.
CREATE SESSION κΆŒν•œ < CONNECT λ‘€
DCL : GRANT
GRANT κΆŒν•œ, λ‘€ TO 계정, λ‘€
 
--27. νŠΉμ •ν¬νŠΈ( 1521 Port )λ₯Ό [ λ°©ν™”λ²½ ν•΄μ œ ]ν•˜λŠ” 방법에 λŒ€ν•΄μ„œ μƒμ„Ένžˆ 과정을 μ μœΌμ„Έμš”.
1) 돋보기 : λ°©ν™”λ²½ - "λ°©ν™”λ²½ ν•΄μ œ 확인" 클릭
 

 )을 μ μœΌμ„Έμš”

--29. SQL의 μž‘μ„±λ°©λ²• X

SELECT
select
Select 'hr'


SELECT ename, job, hiredate
FROM emp
WHERE
ORDER BY ;

select ename, job
from emp
where ename = '???';


JAVA μ‹λ³„μžλ₯Ό λͺ…λͺ…ν•˜λŠ” κ·œμΉ™
 
--30. μ•„λž˜ μ—λŸ¬ λ©”μ‹œμ§€μ˜ 의미λ₯Ό μ μœΌμ„Έμš”.
γ„±. ORA-00942: table or view does not exist
γ„΄. ORA-00904: "SCOTT": invalid identifier
γ„·. ORA-00936: missing expression
γ„Ή. ORA-00933: SQL command not properly ended X

-- 31. emp ν…Œμ΄λΈ”μ—μ„œ λΆ€μ„œλ²ˆν˜Έκ°€ 10번이고, 작이 CLERK 인 μ‚¬μ›μ˜ 정보λ₯Ό μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±.
SELECT deptno, ename, job
FROM emp
WHERE job = 'CLERK' AND deptno = 10;

-- JAVA if( A && B ) λ…Όλ¦¬μ—°μ‚°μž &&
-- 였라클 μ–΄λ–€ μ—°μ‚°μž 정리

10 MILLER CLERK

- , λΆ€μ„œλ²ˆν˜Έκ°€ 10번이 μ•„λ‹Œ μ‚¬μ›μ˜ 정보λ₯Ό μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±.
-- μžλ°” 비ꡐ μ—°μ‚°μž == [!=] > < >= <=
-- 였라클 비ꡐ μ—°μ‚°μž = !=, <>, ^=

SELECT deptno, ename, job
FROM emp
WHERE job = 'CLERK' AND deptno ^= 10;
WHERE job = 'CLERK' AND deptno <> 10;
WHERE job = 'CLERK' AND deptno != 10;


-- 32. 였라클의 null의 의미 와 null 처리 ν•¨μˆ˜μ— λŒ€ν•΄μ„œ μ„€λͺ…ν•˜μ„Έμš” .
γ„±. null 의미? λ―Έν™•μΈλœ κ°’.
γ„΄. null 처리 ν•¨μˆ˜ 2가지 μ’…λ₯˜μ™€ ν˜•μ‹μ„ 적고 μ„€λͺ…ν•˜μ„Έμš” .
NVL()
NVL2()
N
C
γ„·. null 비ꡐ할 λ•Œ μ‚¬μš©ν•˜λŠ” μ—°μ‚°μž : Ora_Help operator 검색
 
-- 33. emp ν…Œμ΄λΈ”μ—μ„œ λΆ€μ„œλ²ˆν˜Έκ°€ 30번이고, μ»€λ―Έμ…˜μ΄ null인 μ‚¬μ›μ˜ 정보λ₯Ό μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±.
( γ„±. deptno, ename, sal, comm, pay 컬럼 좜λ ₯, pay= sal+comm )
( γ„΄. comm이 null 인 κ²½μš°λŠ” 0으둜 λŒ€μ²΄ν•΄μ„œ 처리 )
( γ„·. pay κ°€ λ§Žμ€ 순으둜 μ •λ ¬ )
 
SELECT deptno, ename, sal, comm, sal + NVL(comm, 0) pay
FROM emp
WHERE deptno = 30 AND NOT ( comm IS NULL )
ORDER BY pay DESC; -- 이 λ•Œ NOT 은 AND, OR, NOT λΆ€μ •μ—°μ‚°μž
 
WHERE deptno = 30 AND comm IS NOT NULL;
WHERE deptno = 30 AND comm IS NULL;
WHERE deptno = 30 AND comm = null; X
 
-- κ²°κ³Ό λ¬Ό X -> 2λͺ…
 
 
-- 34. 였라클 비ꡐ μ—°μ‚°μžλ₯Ό μ μœΌμ„Έμš”.
γ„±. κ°™λ‹€ : =
γ„΄. λ‹€λ₯΄λ‹€ : != <> ^=
 
-- *****
-- 35. emp ν…Œμ΄λΈ”μ—μ„œ pay(sal+comm)κ°€ 1000 이상~ 2000 μ΄ν•˜ λ°›λŠ” 30λΆ€μ„œμ›λ“€λ§Œ μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±
쑰건 : γ„±. pay κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ --ename을 κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•΄μ„œ 좜λ ₯(쑰회)
γ„΄. comm 이 null은 0으둜 처리 ( nvl () )
 

(첫번째 방법)
SELECT deptno, ename, sal, comm, sal + NVL(comm, 0) pay
FROM emp
WHERE deptno = 30 AND 1000 <= (sal + NVL(comm, 0)) AND (sal + NVL(comm, 0)) <= 2000 ;
-- 처리 μˆœμ„œ λ•Œλ¬Έμ— 별칭 payλŠ” Where μ ˆμ—μ„œλŠ” 인식할 수 μ—†λ‹€.
-- WHERE deptno = 30 AND 1000 <= pay AND pay <= 2000 ; --510ν–‰, 44μ—΄μ—μ„œ 였λ₯˜ λ°œμƒ
-- ORA-00904: "PAY": invalid identifier 잘λͺ»λœ μ‹λ³„μž.
WHERE deptno = 30 AND ( 1000 <= pay AND pay <= 2000 );
-- JAVA 1000 <= pay && pay <= 2000
 
(두 번째 방법) WITH 절 μ‚¬μš©
WITH query AS (subquery),query AS (subquery),query AS (subquery),...
예제)
SQL> WITH
2 dept_costs AS (
3 SELECT department_name, SUM(salary) dept_total
4 FROM employees e, departments d
5 WHERE e.department_id = d.department_id
6 GROUP BY department_name),
7 avg_cost AS (
8 SELECT SUM(dept_total)/COUNT(*) avg
9 FROM dept_costs)
 
 
10 SELECT * FROM dept_costs
11 WHERE dept_total >
12 (SELECT avg FROM avg_cost)
13 ORDER BY department_name;

WITH
emp_30 AS (
SELECT deptno, ename, sal, comm, sal + NVL(comm, 0) pay -- μ„œλΈŒμΏΌλ¦¬(subquery)
FROM emp
WHERE deptno = 30
),
emp_20 AS (
SELECT deptno, ename, sal, comm, sal + NVL(comm, 0) pay -- μ„œλΈŒμΏΌλ¦¬(subquery)
FROM emp
WHERE deptno = 20
)
SELECT *
FROM emp_30
WHERE pay BETWEEN 1000 AND 2000 ;
WHERE pay >= 1000 AND pay <= 2000 ;
이상 μ΄ν•˜ SQL μ—°μ‚°μž : [NOT] between A and B , IS [NOT] NULL
 
(μ„Έ 번째 방법) μ„œλΈŒμΏΌλ¦¬( subquery) μ‚¬μš©
Queryλž€?
ν•˜λ‚˜μ΄μƒμ˜ ν…Œμ΄λΈ”μ΄λ‚˜ λ·°λ‘œλΆ€ν„° 데이터λ₯Ό 검색(retrieve)ν•˜λŠ” λ™μž‘μ„ λ§ν•œλ‹€.
이 λ•Œ μ°Έμ‘°ν•˜λŠ” μ΅œμƒμœ„ 레벨 SELECT 문을 Query(질의:쿼리)라 ν•œλ‹€.
subqueryλž€ ? 쿼리가 λ˜λ‹€λ₯Έ SQL 문으둜 nested된 것을 subquery라 ν•œλ‹€.
즉 SQL λ¬Έ 속에 또 λ‹€λ₯Έ SQL문이 λ„£μ–΄μ Έ μžˆλŠ” κ²½μš°μ΄λ‹€.

[ IN-LINE view μ„œλΈŒμΏΌλ¦¬ ]

--------------------------------------------------------------------------------
VIEWλ₯Ό μ‚¬μš©ν•˜λŠ” 것과 μ•„μ£Ό μœ μ‚¬ν•˜κ²Œ select λ¬Έμž₯의 FROMμ ˆμ—μ„œ subqueryλ₯Ό μ‚¬μš©ν•œλ‹€.
• subquery λ’€μ—λŠ” λ°˜λ“œμ‹œ aliasλ₯Ό μ€€λ‹€.
• subqueryλ₯Ό FROMμ ˆμ— μ‚¬μš©ν•  수 μžˆλ‹€. FROMμ ˆμ— μœ„μΉ˜ν•œ subqueryλŠ” ν•˜λ‚˜μ˜ ν…Œμ΄λΈ”μ²˜λŸΌ μ‚¬μš©λœλ‹€.
참고둜

 

μ„œλΈŒμΏΌλ¦¬κ°€ FROM μ ˆμ— 있으면 이λ₯Ό Inline viewλΌν•˜κ³ ,

μ„œλΈŒμΏΌλ¦¬κ°€ WHERE μ ˆμ— 있으면 이λ₯Ό Nested subquery라 함

Nested subqueryμ€‘μ—μ„œ μ°Έμ‘°ν•˜λŠ” ν…Œμ΄λΈ”μ΄ parent, child관계λ₯Ό 가지면 이λ₯Ό "correlated subquery"라 ν•œλ‹€.

상관 μ„œλΈŒμΏΌλ¦¬
μš©μ–΄ ( μ•”κΈ° )
쿼리 ?
μ„œλΈŒμΏΌλ¦¬ ?
인라인뷰?
쀑첩 μ„œλΈŒμΏΌλ¦¬?
상관 μ„œλΈŒμΏΌλ¦¬ ?

 

FROM ( μ„œλΈŒμΏΌλ¦¬ ) alias(별칭,별λͺ…) "인라인뷰( inline view )" ν•œλ‹€.

문제
-- 문제1) insa ν…Œμ΄λΈ”μ—μ„œ μ‚¬μ›μ˜ μΆœμ‹ μ§€μ—­μ΄ 'μ„œμšΈ'인 사원 정보 쑰회(좜λ ₯)
-- ( μ‚¬μ›λ²ˆν˜Έ, 이름, μž…μ‚¬μΌμž, μΆœμ‹ μ§€μ—­ μ •λ³΄λ§Œ 좜λ ₯ )
SELECT num, name, ibsadate, city
FROM insa
WHERE city = 'μ„œμšΈ';

-- 문제2) insa ν…Œμ΄λΈ”μ—μ„œ μ‚¬μ›μ˜ μΆœμ‹ μ§€μ—­μ΄ [μˆ˜λ„κΆŒ]인 사원 정보 쑰회(좜λ ₯)
-- ( μ‚¬μ›λ²ˆν˜Έ, 이름, μž…μ‚¬μΌμž, μΆœμ‹ μ§€μ—­ μ •λ³΄λ§Œ 좜λ ₯ )
-- μˆ˜λ„κΆŒ : μ„œμšΈ, 인천, κ²½κΈ°
SELECT num, name, ibsadate, city
FROM insa
--WHERE city = 'μ„œμšΈ' OR city = '인천' OR city = 'κ²½κΈ°'
-- [NOT] IN (list)
WHERE city IN ( 'μ„œμšΈ',  '인천', 'κ²½κΈ°')
ORDER BY city ASC;

-- AND OR NOT 였라클 논리 μ—°μ‚°μž
-- SQL μ—°μ‚°μž IN μ—°μ‚°μž

-- ( 문제 3) insa ν…Œμ΄λΈ”μ—μ„œ μΆœμ‹ μ§€μ—­μ΄ 'μ„œμšΈ'이 μ•„λ‹Œ 사원 정보 쑰회.
SELECT num, name, ibsadate, city
FROM insa
--WHERE city != 'μ„œμšΈ'
WHERE NOT city = 'μ„œμšΈ'
ORDER BY city ASC;

-- 문제4) insa ν…Œμ΄λΈ”μ—μ„œ μ‚¬μ›μ˜ μΆœμ‹ μ§€μ—­μ΄ [μˆ˜λ„κΆŒ]이 μ•„λ‹Œ 사원 정보 쑰회(좜λ ₯)
-- ( μ‚¬μ›λ²ˆν˜Έ, 이름, μž…μ‚¬μΌμž, μΆœμ‹ μ§€μ—­ μ •λ³΄λ§Œ 좜λ ₯ )
SELECT num, name, ibsadate, city
FROM insa
WHERE city != 'μ„œμšΈ' AND city != '인천' AND city != 'κ²½κΈ°'
-- [NOT] IN (list 'λ‚ μ§œ', 숫자, 'λ¬Έμžμ—΄' )
--WHERE city NOT IN ( 'μ„œμšΈ', '인천', 'κ²½κΈ°')
--WHERE NOT city IN ( 'μ„œμšΈ', '인천', 'κ²½κΈ°')
ORDER BY city ASC;

 
-- 36. emp ν…Œμ΄λΈ”μ—μ„œ 1981년도에 μž…μ‚¬ν•œ μ‚¬μ›λ“€λ§Œ μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±. X
-- 비ꡐ μ—°μ‚°μž : 숫자, 문자, λ‚ μ§œ.
-- ORA-00933: SQL command not properly ended
SELECT empno, ename, hiredate, deptno
FROM emp
WHERE SUBSTR( hiredate, 0, 2) = 81; -- "81" == 81    // μ˜€λΌν΄μ€ λ¬Έμžμ™€ 숫자 κ°™λ‹€κ³  λ‚˜μ˜΄
WHERE SUBSTR( hiredate, 0, 2) = '81';
WHERE EXTRACT( YEAR FROM hiredate ) = 1981;
WHERE TO_CHAR( hiredate, 'YYYY' ) = 1981;
WHERE hiredate BETWEEN '1981.1.1' AND '1981.12.31';
 


-- JAVA λ‚ μ§œ-> year

-- JAVA "80/12/17".substring(0, 2); -> "80"
SELECT hiredate
, SUBSTR( hiredate, 0, 2) year
, SUBSTR( hiredate, 1, 2) year
, SUBSTR( hiredate, 7, 2) day
, SUBSTR( hiredate, 7 ) day
, SUBSTR( hiredate, -2) day
FROM emp;

 

-- [ RR/YY 차이점 ]
-- 1. EXTRACT()                           //87이 λ‚˜μ˜€κ³      (숫자둜 κ°€μ Έμ˜΄)
-- 2. TO_CHAR( λ‚ μ§œ , 'μ„œμ‹' )       // 0087이 λ‚˜μ˜΄  (문자둜 κ°€μ Έμ˜΄)
-- DATE hiredate μ»¬λŸΌμ—μ„œ 년도 μ–»μ–΄μ˜€λŠ” 방법
SELECT hiredate
, EXTRACT( YEAR FROM hiredate ) -- number 1980
-- , EXTRACT( MONTH FROM hiredate )
-- , EXTRACT( DAY FROM hiredate )
, TO_CHAR( hiredate, 'YYYY' ) -- '1980'
, TO_CHAR( hiredate, 'MM' )
, TO_CHAR( hiredate, 'DD' )
FROM emp
WHERE 년도 = 1981;
 

μœ„μ˜ 코딩을 LIKE μ—°μ‚°μž, REGEXP_LIKE ν•¨μˆ˜ μ‚¬μš©ν•΄μ„œ μˆ˜μ •

1) [NOT] LIke μ—°μ‚°μž
2) μˆ˜μ‹κΈ°ν˜Έ( wildcard )만 μ‚¬μš© κ°€λŠ₯
% 0~ μ—¬λŸ¬ 개 *
_ ν•œκ°œμ˜ 문자
3) wildcard( %, _ )λ₯Ό 일반 문자처럼 μ“°κ³  싢은 κ²½μš°μ—λŠ” ESCAPE μ˜΅μ…˜μ„ μ‚¬μš©
예) LIKE 'ν•œκΈ€\_%' ESCAPE '\';
 
-- 문제) insa ν…Œμ΄λΈ”μ—μ„œ 성이 κΉ€, 이, μ΅œμ”¨ 인 사원 쑰회.
-- ( WHERE μ‘°κ±΄μ ˆμ— LIKE μ—°μ‚°μž μ‚¬μš© )
-- ( WHERE μ‘°κ±΄μ ˆμ— REGEXP_LIKE ν•¨μˆ˜ μ‚¬μš© )
-- ν˜•μ‹ ) REGEXP_LIKE(source_char, pattern [,match_param])

SELECT name, ibsadate
FROM insa
WHERE REGEXP_LIKE( name , '^[κΉ€μ΄μ΅œ]');
WHERE REGEXP_LIKE( name , '^(κΉ€|이)');

WHERE REGEXP_LIKE( name , '^영'); -- WHERE name LIKE '영%';
WHERE REGEXP_LIKE( name , '영$'); -- WHERE name LIKE '%영';
WHERE REGEXP_LIKE( name , '영'); -- WHERE name LIKE '%영%';


SELECT name, ibsadate
, SUBSTR( name, 0, 1 )
FROM insa
WHERE name LIKE 'κΉ€%' OR name LIKE '이%' OR name LIKE '졜%';
WHERE name LIKE '__영';
WHERE name LIKE '_영%';
WHERE name LIKE '%영%';
WHERE name LIKE '%κΈΈ';
WHERE name LIKE 'κΉ€%'; -- κΉ€????????
WHERE name LIKE 'λ¬ΈμžνŒ¨ν„΄'; -- μ™€μΌλ“œμΉ΄λ“œ % _
WHERE SUBSTR( name, 0, 1 ) IN ( 'κΉ€', '이', '졜' );
WHERE SUBSTR( name, 0, 1 ) = 'κΉ€' OR SUBSTR( name, 0, 1 ) = '이';
WHERE SUBSTR( name, 0, 1 ) = 'κΉ€';

-- (문제) emp ν…Œμ΄λΈ” '1981' ~ '1985' μž…μ‚¬ν•œ 사원정보 쑰회.
SELECT ename, hiredate
FROM emp
WHERE SUBSTR( hiredate, 0, 2) BETWEEN 81 AND 85;
WHERE REGEXP_LIKE( hiredate, '^8[1-5]' ); -- '1981' ~ '1985'
WHERE REGEXP_LIKE (hiredate, '^(81|85)'); X

WHERE REGEXP_LIKE( hiredate, '^81' ); -- REGEXP_LIKEν•¨μˆ˜
WHERE hiredate LIKE '81%';

 
-- ( 문제 ) 사원λͺ… 속에 am, ak , ar λŒ€μ†Œλ¬Έμž ꡬ뢄없이 ν¬ν•¨λœ 사원 정보 쑰회.
-- LIKE, REGEXP_LIKE, κΈ°νƒ€μ—°μ‚°μž,ν•¨μˆ˜ μ‚¬μš©.~
SELECT ename , hiredate
FROM emp
WHERE REGEXP_LIKE( ename , 'a[mkr]' , 'i' );
WHERE REGEXP_LIKE( ename , 'am|ak|ar' , 'i' );

WHERE ename LIKE '%' || UPPER('am') || '%'; -- 이 ꡬ문 μ•”κΈ°.
WHERE LOWER(ename) LIKE '%am%' OR LOWER(ename) LIKE '%ak%' OR LOWER(ename) LIKE '%ar%';
WHERE ename LIKE '%am%' OR ename LIKE '%ak%' OR ename LIKE '%ar%'
OR ename LIKE '%AM%' OR ename LIKE '%AK%' OR ename LIKE '%AR%'
OR ename LIKE '%Am%' OR ename LIKE '%Ak%' OR ename LIKE '%Ar%'
OR ename LIKE '%aM%' OR ename LIKE '%aK%' OR ename LIKE '%aR%';
 
SELCECT first_name, last_name
FROM employees
WHERE REGEXP_LIKE (first_name, '^Ste(v|ph)en$')
ORDER BY last_name;​

 

-> 무쑰건 μ‹œμž‘, 끝 κ°™μ•„μ•Όν•˜κ³  v or ph


 
 
-- 37. emp ν…Œμ΄λΈ”μ—μ„œ 직속상사(mgr)κ°€ μ—†λŠ” μ‚¬μ›μ˜ 정보λ₯Ό 쑰회
SELECT *
FROM emp
WHERE mgr IS NULL;
WHERE mgr = null;


-- 38. empν…Œμ΄λΈ”μ—μ„œ 각 λΆ€μ„œλ³„λ‘œ μ˜€λ¦„μ°¨μˆœ 1μ°¨ μ •λ ¬ν•˜κ³  κΈ‰μ—¬(PAY)λ³„λ‘œ 2μ°¨ λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•΄μ„œ μ‘°νšŒν•˜λŠ” 쿼리 μž‘μ„±


-- 39. Alias λ₯Ό μž‘μ„±ν•˜λŠ” 3가지 방법을 μ μœΌμ„Έμš”.
SELECT deptno, ename
, sal + comm (γ„±) AS "PAY" " μŒλ”°μ˜΄ν‘œ
, sal + comm (γ„΄) "P AY"
, sal + comm (γ„·) P_AY
FROM emp;


"" μƒλž΅ν•  수 μ—†λŠ” 경우 ? 별칭에 곡백이 μžˆλŠ” κ²½μš°λŠ” μƒλž΅ X
 

-- 40. 였늘 배운 였라클의 SQL μ—°μ‚°μžλ₯Ό μ μœΌμ„Έμš”.
γ„±. ||
γ„΄. = != > < >= <= λΉ„κ΅μ—°μ‚°μž
AND OR NOT λ…Όλ¦¬μ—°μ‚°μž
[NOT] LIKE SQL μ—°μ‚°μž
[NOT] BETWEEN AND "
IS [NOT] NULL "
[NOT] IN ( list ) "

40-2) 였라클 ν•¨μˆ˜
UPPER(), LOWER() , INITCAP(), SUBSTR(), CONCAT()
TO_CHAR(), EXTRACT() λ‚ μ§œ -> λ…„,μ›”,일
NVL
NVL2
 
-- [ LIKE wildcardλ₯Ό 일반 문자처럼 μ“°κ³  싢은 κ²½μš°μ—λŠ” ESCAPE μ˜΅μ…˜μ„ μ‚¬μš© ]
7369 SMITH CLERK 7902 80/12/17 800 null 20
S_MITH
SELECT *
FROM emp
WHERE empno = 7369;
 
-- 사원λͺ… μˆ˜μ • : DML( insert, [update], delete ) + 컀밋, λ‘€λ°±
[UPDATE]
1. 일반적인 update
γ€ν˜•μ‹γ€‘
UPDATE ν…Œμ΄λΈ”λͺ…
SET 컬럼λͺ…= λ³€κ²½ν• κ°’[, 컬럼λͺ…= λ³€κ²½ν• κ°’,...]
[WHERE 쑰건];

SELECT SYSDATE --22/09/27 + μ‹œκ°„
FROM dual;

--
UPDATE emp
SET ename = 'S_MITH' --, hiredate = SYSDATE, sal = 100;
WHERE empno = 7369;
 
-- 14개 ν–‰ 이(κ°€) μ—…λ°μ΄νŠΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€. (WHERE쑰건이 μ—†μœΌλ©΄ λͺ¨λ“  μ‚¬μ›μ˜ 쑰건을 바꿔버림)
 
ROLLBACK;
SELECT *
FROM emp;
 
-- ( 문제 ) 사원이름 'S_' ν¬ν•¨λœ μ‚¬μ›μ˜ 정보 쑰회
-- LIKE μ—°μ‚°μž + ESCAPE μ˜΅μ…˜ μ„€λͺ…
 
SELECT ename, hiredate
FROM emp
WHERE ename LIKE '%S\_%' ESCAPE '\'; -- S_MITH 80/12/17
// μ—­μŠ¬λŸ¬μ‹œκ°€ 뢙은 μ™€μΌλ“œμΉ΄λ“œλŠ” μ™€μΌλ“œμΉ΄λ“œμ—μ„œ μ œμ™Έλœλ‹€λŠ”λœ»

-- SCOTT 87/04/19

2. μ„œλΈŒμΏΌλ¦¬λ₯Ό μ‚¬μš©ν•˜λŠ” update
γ€ν˜•μ‹γ€‘
UPDATE ν…Œμ΄λΈ”λͺ…1
SET (컬럼λͺ…1, 컬럼λͺ…2, ...) = (SELECT s_컬럼λͺ…1, s_컬럼λͺ…2,...
FROM ν…Œμ΄λΈ”λͺ…2
[WHERE 쑰건2])
[WHERE 쑰건];



μ—°μ‚°μž

 

  • 넀이버 λΈ”λŸ¬κ·Έ κ³΅μœ ν•˜κΈ°
  • 넀이버 λ°΄λ“œμ— κ³΅μœ ν•˜κΈ°
  • 페이슀뢁 κ³΅μœ ν•˜κΈ°
  • μΉ΄μΉ΄μ˜€μŠ€ν† λ¦¬ κ³΅μœ ν•˜κΈ°