日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql seq 重置_需要在Oracle中重置序列的值

發(fā)布時間:2025/4/16 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql seq 重置_需要在Oracle中重置序列的值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

小編典典

如果使用該值,則不應重置該值的原因:

如果你有20條記錄并刪除5-10條記錄會怎樣?中間有一個縫隙,無法重新設置序列。序列永遠不會生成無間隙的數字序列,即完美的1、2 .. n。

如果你調用.nextval并且不使用該值,它就消失了。你要刪除并重新創(chuàng)建序列嗎?如果開始插入并取消插入,Oracle 將回滾所做的操作,這些值將消失。如果你設置了該選項,nocache則差距將較小,但會降低性能。這值得么?

你的緩存應設置為希望在所有會話中一次執(zhí)行的插入次數,以避免任何性能問題。序列旨在提供一種非??焖?#xff0c;可擴展的方式來創(chuàng)建代理密鑰,而無需任何鎖等,以免重新生成正整數集。

歸根結底,這絲毫沒有關系。如果你將不間斷的序列作為表的鍵,則你的數據而不是序列會出現問題。

回答問題:

要實際回答你的問題,你需要:

首先,找出表中的最大id(序列)值。

然后放下并重新創(chuàng)建序列。

找到最大值意味著你需要動態(tài)地重新創(chuàng)建序列,而又要犧牲性能。

如果在這種情況下嘗試將某些東西插入表中,它將失敗,并且可能會使使用該序列的任何觸發(fā)器或其他對象無效:

declare

l_max_value number;

begin

select max(id)

into l_max_value

from my_table;

execute immediate 'drop sequence my_sequence_name';

-- nocache is not recommended if you are inserting more than

-- one row at a time, or inserting with any speed at all.

execute immediate 'create sequence my_sequence_name

start with ' || l_max_value

|| ' increment by 1

nomaxvalue

nocycle

nocache';

end;

/

正如我所說,不建議這樣做,你應該忽略任何差距。

更新-又名更好的答案感謝Jeffrey Kemp:

正如杰弗里·肯普(Jeffrey Kemp)在評論中所建議的那樣,有一種與文檔建議相反的方法,該方法無需刪除并重新創(chuàng)建序列即可。

即,通過:

計算id表中的最大值與序列的當前值之間的差。

更改順序以此負數遞增

更改順序以再次增加1。

這樣做的好處是對象仍然存在,并且觸發(fā)器,授權等也得以維護。如我所見,其不利之處在于,如果另一個會話與你的會話同時增加此負數,則你可能退得太遠。

這是一個示范:

設置測試:

SQL> create sequence test_seq

2 start with 1

3 increment by 1

4 nomaxvalue

5 nocycle

6 nocache;

Sequence created.

SQL>

SQL> create table tmp_test ( id number(16) );

Table created.

SQL>

SQL> declare

2 l_nextval number;

3 begin

4

5 for i in 1 .. 20 loop

6 insert into tmp_test values ( test_seq.nextval );

7 end loop;

8

9 end;

10 /

PL/SQL procedure successfully completed.

SQL>

SQL> select test_seq.currval from dual;

CURRVAL

----------

20

SQL>

SQL> delete from tmp_test where id > 15;

5 rows deleted.

SQL> commit;

Commit complete.

還原順序

SQL>

SQL> declare

2

3 l_max_id number;

4 l_max_seq number;

5

6 begin

7

8 -- Get the maximum ID

9 select max(id) into l_max_id

10 from tmp_test;

11

12 -- Get the current sequence value;

13 select test_seq.currval into l_max_seq

14 from dual;

15

16 -- Alter the sequence to increment by the difference ( -5 in this case )

.

17 execute immediate 'alter sequence test_seq

18 increment by ' || ( l_max_id - l_max_seq );

19

20 -- 'increment' by -5

21 select test_seq.nextval into l_max_seq

22 from dual;

23

24 -- Change the sequence back to normal

25 execute immediate 'alter sequence test_seq

26 increment by 1';

27

28 end;

29 /

PL/SQL procedure successfully completed.

SQL>

SQL> select test_seq.currval from dual;

CURRVAL

----------

15

SQL>

2020-04-13

總結

以上是生活随笔為你收集整理的mysql seq 重置_需要在Oracle中重置序列的值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。