Re: Generating unique values for TEXT columns

From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: "Frank D(dot) Engel, Jr(dot)" <fde101(at)fjrhome(dot)net>
Cc: pgsql general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Generating unique values for TEXT columns
Date: 2005-01-03 17:13:41
Message-ID: 1104772421.23251.14.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 2005-01-03 at 10:53, Frank D. Engel, Jr. wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Is there any "convenient" way to generate (on request) a unique value
> for a TEXT column? I have a situation in which I want users of my
> front-end program to be able to manually enter values for this column,
> but if they leave it blank (in the front-end), to have the database
> automatically fill in a unique value. I would like to restrict the
> unique values to (for example) digits and uppercase letters (this is
> flexible, but the uniqueness of the values should be visually
> discernible, and all characters should be printable).
>
> I know how to do this with a numeric column (I can just SELECT MAX on
> the column and add one, for example), but how can this be done with a
> TEXT column?

You might want to try creating a sequence for this, then doing something
like:

select upper(md5(nextval('lll')));

Then checking to see if that string is already used to be sure and use
that. If md5 strings are too long, then just substr() the function
above to get a small bit. Chances of failing uniqueness test will
increase, but you have to test for that anyway, so...

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Pierre-Frédéric Caillaud 2005-01-03 17:20:00 Re: Merging Data from Multiple DB
Previous Message Josué Maldonado 2005-01-03 17:07:42 Re: Generating unique values for TEXT columns