Auto-increment text primary key in postgres
0
votes
2
answers
1666
views
I would like my content to be searchable by users with a unique 6 characters id (i.e. zab4ac, cba884, ...).
I was thinking of creating a
id character(6) primary key
column, but then I started wondering how to create ids for new rows. They can be random or sequential, it doesn't matter to me, but I was wondering what the best option would be:
1) For every INSERT
, do a SELECT
to get the last key, and "increment it"?
2) or create a random key and verify if it already exists?
Since these seem inefficient (2 queries for every insertion), is there a automated way to do it, like serial
, but for characters (000009, 00000a, 00000b, ..., 00000z, 000010, ..., or something more random)? Or should I use serial
in the database and convert it to a string for users?
I am looking to see if I am missing an easy and efficient approach to the problem.
Asked by pascal
(3 rep)
Jun 29, 2023, 06:25 PM
Last activity: Jun 29, 2023, 09:34 PM
Last activity: Jun 29, 2023, 09:34 PM