如何使用表中的特定max(id)更改PostgreSQL中的Sequence?

如何使用表中的特定max(id)更改PostgreSQL中的Sequence?,第1张

概述参见英文答案 > How to reset postgres’ primary key sequence when it falls out of sync?                                    26个 >             Alter auto-generated sequence                                    2个 参见英文答案 > How to reset postgres’ primary key sequence when it falls out of sync?                                    26个
>             Alter auto-generated sequence                                    2个
我需要正确地遵循sql脚本语法.在postgres中,你不能真正将“alter sequence”与“select max(ID)”链接起来.那么以Postgresql接受它的方式编写脚本的正确方法是什么?

这是脚本,所以你可以知道我需要什么:

alter SEQUENCE notification_settings_seq START with (select max(ID) from game_user)
解决方法 这将使用新值重新启动序列:

do $$declare maxID int;begin    select max(ID) from game_user into maxID;    execute 'alter SEQUENCE seq_name RESTART with '|| maxID;   end;$$language plpgsql
总结

以上是内存溢出为你收集整理的如何使用表中的特定max(id)更改PostgreSQL中的Sequence?全部内容,希望文章能够帮你解决如何使用表中的特定max(id)更改PostgreSQL中的Sequence?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1155634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存