본문 바로가기
DB/SQLPLUS

[ORACLE] SEQUNCE(시퀀스)

by KhyeonS 2022. 6. 20.

이번엔 시퀀스에 대해서 알아 볼 것이다.


시퀀스(Sequence)
  시퀀스는 연속적으로 입력되는 데이터에 어떤 순서 값을 부여할 때 사용된다. 예를 들어 가입하는 신규 회원을 식별하는 식별자를 회원 테이블에서 자동으로 회원번호를 매기는 경우이다. 일반적으로 Primary Key가 적용된 컬럼에 순서적으로 번호를 부여할 때 시퀀스를 주로 사용한다. 일련 번호지만 한 자리 건너, 중간에 새로운 번호로 시작 등으로 해줄 수 있다. 'CREATE SEQUENCE 시퀀스_명'식으로 시퀀스를 생성해준다. 
▪ NEXTVAL을 써주면 자동으로 다음 번호가 매겨지고, 
▪ INCREMENT BY 뒤에 증가_값을 지정할 수 있는데 디폴트 증가_값은 1이다. 
▪ START WITH로 디폴트 시작_값은 1부터지만 시작_값이 1이 아니게 지정할 수도 있다 등의 옵션을 사용할 수 있다.  

  사용자와 롤(Role), 권한 위임
  이제 사용자 계정 생성과 계정 묶기, 권한 주기 등을 알아보자. 여기서는 특히 롤을 알아보는데 사용자들이 가질 수 있는 권한들을 묶어서 여러 롤로 만들어 두고, 각 롤에 사용자를 넣어서, 해당 롤에 들어 있는 사용자들은 해당 롤에 주어진 권한만 사용하게 해주는 것을 말한다. 
→Windows AD(Active Directory)에서 그룹을 만들고 그룹에 권한을 준 뒤 그룹에 사용자를 넣어서 해당 사용자들이 해당 그룹에 주어진 권한을 사용하는 것과 동일한 개념이다. 
→Linux/UNIX에서도 그룸을 생성한 뒤, 사용자를 넣고, 그룹이 할 수 있는 권한을 부여하면, 해당 그룹에 속한 사용자들은 해당 그룹이 가지고 있는 권한을 사용할 수 있다. 
Oracle에서는 사용자를 Role-based로 관리한다고 한다.


●예제

●create table tasks (id number primary key, title varchar2(20) not null); 

create table tasks (id number primary key, title varchar2(20) not null);



●create sequence task_id_seq
increment by 3
start with 10
minvalue 10
maxvalue 100
cycle;

create sequence task_id_seq
increment by 3
start with 10
minvalue 10
maxvalue 100
cycle;




●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT MAN');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT SALES');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT DEVELOP');

insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT MAN');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT SALES');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT DEVELOP');



●create sequence dept_id_seq
start with 20
increment by 3;

●insert into tasks values(tasks_id_seq.NEXTVAL, 'IT DEVELOP');
●insert into tasks  values(tasks_id_seq.NEXTVAL, 'IT MAN');
●insert into tasks  values(tasks_id_seq.NEXTVAL, 'IT PROG');

create sequence dept_id_seq
start with 20
increment by 3;

insert into tasks values(tasks_id_seq.NEXTVAL, 'IT DEVELOP');
insert into tasks  values(tasks_id_seq.NEXTVAL, 'IT MAN');
insert into tasks  values(tasks_id_seq.NEXTVAL, 'IT PROG');




● create table tasks (id number primary key, title varchar2(20) not null);
●create sequence tasks_id_seq
increment by 3
start with 10
minvalue 10
maxvalue 100
cycle;  

create table tasks (id number primary key, title varchar2(20) not null);

create sequence tasks_id_seq
increment by 3
start with 10
minvalue 10
maxvalue 100
cycle;

 

●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT MAN');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT SALES');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
●insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT DEVELOP');

insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');

insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT MAN');

insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT SALES');

insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');

insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT DEVELOP');

 

●create sequence dept_id_seq
start with 20
increment by 3; 

 

●insert into tasks values(dept_id_seq.NEXTVAL, 'IT DEVELOP');
●insert into tasks values(dept_id_seq.NEXTVAL, 'IT MAN');
●insert into tasks values(dept_id_seq.NEXTVAL, 'IT PROG'); */

create sequence dept_id_seq
start with 20
increment by 3; 

insert into tasks values(dept_id_seq.NEXTVAL, 'IT DEVELOP');

insert into tasks values(dept_id_seq.NEXTVAL, 'IT MAN');

insert into tasks values(dept_id_seq.NEXTVAL, 'IT PROG');


●select id, title from tasks;

● select * from user_sequences
where sequence_name = 'DEPT_ID_SEQ'; 

●select dept_id_seq.CURRVAL from dual;// 설정한 시퀀스의 현재값(currval)

●insert into tasks
values (dept_id_seq.NEXTVAL, 'IT Sales');
rollback;//시퀀스 취소 가능

select id, title from tasks;

select * from user_sequences
where sequence_name = 'DEPT_ID_SEQ'; 

select dept_id_seq.CURRVAL from dual;// 설정한 시퀀스의 현재값(currval)

insert into tasks
values (dept_id_seq.NEXTVAL, 'IT Sales');
rollback;//시퀀스 취소 가능

'DB > SQLPLUS' 카테고리의 다른 글

[ORACLE] Sysnonym(동의어)  (0) 2022.06.20
[ORACLE] ROLE(롤)  (0) 2022.06.20
[ORACLE] INDEX(인덱스)  (0) 2022.06.20
[ORACLE] VIEW(뷰)  (0) 2022.06.20
[Oracle] 서브 쿼리(SUB QUERY)  (0) 2022.06.17

댓글