-
oracle 시퀀스DataBase/oracle 2020. 3. 22. 01:50123456789101112131415161718192021222324252627282930313233343536373839/* 시퀀스 : 연속적인 번호를 만들어주는 기능create sequence 시퀀스이름increment by n : 증가값을 설정 ex) n이 1이면 1씩 증가 / 기본값은 1start with n : 시작값 설정 / 기본값은 1maxvalue n | nomaxvalue : 시퀀스 최대값minvalue n | nominvalue : 시퀀스 최소값 : cycle 옵션일 경우 시작값cycle | nocycle : 시퀀스를 순환 사용할 지 설정cache n | nocache : 시퀀스의 속도를 개선하기위해 캐싱여부 지정*/--1) 시퀀스 생성 : 제품번호를 생성하는 시퀀스 만들기create sequence seq_serial_noincrement by 1start with 100maxvalue 110minvalue 99cyclecache 2;create table good(good_no number(3),good_name varchar2(10));insert into goodvalues(seq_serial_no.nextval, '제품1'); -- 나는 100번 출력됐는데 인강은 101번 출력됨..뭐지..-_-select * from good;insert into good values(seq_serial_no.currval, '제품1');insert into good values(seq_serial_no.nextval, '제품2');-- 시퀀스이름.currval : 시퀀스 현재값-- 시퀀스이름.nextval : 다음 시퀀스 값select seq_serial_no.currval from dual;-- 시퀀스 110번까지 모두 소진하면 99번으로 다시 돌아감 <-- cycle 명령어로 순환을 가능하게 했기때문에!1234567891011121314151617create table good2(good_no number(3),good_name varchar2(10));create sequence seq_serial_no2increment by 1start with 100maxvalue 105cache 2;insert into good2 values(seq_serial_no2.nextval, '제품1');commit;select * from good2;http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
1) 새로운 테이블과 시퀀스를 만들고
2) 데이터 하나를 입력 후
3) system 계정으로 들어가서 shutdown 시킨 후 startup 한 다음
4) 다시 새로운 데이터를 입력하면!
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647SQL> select * from good2;GOOD_NO GOOD_NAME---------- --------------------100 제품1SQL> SYSTEM 접속Connected.SQL>SQL>SQL>SQL> shutdown abort;ORACLE instance shut down.SQL> startupORACLE instance started.Total System Global Area 1068937216 bytesFixed Size 2260048 bytesVariable Size 796918704 bytesDatabase Buffers 264241152 bytesRedo Buffers 5517312 bytesDatabase mounted.Database opened.SQL>SQL> HR접속Connected.SQL>SQL>SQL> select * from good22 ;GOOD_NO GOOD_NAME---------- --------------------100 제품1SQL> insert into good2 values(seq_serial_no2.nextval, 'good2');1 row created.SQL> select * from good2;GOOD_NO GOOD_NAME---------- --------------------100 제품1102 good2SQL>http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter5) cache에 다음값이 101로 설정이 되어있었는데 shutdown으로 초기화시켰기때문에 seq_serial_no2 의 nextval이 102가 되었다...!!?? <-- 이해안감...이건 책을 더 찾아봐야겠다
** 서버 다운시키는 명령어 : shutdown abort;
** 서버 기동시키는 명령어 : startup
** sequence 는 생성또는 드랍시키거나 nextval로 증가시키는것만 해봤는데 다른 설정값들 주는게 흥미로웠다!
'DataBase > oracle' 카테고리의 다른 글
oracle PL/SQL_개념 (0) 2020.03.22 oracle 계층형 쿼리 (0) 2020.03.22 oracle 뷰 (0) 2020.03.21 oracle 제약조건 (0) 2020.03.21 oracle DML (0) 2020.03.21