更改序列的起始值-Postgresql [英] Change the starting value of a serial - Postgresql

查看:635
本文介绍了更改序列的起始值-Postgresql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对序列号有一个小问题:从文件中,填满了我的数据库,其中有一个客户端ID(这是一个序列号,它是我的主键)。我有300个客户,所以有300个客户ID(1到300)。现在的问题是,我有一个新客户的表格,我无法添加它们,因为当我添加客户时,我的程序会添加ID为1的客户或ID为1的客户已分配给另一个客户。

I've a little problem with serial : From a file, I filled my database in which I have a client ID (it is a serial and it is my primary key). I have 300 clients so 300 client ID (1 to 300). Now my problem is, I've a form for new clients.I cannot add them because when I add a client, my program adds the client with ID 1 or the ID 1 is already assigned to another client.

所以我的问题是:是否可以更改序列的起始值以解决此问题?

So my question is : is it possible to change the starting value of a serial for to resolve this problem ?

推荐答案

您可以使用 RESTART WITH 更改序列,以更改当前序列号;

You can alter a sequence using RESTART WITH to change the current sequence number;

ALTER SEQUENCE test_seq RESTART WITH 300;

如果要使用序列关键字创建序列名称,则要获取序列名称,请使用

To get the sequence name if you created it using the serial keyword, use

SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'table name goes here'); 

要测试的SQLfiddle

这篇关于更改序列的起始值-Postgresql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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