스터디/DB(ORACLE) ORACLE 시퀀스(sequence) 고무곰(GomuGom) 2017. 9. 8. 13:59 --[ 시퀀스(sequence) ] -- : mysql에서 auto_increment 를 대체하는 기능으로 값들이 자동으로 순차적으로 증가하여야하는 -- 데이터 등에 주로 사용된다. -- 연속적인 번호를 만들어 주는 기능 --사용Ex) -- 구문 형식 ] /* create sequence (시퀀스 이름) increment by n -> 시퀀스 번호의 증가값을 설정한다.(default 1) start with n -> 시작값 설정(default 1) maxvalue n | nomaxvalue <- 시퀀스 최대값 설정 minvalue n | nominvalue <- 시퀀스 최소값 설정 : cycle 옵션일 경우 시작값 cycle | nocycle <- 최대값을 지나면 최소값으로 순환되는 옵션 설정 cache n | nocache <- 시퀀스의 속도를 개선하기위해 캐싱 여부 지정 */ -- [ 시퀀스 생성 ] : 제품번호를 생성하는 시퀀스 만들기 create SEQUENCE seq_serial_no INCREMENT BY 1 START WITH 100 MAXVALUE 110 MINVALUE 99 CYCLE cache 2; create table good( good_no number(3), good_name varchar2(20) ); -- 시퀀스를 이용해 삽입 : 시퀀스명.nextval : 다음 시퀀스 번호를 가져옴 -- : 시퀀스명.currval : 현재 시퀀스 번호를 가져옴 insert into good values(seq_serial_no.nextval, '제품1'); select * from good; select seq_serial_no.CURRVAL from dual; insert into good values(seq_serial_no.nextval, '제품2'); insert into good values(seq_serial_no.currval, '제품3'); -- 시퀀스 삭제 -- drop sequence 시퀀스명 drop sequence seq_serial_no; create sequence seq_serial_no2 INCREMENT BY 1 start with 100 maxvalue 105 cache 2; insert into good values(seq_serial_no2.nextval,'제품4'); select * from good; 저작자표시 비영리