我对序列号有一个小问题:从文件中,填满了我的数据库,其中有一个客户端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 。
更多推荐
更改序列的起始值
发布评论