如何为postgres表指定主键种子? [英] How to specify the primary key seed for a postgres table?

查看:122
本文介绍了如何为postgres表指定主键种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个postgres表,其中包含数据,其中注入数据的代码已设置了主键。因此,可以说主键为0..49。

I have a postgres table that contains data where the primary key has been set by the code that injected the data. So lets say the primary keys are 0..49.

我如何指示postgres在主键列上以50开始进行下一次插入?当前,所有没有设置主键的插入操作都会导致重复的ID问题,因为它从0开始(我认为)。

How can I instruct postgres to start with 50 on the primary key column for the next insert? Currently all inserts without a set primary key cause duplicate ID issues since its starting from 0 (I assume).

推荐答案

所有您需要的要做的就是创建一个从50开始的序列,并将主键列的默认值设置为序列的下一个值

All you need to do is create a sequence with start 50 and set the default value for the primary key column as the next value for the sequence

这里是代码

CREATE SEQUENCE seq_name START 50;
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT nextval(seq_name);

这篇关于如何为postgres表指定主键种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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