更改序列的起始值

编程入门 行业动态 更新时间:2024-10-26 12:29:35
本文介绍了更改序列的起始值-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 。

更多推荐

更改序列的起始值

本文发布于:2023-10-25 22:26:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528289.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!