설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 94건, 최근 0 건
   

시퀀스 사용하기

글쓴이 : PostgresDBA 날짜 : 2012-12-15 (토) 14:32 조회 : 19153
시퀀스를 사용해보겠습니다. 
PostgreSQL 에서는 오라클과 달리 시퀀스 초기화를 아주 쉽게 할수 있습니다.

scott@[local]:5432 scottdb#SQL> create sequence scott_seq;
CREATE SEQUENCE
Time: 234.327 ms
scott@[local]:5432 scottdb#SQL> select currval('scott_seq'); 
ERROR:  currval of sequence "scott_seq" is not yet defined in this session
Time: 65.701 ms
scott@[local]:5432 scottdb#SQL> select nextval('scott_seq');
 nextval 
---------
       1
(1 row)

Time: 52.630 ms
scott@[local]:5432 scottdb#SQL> select currval('scott_seq');
 currval 
---------
       1
(1 row)

Time: 0.225 ms
scott@[local]:5432 scottdb#SQL> select nextval('scott_seq');
 nextval 
---------
       2
(1 row)

Time: 0.312 ms
scott@[local]:5432 scottdb#SQL> select setval('scott_seq',90,false);  ## nextval 이 90 부터 시작하도록 초기화
 setval 
--------
     90
(1 row)

Time: 349.783 ms
scott@[local]:5432 scottdb#SQL> select nextval('scott_seq');
 nextval 
---------
      90
(1 row)

Time: 11.257 ms
scott@[local]:5432 scottdb#SQL> select nextval('scott_seq');
 nextval 
---------
      91
(1 row)

Time: 0.369 ms
scott@[local]:5432 scottdb#SQL> select setval('scott_seq',90,true);  ## nextval 이 90 다음값인 91 부터 시작하도록 초기화
 setval 
--------
     90
(1 row)

Time: 10.846 ms
scott@[local]:5432 scottdb#SQL> select nextval('scott_seq');
 nextval 
---------
      91
(1 row)

Time: 10.964 ms
scott@[local]:5432 scottdb#SQL> 

   

postgresdba.com