How to find the the primary key sequence and the max of primary key

吃可爱长大的小学妹 提交于 2020-01-25 10:05:21

问题


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');

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)

but the max should be more than 10000.

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?

Error:

select setval('Audit_auditID_seq', 171832, true);
ERROR:  relation "audit_auditid_seq" does not exist
LINE 1: select setval('Audit_auditID_seq', 171832, true);

回答1:


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

and finally make the value as the same:

 select setval('"Audit_auditID_seq"', 171832, true);

If you need to find out the sequence, use

 \d "table_name";


来源:https://stackoverflow.com/questions/52283993/how-to-find-the-the-primary-key-sequence-and-the-max-of-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!