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:
- For every
INSERT
, do aSELECT
to get the last key, and "increment it"? - 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.