[DB] ORACLE - 1์ผ
in DB
ORACLE
์ค๋ผํด 1์ผ
OracleXE112_Win64.zip
์์ถ ํ๊ณ ์ค์นํ๋ค
1. ์น : http://localhost:8080/apex
ID๋ช
: system / pw๋ช
: oracle โ๊ด๋ฆฌ์๊ณ์
ID๋ช
: scott / pw๋ช
: tiger โ์ฌ์ฉ์๊ณ์
ID๋ช
: hr / pw๋ช
: hr โ์ฌ์ฉ์๊ณ์
SID๋ช : xe
2. ์ฝ์ : cmd ์ฐฝ์์
C:\> sqlplus hr/hr
3. ์ค๋ผํด์ด ๊ตฌ๋๋์ง ์์ ๋ ํ์ธํ ๊ฒ
๊ฐ. ์์โ์ค์ โ์ ์ดํโ๊ด๋ฆฌ๋๊ตฌโ์๋น์ค
๋. ์ปดํจํฐโ๊ด๋ฆฌโ์๋น์ค
OracleServiceXE ์์๋จ
OracleXETNSListener ์์๋จ
C:\oraclexe\app\oracle\product\10.2.0\server\network\ADMIN
listener.ora
tnsnames.ora
select * from tab; โํ
์ด๋ธ ๋ชฉ๋กํ์ธ
desc employees; โdescribe์ ์ฝ์(ํ
์ด๋ธ ๊ตฌ์กฐํ์ธ)
select * from employees;
select * from departments;
select * from jobs;
โ
SQL๋ฌธ
- ๋ฐ์ดํฐ ์กฐ์์ด (DML : Data Manipulation Language)
- insert, update, delete, merge
- ๋ฐ์ดํฐ ์ ์์ด (DDL : Data Definition Language)
- create, alter, drop, rename, truncate
- ๋ฐ์ดํ๊ฒ์
- select
- ํธ๋์ ์
์ ์ด
- commit, rollback, savepoint
- ๋ฐ์ดํฐ ์ ์ด์ด (DCL : Data Control Language)
- grant, reboke
โป select
[ํ์]
select [distinct] [์ปฌ๋ผ1,์ปฌ๋ผ2,โฆ..][as ๋ณ๋ช ][ | ย | ์ฐ์ฐ์][*] |
from ํ ์ด๋ธ๋ช
[where ์กฐ๊ฑด์ ]
distinct : ์ค๋ณต์ ๊ฑฐ
* : ๋ชจ๋
์กฐ๊ฑด์ : and,or,like,in,between and,is null,is not null
ex1 ) employeesํ ์ด๋ธ์ ๋ชจ๋ ์ฌ์์ ์ฌ์๋ฒํธ,์ด๋ฆ(last_name),๊ธ์ฌ ๊ฒ์
select employee_id, last_name, salary from employees;
ex2 ) ๋ณ๋ช ๋ถ์ด๊ธฐ(as๋ ์๋ต๊ฐ๋ฅ)
employeesํ ์ด๋ธ์ ๋ชจ๋ ์ฌ์์ ์ฌ์๋ฒํธ,์ด๋ฆ(last_name),๊ธ์ฌ ๊ฒ์
์กฐ๊ฑด) title ์ฌ์๋ฒํธ, ์ด๋ฆ ,๊ธ์ฌ๋ก ์ถ๋ ฅํ ๊ฒ
select employee_id as ์ฌ์๋ฒํธ , last_name as "์ด ๋ฆ", salary as "๊ธ ์ฌ" from employees;
โ //๊ณต๋ฐฑ์ผ๋ก ์ธํด โโ์ฌ์ฉ(์ธ๋ถ๋ก ๋ณด์ด๋๊ฒ)
โ //Data ๋ถ๋ถ์ โ โ Single
ex3 ) employeeํ ์ด๋ธ์์ ์ฌ์๋ฒํธ,์ด๋ฆ,์ฐ๋ด์ ๊ตฌํ์์ค
์กฐ๊ฑด1) ์ฐ๋ด = ๊ธ์ฌ * 12
์กฐ๊ฑด2) ์ ๋ชฉ์ ์ฌ์๋ฒํธ, ์ด๋ฆ, ์ฐ๋ด์ผ๋ก ์ถ๋ ฅ
select employee_id as ์ฌ์๋ฒํธ , last_name as "์ด ๋ฆ", salary*12 as "์ฐ ๋ด"
from employees;
ex4 ) ์ฐ๊ฒฐ์ฐ์ฐ์ ์ปฌ๋ผ์ ์ฐ๊ฒฐํด์ ์ถ๋ ฅ
frist_name๊ณผ last_name์ ์ฐ๊ฒฐํด์ ์ถ๋ ฅํ์์ค
์ด ๋ฆ
------------ ์ฐ๊ฒฐ ์ฐ์ฐ์ ( || )
Ellen Abel
select first_name||' '||last_name as "์ด ๋ฆ" from employees;
[๋ฌธ์ 1] ๋ค์์ฒ๋ผ ์ถ๋ ฅํ์์ค
์ฌ์๋ฒํธ ์ด ๋ฆ ์ฐ ๋ด
----------------------------
100 Steven King 288000๋ฌ๋ฌ
[๋ฌธ์ 2] ๋ค์์ฒ๋ผ ์ถ๋ ฅํ์์ค (last_name, job_id)
Employee Detail
--------------------
King is a SA_REP
King is a AD_PRES
ex5 ) distinct(์ค๋ณต์ ๊ฑฐ)
employees ํ ์ด๋ธ์์ ๋ถ์ID๋ฅผ ์ถ๋ ฅํ์์ค
select distinct department_id from employees;
ex6 ) 10๋ฒ๋ถ์ ๋๋ 90๋ฒ๋ถ์ ์ฌ์๋ค์ ์ด๋ฆ, ์ ์ฌ์ผ, ๋ถ์ID๋ฅผ ์ถ๋ ฅํ์์ค
select last_name, hire_date, department_id
from employees
where department_id=10 or department_id=90;
ex7 ) ๊ธ์ฌ๊ฐ 2500์ด์ 3500๋ฏธ๋ง์ธ ์ฌ์์ ์ด๋ฆ(last), ์ ์ฌ์ผ, ๊ธ์ฌ๋ฅผ ๊ฒ์ํ์์ค
select last_name, hire_date, salary
from employees
where salary>=2500 and salary<3500;
[๋ฌธ์ 3] ๊ธ์ฌ๊ฐ 2500์ดํ ์ด๊ฑฐ๋ 3000์ด์์ด๋ฉด์ 90๋ฒ ๋ถ์์ธ
์ฌ์์ ์ด๋ฆ, ๊ธ์ฌ, ๋ถ์ID๋ฅผ ์ถ๋ ฅํ์์ค.
์กฐ๊ฑด1) ์ ๋ชฉ์ ์ฌ์๋ช , ์ ๊ธ, ๋ถ์์ฝ๋๋ก ํ์์ค
์กฐ๊ฑด2) ๊ธ์ฌ์์ $๋ฅผ ๋ถ์ด์์ค
์กฐ๊ฑด3) ์ฌ์๋ช ์ first_name๊ณผ last_name์ ์ฐ๊ฒฐํด์ ์ถ๋ ฅํ์์ค
ex8 ) โKingโ์ฌ์์ ๋ชจ๋ ์ปฌ๋ผ์ ํ์ํ์์ค
select * from employees where last_name='King';
โ ๋ฌธ์์ด ๊ฒ์ํ ๋๋ ๋,์๋ฌธ์๋ฅผ ๊ตฌ๋ถ
like : ๋ฌธ์๋ฅผ ํฌํจ
โ%dโ - d๋ก ๋๋๋
โa%โ - a๋ก ์์ํ๋
- โ%test% - test๊ฐ ํฌํจ๋์ด์๋
- โ_a%โ - ๋๋ฒ์งธ ๊ธ์๊ฐ a๋ก ์์ํ๊ณ ๋๋จธ์ง๋ ๋ฌด์
- โ__a%โ - ์ธ๋ฒ์งธ ๊ธ์๊ฐ a๋ก ์์ํ๊ณ ๋๋จธ์ง๋ ๋ฌด์
ex9 ) ์ ๋ฌดID์ MAN์ด ํฌํจ๋์ด์๋ ์ฌ์๋ค์ ์ด๋ฆ,์ ๋ฌดID,๋ถ์ID๋ฅผ ์ถ๋ ฅํ์์ค
select last_name, job_id, department_id
from employees
where job_id like '%MAN%';
ex10 ) ์ ๋ฌดID๊ฐ IT๋ก ์์ํ๋ ์ฌ์๋ค์ ์ด๋ฆ, ์ ๋ฌดID, ๋ถ์ID๋ฅผ ์ถ๋ ฅํ์์ค
select last_name, job_id, department_id
from employees
where job_id like 'IT%';
ex11 ) is null / is not null
์ปค๋ฏธ์ ์ ๋ฐ๋ ์ฌ์๋ค์ ์ด๋ฆ๊ณผ ๊ธ์ฌ, ์ปค๋ฏธ์ ์ ์ถ๋ ฅํ์์ค
select last_name, salary, commission_pct
from employees
where commission_pct is not null;
์ปค๋ฏธ์ ์ ๋ฐ์ง ์๋ ์ฌ์๋ค์ ์ด๋ฆ๊ณผ ๊ธ์ฌ, ์ปค๋ฏธ์ ์ ์ถ๋ ฅํ์์ค
select last_name, salary, commission_pct
from employees
where commission_pct is null;
ex12 ) in์ฐ์ฐ์(or์ฐ์ฐ์์ ๋ค๋ฅธํํ)
์ ๋ฌดID๊ฐ FI_MGR์ด๊ฑฐ๋ FI_ACCOUNT์ธ ์ฌ์๋ค์ ์ฌ์๋ฒํธ,์ด๋ฆ,์ง๋ฌด๋ฅผ ์ถ๋ ฅํ์์ค
select employee_id, last_name, job_id
from employees
where job_id='FI_MGR' or job_id='FI_ACCOUNT';
select employee_id, last_name, job_id
from employees
where job_id in('FI_MGR', 'FI_ACCOUNT');
ex13 ) between์ฐ์ฐ์(and์ฐ์ฐ์์ ๋ค๋ฅธ ํํ) : ์ด๊ณผ,๋ฏธ๋ง์์๋ ์ฌ์ฉํ ์ ์๋ค
๊ธ์ฌ๊ฐ 10000์ด์ 20000์ดํ์ธ ์ฌ์์ ์ฌ์๋ฒํธ,์ด๋ฆ,๊ธ์ฌ๋ฅผ ์ถ๋ ฅํ์์ค
select employee_id, last_name, salary
from employees
where salary>=10000 and salary<=20000;
select employee_id, last_name, salary
from employees
where salary between 10000 and 20000;
[๋ฌธ์ 4] ์ ๋ฌดID๊ฐ โSA_REPโ ์ด๊ฑฐ๋ โAD_PRESโ ์ด๋ฉด์ ๊ธ์ฌ๊ฐ 15000๋ฅผ
์ด๊ณผํ๋ ์ฌ์๋ค์ ์ด๋ฆ,์ ๋ฌดID,๊ธ์ฌ๋ฅผ ์ถ๋ ฅํ์์ค
์ด๋ฆ ์
๋ฌดID ๊ธ ์ฌ
-----------------------------
King AD_PRES 24000์
[๋ฌธ์ 5] Employeesํ ์ด๋ธ์ ์ ๋ฌดID๊ฐ ์ค๋ณต๋์ง ์๊ฒ ํ์ํ๋ ์ง์๋ฅผ ์์ฑํ์์ค
JOB_ID
-----------
AC_ACCOUNT
AC_MGR
: