如何重置postgres中的序列并用新数据填充id列? [英] How to reset sequence in postgres and fill id column with new data?

查看:65
本文介绍了如何重置postgres中的序列并用新数据填充id列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超过一百万行的表.我需要重置序列并使用新值(1、2、3、4 ...等)重新分配ID列.有什么简单的方法吗?

I have a table with over million rows. I need to reset sequence and reassign id column with new values (1, 2, 3, 4... etc...). Is any easy way to do that?

推荐答案

如果您不想保留ID的顺序,则可以

If you don't want to retain the ordering of ids, then you can

ALTER SEQUENCE seq RESTART WITH 1;
UPDATE t SET idcolumn=nextval('seq');

我怀疑是否有一种简单的方法可以按您选择的顺序执行此操作,而无需重新创建整个表.

I doubt there's an easy way to do that in the order of your choice without recreating the whole table.

这篇关于如何重置postgres中的序列并用新数据填充id列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆