Need to reset the value of sequence in Oracle

后端 未结 4 2073
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 13:45

I\'m working with Spring and Hibernate to develop web applications in Java. Let\'s assume that I have a table. When I delete some records from this table, sometimes I need t

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 14:26

    This trick works for me. In my sequence (USERSUTPL_USERUTPL_SQ) the last number was 53. The last id in my table was 83

    SELECT MAX(ID) FROM USERSUTPL_USERUTPL
    

    Then 83 - 53 = 31. So:

    ALTER SEQUENCE USERSUTPL_USERUTPL_SQ INCREMENT BY +31;
    SELECT USERSUTPL_USERUTPL_SQ.NEXTVAL FROM dual;
    ALTER SEQUENCE USERSUTPL_USERUTPL_SQ INCREMENT BY 1;
    

    And its changes the last number. :D

提交回复
热议问题