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

查看:36
本文介绍了如何在 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天全站免登陆