Re: Query to get the "next available" unique suffix for a name

From: John R Pierce <pierce(at)hogranch(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Query to get the "next available" unique suffix for a name
Date: 2010-09-28 01:56:00
Message-ID: 4CA14B30.1010609@hogranch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 09/27/10 6:36 PM, Mike Christensen wrote:
> Thus, the users table already has:
>
> MikeChristensen1
> MikeChristensen2
> MikeChristensen3
> MikeChristensen4
>
> I want to write a SQL query that figures out that MikeChristensen5 is
> the next available username and thus suggest it. Here's some things I
> could do:

bogo pseudocode.
$n is 'MikeChristensen'

table users {
uid serial,
username text unique,
userroot text,
namesuffix integer,
...
}

sql.exec ("begin;");
sql.exec ("select max(namesuffix) from users where userroot=$1;", $n);
$i = sql.result(1);
newname = $n.$i;
sql.exec("insert ......");
sql,exec("commit;");

of course, you need to deal with an unique constraint exception on that
insert, and rollback and retry the whole thing.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tim Uckun 2010-09-28 02:08:21 Re: Deleting orphaned records (not exists is very slow)
Previous Message Mike Christensen 2010-09-28 01:37:44 Re: Installing pgAdmin on a Mac