How do I reset a sequence in Oracle?

后端 未结 18 1282
天命终不由人
天命终不由人 2020-11-22 05:09

In PostgreSQL, I can do something like this:

ALTER SEQUENCE serial RESTART WITH 0;

Is there an Oracle equivalent?

18条回答
  •  逝去的感伤
    2020-11-22 05:35

    1) Suppose you create a SEQUENCE like shown below:

    CREATE SEQUENCE TESTSEQ
    INCREMENT BY 1
    MINVALUE 1
    MAXVALUE 500
    NOCACHE
    NOCYCLE
    NOORDER
    

    2) Now you fetch values from SEQUENCE. Lets say I have fetched four times as shown below.

    SELECT TESTSEQ.NEXTVAL FROM dual
    SELECT TESTSEQ.NEXTVAL FROM dual
    SELECT TESTSEQ.NEXTVAL FROM dual
    SELECT TESTSEQ.NEXTVAL FROM dual
    

    3) After executing above four commands the value of the SEQUENCE will be 4. Now suppose I have reset the value of the SEQUENCE to 1 again. The follow the following steps. Follow all the steps in the same order as shown below:

    1. ALTER SEQUENCE TESTSEQ INCREMENT BY -3;
    2. SELECT TESTSEQ.NEXTVAL FROM dual
    3. ALTER SEQUENCE TESTSEQ INCREMENT BY 1;
    4. SELECT TESTSEQ.NEXTVAL FROM dual

提交回复
热议问题