如何找到主键序列和主键的最大值 [英] How to find the the primary key sequence and the max of primary key
问题描述
我知道这个问题有点愚蠢,我需要运行这些查询以查看值是否不同步,但出现类似关系不存在的错误:
I know this question is kind of silly, I need to run these queries to see if the values are out of sync, but I get an error like 'relation does not exist':
SELECT MAX(the_primary_key) FROM the_table;
SELECT nextval('the_primary_key_sequence');
我有一个名为 Audit的表,其主键列为 auditID。当我运行第一个查询时,我得到了结果:
I have a table named "Audit" with the primary key column 'auditID'. When I run the first query I got the result:
SELECT MAX('auditID') FROM "Audit";
max
---------
auditID
(1 row)
,但最大值应大于10000。
but the max should be more than 10000.
然后我运行第二个查询,并收到错误信息 the_primary_key_sequence或 Audit_auditID_seq不存在。
如何检查是否存在任何主键序列?
Then I run the second query and I get the error 'relation "the_primary_key_sequence" or "Audit_auditID_seq" does not exist'. How can I check if any primary key sequence exists or not?
错误:
select setval('Audit_auditID_seq', 171832, true);
ERROR: relation "audit_auditid_seq" does not exist
LINE 1: select setval('Audit_auditID_seq', 171832, true);
推荐答案
我弄清楚我所有的问题,引号是在这种情况下真的很敏感;
I figure out all my questions, the quote mark is really sensitive in this case;
SELECT MAX("auditID") FROM "Audit";
max
--------
171832
(1 row)
SELECT nextval('"Audit_auditID_seq"');
----
139801
并最终使值相同:
select setval('"Audit_auditID_seq"', 171832, true);
如果需要找出序列,请使用
If you need to find out the sequence, use
\d "table_name";
这篇关于如何找到主键序列和主键的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!