SQL

[Oracle] flashback table, 날짜타입

ㄷㅐ장님 2022. 1. 5. 00:39

테이블 삭제 
drop table 테이블명; --recyclebin에서 확인 가능
drop table 테이블명 purge; --복원 불가
drop table 테이블명; --연관된 인덱스도 삭제

삭제한 테이블 확인 
show recyclebin
select * from user_recyclebin;

휴지통(recyclebin) 비우기 
purge recyclebin;

flashback table(유료프로그램)

: 삭제한 테이블을 복원하는 sql문

flashback table emp_2022 to before drop;

drop한 후에 기존 테이블과 동일한 이름이 존재할 때 : 

flashback table test1 to before drop rename to new_test1

recyclebin에 동일한 이름의 테이블이 있을 경우에는 가장 최근에 삭제한 테이블을 복원한다.

 

날짜타입

sysdate, systimestamp : 현재 오라클 서버의 날짜시간 정보

current_date,current_timestamp, localtimestamp : 현재 session의 날짜시간 정보

alter session set time_zone = '+9:00'; : 현재 접속한 지역의 timezone 수정

 

date : sysdate, current_date

timestamp(9): localtimestamp

timestamp with time zone : systimestamp, current_timestamp

 

timestamp with local time zone : 보는 지역에 따라 날짜시간이 자동 정규화 되는 날짜타입

interval year to month : 기간을 나타내는 날짜 타입. 년수, 개월수

to_yminterval('년수-개월수')

interval day to second : 기간을 나타내는 날짜 타입. 일수, 시분초.9자리

to_dsinterval('일수 시간')

 

날짜 + 숫자(일수) = 날짜

날짜 - 숫자(일수) = 날짜

날짜 + 시간 = 날짜,시간

날짜 - 날짜 = 숫자(일수)

날짜 + 날짜 = 오류

날짜 + interval year to month = 날짜

날짜 + interval day to second = 날짜, 시간

 

형변환

char -> date 형변환
select to_date('2022-01-04','yyyy-mm-dd') from dual;

char -> timestamp 형변환
select to_timestamp('2022-01-04 11:30:00','yyyy-mm-dd hh24:mi:ss') from dual;

char -> timestamp with time zone 형변환
select to_timestamp_tz('2022-01-04 11:30:00.123456789 +9:00','yyyy-mm-dd hh24:mi:ss.ff tzh:tzm')
from dual;

char -> interval year to month 형변환 to_yminterval('년수-개월수')
select add_months(sysdate,-120), sysdate - to_yminterval('10-00') from dual;

char -> interval day to second 형변환 to_dsinterval('일수 시간')
select localtimestamp + to_dsinterval('100 00:00:00') from dual;

[문제101]각 사원의 last_name, hire_date 및 근속 연수를 출력하는 query 를 작성합니다.

사원의 근속 연수가 5 년 이상인 경우 '5 years of service' 를 출력합니다.

사원의 근속 연수가 10 년 이상인 경우 '10 years of service' 를 출력합니다.

사원의 근속 연수가 15 년 이상인 경우 '15 years of service' 를 출력합니다.

어떠한 조건과도 일치하지 않을 경우 'maybe next year!'를 출력합니다.

단 근속 연수를 출력은 case, to_yminterval을 사용하세요.

 

① case, to_yminterval 이용

select last_name, hire_date, 
case
	when sysdate - to_yminterval('15-00') >= hire_date then '15 years of service'
	when sysdate - to_yminterval('10-00') >= hire_date then '10 years of service'
	when sysdate - to_yminterval('5-00') >= hire_date then '5 years of service'
else
	'maybe next year!'
end "AWARDS"
from employees;

② case, between_months 이용

select last_name, hire_date, 
case
	when months_between(sysdate,hire_date)/12 >= 15 then '15 years of service'
	when months_between(sysdate,hire_date)/12 >= 10 then '10 years of service'
	when months_between(sysdate,hire_date)/12 >= 5 then '5 years of service'
else
	'maybe next year!'
end "AWARDS"
from employees;