본문 바로가기
oracle

1026_시퀀스

by 신방동불주먹 2022. 10. 26.

시퀀스(sequence)

유일한 값을 생성해주는 오라클 객체, 자동으로 숫자 증가

보통 primary key로 사용할 수 있는 순차적으로 증가하는 숫자 컬럼 생성 시 사용

 

--create , drop
--nextval , currval

 

<생성>

 

create sequence 시퀀스명
start with 시작값 => 1(기본값)
increment by 증가치 =>1
maxvalue 최댓값 => 10의 1027
minvalue 최솟값  => 10의 -1027

 

increment by :시퀀스 증가 값. 생략 시 1씩 증가

start with : 시퀀스 시작 값. 생략 시 1부터 시작

maxvalue : 시퀀스가 증가할 수 있는 최대값 

min value : 시퀀스의 최소값 지정 ,기본값 1

 

create sequence dept_deptno_seq
increment by 10
start with 10;


<삭제>

 

drop sequence dept_deptno_seq;

 

 

 

<생성된 시퀀스 정보보기>

select * from user_sequeneces;

 

select dept_deptno_seq.nextval
from dual;

select dept_deptno_seq.currval  from dual;

--=========================
drop sequence emp_seq;

create sequence emp_seq
increment by 1
start with 1
maxvalue 1000;

drop table emp01;

create table emp01
as
select empno,ename,hiredate from emp
where 1 != 1;

select * from emp01;

insert into emp01
values(emp_seq.nextval,'hong',sysdate);  


drop table  product;

create table product(
                                        pid varchar2(10),
                                        pname varchar2(10),
                                        price number(5),
                                        
                                        constraint product_pid_pk primary key(pid)
                                     );


create sequence idx_product_id
start with 1000;

insert into product
values('pid'|| idx_product_id.nextval,'치즈',1000); 
select * from product;

drop sequence idx_product_id;

--p392 end

currval : 현재 시퀀스 값을 반환

nextval : 현재 시퀀스값의 다음 값을 반환

.

 

 

'oracle' 카테고리의 다른 글

1027_저장 프로시저  (0) 2022.10.30
1026_PL/SQL(변수, 조건문, 반복문)  (0) 2022.10.27
1024_제약조건(foreign key, check, default)  (0) 2022.10.26
뷰 (View)  (0) 2022.10.26
1019_날짜함수  (1) 2022.10.26