1. 뷰(view)
- 테이블처럼 조회 가능한 객체
- 물리적으로 실제 저장된 공간이 있지는 x
- create view 권한을 가진자만이 생성 가능
1) 생성
create [or replace] view 뷰이름
as
select ....;
2) 삭제
drop view 뷰이름;
3) 조회
select *
from dba_views
where owner = 'SCOTT';
** DBA_VIEW 뷰를 사용하여 SYSTEM 뷰 찾을 수 있음
select *
from dba_views
where view_name like 'DBA_%TAB%';
** dictionary view 종류
1) static dictionary view : 객체 현황 조회
- dba_XXX : 전체 객체
- all_XXX : 접속한 계정 소유 + 권한을 가진 객체
- user_XXX : 접속한 계정 소유 객체
예)
select * from dba_tables;
select * from dba_tab_columns;
select * from dba_constraints;
select * from dba_cons_columns;
select * from dba_views;
2) dynamic performance view : 실시간 정보(성능과 관련된 데이터 확인 목적 : 세션, 락현황, 메모리 사용률)
- v$XXXX
select * from v$parameter;
select * from v$session;
select * from v$sql;
예제) VIEW 생성(view와 table 차이)
system 계정으로 접속)
grant dba to scott;
scott 계정으로 재접속)
create table student_exam01
as
select s.studno, s.name, s.grade, e.total
from student s, exam_01 e
where s.studno = e.studno;
create view view_student_exam01
as
select s.studno, s.name, s.grade, e.total
from student s, exam_01 e
where s.studno = e.studno;
select * from student_exam01; -- 테이블을 통한 조회
select * from view_student_exam01; -- view를 통한 조회
insert into student(studno, name, id, jumin) values(9999,'홍길동','abcd','1111111111111');
insert into exam_01 values(9999,100);
commit;
select * from student_exam01; -- 테이블을 통한 조회(새로운 데이터는 조회 되지 X)
select * from view_student_exam01; -- view를 통한 조회(새로운 데이터 조회 가능)
** 뷰 수정
create or replace view view_student_exam01
as
select s.studno, s.name, s.grade, s.jumin, e.total
from student s, exam_01 e
where s.studno = e.studno;
2. 시퀀스(sequence)
- 주문번호, 게시글번호, 송장번호처럼 자동 증가하는 숫자를 부여하는 객체
- 채번 프로세스 중 하나
1) 생성
create sequence 시퀀스명
[start with n] -- 시작값(default:1)
[increment by n] -- 증가값(default:1, 음수가능)
[maxvalue n] -- 최대번호 또는 재시작값(default : 999999999)
[minvalue n] -- 최소번호 또는 재시작값(default 1)
[cycle | nocycle] -- 재사용여부(default:nocycle)
[cache n] -- 캐시사이즈(default:20)
;
** 증가하는 시퀀스의 경우 maxvalue까지 도달 가능, cycle 옵션 사용 시 minvalue부터 재시작함(start with값보다 큰 값 불가)
** 감소하는 시퀀스의 경우 minvalue까지 도달 가능, cycle 옵션 사용 시 maxvalue부터 재시작함(start with값보다 작은 값 불가)
2) 삭제
drop sequence 시퀀스명;
3) 조회
select *
from dba_sequences;
예제) 시퀀스를 통한 자동 번호 부여(scott 계정으로 시도)
테이블 생성)
create table jumun2
(
jno number(4) primary key, -- 주문번호
pno number(4) not null, -- 상품번호(1~10)
gno number(4) not null, -- 고객번호(100~200)
qty number not null, -- 수량
jdate date default sysdate -- 주문날짜
);
시퀀스 생성)
create sequence seq_jumun2
start with 1000
maxvalue 1010;
create sequence seq_jumun3
start with 1000
maxvalue 1010;
시퀀스를 통한 주문번호 생성 및 데이터 입력)
insert into jumun2 values(seq_jumun2.nextval, 2, 100, 5, sysdate);
insert into jumun2 values(seq_jumun2.nextval, 10, 101, 10, sysdate);
insert into jumun2 values(seq_jumun2.nextval, 5, 102, 1, sysdate);
select * from jumun2; -- 1001번부터 시작
rollback; -- rollback 하더라도 이미 증가한 시퀀스는 재사용되지 X
insert into jumun2 values(seq_jumun3.nextval, 2, 100, 5, sysdate);
select * from jumun2; -- 1000번부터 시작(이미 테이블에 저장공간이 할당되었기 때문에)
** 11g new feature : deferred segment creation
테이블 생성 시 세그먼트 디스크 공간을 즉시 할당하지 않고, 디스크 공간을 절약하기 위해 세그먼트 생성을 잠시 연기함
최초 insert 시도 시 세그먼트는 할당됨(단, system 소유 테이블은 적용되지 X)
예제) 시퀀스를 통한 자동 번호 부여(scott 계정으로 시도, cycle 옵션)
create table jumun4
(
jno number(4) primary key, -- 주문번호
pno number(4) not null, -- 상품번호(1~10)
gno number(4) not null, -- 고객번호(100~200)
qty number not null, -- 수량
jdate date default sysdate -- 주문날짜
);
시퀀스 생성)
create sequence seq_jumun4
start with 1000
maxvalue 1010
cycle;
데이터 입력)
insert into jumun4 values(seq_jumun4.nextval, 2, 100, 5, sysdate);
select * from jumun4;
commit;
'SQL' 카테고리의 다른 글
25. 기타 오브젝트(3) (0) | 2024.11.01 |
---|---|
24. 기타 오브젝트(2) (1) | 2024.10.31 |
22. 테이블의 내용 추가, 수정, 삭제 : DML(2) (3) | 2024.10.29 |
21. 테이블의 내용 추가, 수정, 삭제 : DML(1) (1) | 2024.10.28 |
20. 테이블 구조 생성, 변경, 제거 : DDL (2) (1) | 2024.10.28 |