국비교육 22일차 : 오라클 기초
★★ 중요 ★★
5. select 컬럼명, 컬럼명
-컬럼명 대신에 *(아스테리크)을 쓰면 모든 컬럼을 뜻하는 것이다.
1. from 테이블명(또는 뷰명)
2. where 조건절
-where 조건절이 뜻하는 것은 해당 테이블명(또는 뷰명)에서 조건에 만족하는 행(row)을 메모리(RAM)에 로딩(퍼올리는 것) 해주는 것이다.
3. group by 절
4. having 그룹함수조건절
6. order by 절
select nvl(1,2), nvl(null,2),
nvl('비','장마'), nvl('null','장마')
from dual;
select nvl2(1,2,3), nvl2(null,2,3),
nvl2('비','장마','그만'), nvl2('null','장마','그만')
from dual;
select employee_id AS "사원번호" -- 별칭(별명) alias
, first_name "이름" -- 별칭(별명) alias에서 as는 생략가능
, last_name 성 -- 별칭(별명) alias에서 ""는 생략가능
, salary "기본 급여" -- 별칭(별명) alias에서 공백을 주고자 한다면
-- 반드시 "" 해주어야 한다.
, commission_pct "수당퍼센티지"
, NVL(Salary + (Salary * Commission_Pct),Salary) 월급
, NVL2(commission_pct, salary + (salary * commission_pct), salary) Month_Salary
-- "" 안쓰면 대소문자 상관 없는데 "" 쓰면 대소문자를 구분한다.
-- 그래서 "" 잘 사용하지 않고 띄어쓰기는 _ 이용한다.
, department_id 부서번호
from EMPLOYEES;
- 같다 =
- 같지않다 != <> ^=
- 크다. 작다 > <
- 같거나크다. 같거나작다 >= <=
- NULL 은 존재하지 않는 것이므로 비교대상이 될 수가 없다.
그러므로 비교연산( = != <> ^= > < >= <= )을 할수가 없다.
그래서 비교연산을 하려면 nvl()함수, nvl2()함수를 사용하여 처리한다.
desc employees; -- 테이블의 구조부터 확인한다.(null 값이 있는지 없는지 등)
select employee_id 사원번호
, first_name || ' ' || last_name 사원명
, nvl(salary + (salary * commission_pct), salary) 월급
, department_id 부서번호 -- 화면에 퍼 올릴 때는 원래 부서 번호를 부여한다.
from employees
where nvl(department_id, -9999) = -9999;
-- 또는
-- where department_id is null;
select employee_id, first_name, last_name, salary, department_id
from employees
order by salary asc; -- asc 생략 가능
select employee_id, first_name, last_name, salary, department_id
from employees
order by salary desc; -- desc 는 생략 불가능
-- order by 4 desc; -- salary가 4번째 컬럼이기 때문에 이렇게 쓸 수도 있다.
null의 정렬
정렬(오름차순, 내림차순)을 할 때 NULL은 존재하지 않는 것이므로 오라클에서는 NULL을 가장 큰 것으로 간주를 하고, 마이크로소프트사의 MS-SQL 에서는 NULL을 가장 작은 것으로 간주한다.
1차 정렬, 2차 정렬
-- employees 테이블에서 부서번호별 오름차순 정렬을 한 후에 동일한 부서번호내에서는
-- 월급의 내림차순으로 정렬하여 사원번호, 사원명, 월급, 부서번호를 나타내세요.
select employee_id
, first_name
, last_name
, nvl(salary + (salary * commission_pct), salary) as month_salary
, department_id
from employees
order by 5 asc, 4 desc;
-- order by 5, 4 desc; -- asc 생략 가능
-- 1차 정렬, 2차 정렬
select sysdate, current_date, localtimestamp, current_timestamp, systimestamp
from dual;
-- sysdate는 피씨의 시간 데이터
-- 22/06/30 22/06/30 22/06/30 15:27:54.040000000 22/06/30 15:27:54.040000000 ASIA/SEOUL 22/06/30 15:27:54.040000000 +09:00
select sysdate
, to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss')
, to_char(sysdate, 'yyyy-mm-dd hh:mi:ss')
, to_char(sysdate, 'yyyy-mm-dd am hh:mi:ss')
, to_char(sysdate, 'yyyy-mm-dd pm hh:mi:ss')
from dual;
create table tbl_test01
(no number(5) -- no는 컬럼명, -99999~99999
,name Nvarchar2(20) -- 20글자까지 가능
,input_day date default sysdate -- 특정 값을 주지 않으면 자동적으로 현재 날짜가 입력
);
-- Table TBL_TEST01이(가) 생성되었습니다.
insert into tbl_test01(no, name, input_day) values(1, '일번', sysdate);
-- 1 행 이(가) 삽입되었습니다.
insert into tbl_test01(name, no, input_day) values('이번', 2, sysdate);
insert into tbl_test01 values(3, '삼번', sysdate);
commit;
-- 커밋 완료.
select no, name, input_day, to_char(input_day, 'yyyy-mm-dd hh24:mi:ss')
from tbl_test01;
-- 테이블 출력해주기
update tbl_test01 set name = '3번이다'
where no = 3
-- 1 행 이(가) 업데이트되었습니다.
-- no가 3 인 애만 메모리에서 퍼 올려서 수정
commit;
select no as 번호
, name as 성명
, to_char(input_day, 'yyyy-mm-dd hh24:mi:ss') as 입력날짜
from tbl_test01;
delete from tbl_test01
where no = 3;
-- 1 행 이(가) 삭제되었습니다.
commit;
drop table tbl_test01 purge;
-- Table TBL_TEST01이(가) 삭제되었습니다.
벌써 수업 들은지 한달이 넘었다.. 나 잘하고 있는 거 겠지? 아직도 하나도 모르겠는 기분...