pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jean-Michel POURE <jm.poure@freesurf.fr>
To: Alan Wayne <alanjwayne@yahoo.com>
To: Stephan Szabo <sszabo@megazone23.bigpanda.com>
Cc: pgsql-general@postgresql.org, pgsql-admin <pgsql-admin@postgresql.org>
Subject: HOWTO - Random character generation for primary key
Date: Thu, 2 May 2002 11:04:06 +0200
Message-ID: <200205021104.06992.jm.poure@freesurf.fr> (raw)
In-Reply-To: <20020502020148.35486.qmail@web21203.mail.yahoo.com>
References: <20020502020148.35486.qmail@web21203.mail.yahoo.com>

Le Jeudi 2 Mai 2002 04:01, Alan Wayne a écrit :
> In foxpro, I've been using the provided 10-character
> random generator to provide for the primary keys on my
> data files. As far as I can tell, PostgreSQL only uses
> an auto-incrementing serial field for its primary
> keys. So as far as I can see, I'm left with the
> options of somehow converting all my existing records
> (with their relationships intact) to some form of
> integer key, or providing a similiar random character
> generator from PostgreSQL. So what to do????

Dear Alan,

Do not hesitate to CC pgsql-general@postgresql.org and pgsql-admin so that 
anyone can participate.

I don't know if plain SQL supports loops, I guess not (?). Writing a primary 
key generator is quite easy under PostgreSQL using PLpgSQL. So let's go for 
PLpgSQL:

First of all, enable PLpgSQL in your database:

Under postgresql user:
postgres@locahost>createlang plpgsql database_name

Then add this PLpgSQL script to your database:

CREATE FUNCTION "random_string"("int4") RETURNS "varchar" AS '
DECLARE
iLoop int4;
result varchar;

BEGIN
result = '''';
IF ($1>0) AND ($1 < 255) THEN
  FOR iLoop in 1 .. $1 LOOP
    result = result || chr(int4(random()*26)+65);
  END LOOP;
  RETURN result;
ELSE
  RETURN ''f'';
END IF;
END;
'  LANGUAGE 'plpgsql';

chr() is multi-byte safe. This means it will work for an UTF-8 or ASCII 
database. Not all PostgreSQL system functions are commented and described. A 
good way to learn how to use these functions is to run pgAdmin2 
(http://pgadmin.postgresql.org) and choose the "'display system objects" 
option. Each function has a small comment which provides a small description. 

PLpgSQL syntax is close to Pascal and SQL. More information can be found on:
http://www.postgresql.org/idocs/index.php?programmer-pl.html

PostgreSQL advantage compared to other open-source systems, like MySQL for 
example, is the ability to write server-side code.

It is highliy recommanded to migrate some of your existing Foxpro / Access 
code to PostgreSQL server-side, for the following reasons:

- a server-side application has a better transactional behavior (ex: perform 
complex actions when you add / drop / update a table within transactions).

- a PostgreSQL server-side application is ***much faster*** than a flat table 
application (PostgreSQL is able to process complex scripts involving several 
tables in ONE client-side query. In some situations, it can boost the speed 
by a 10 to 100 factor).

After migration, your Foxpro / Access application should look like a simple 
data "viewer" or "browser" application. Furthermore, it allows you to add a 
Web interface (ex: PHP) quite easily because, again, PHP will be used for 
data viewing, not data processing.

If you need more information, do not hesitate to get back to me and/or post 
comments on the list.

Cheers,
Jean-Michel



view thread (5+ messages)  latest in thread

Message-ID: <200205021104.06992.jm.poure@freesurf.fr>
Permalink:  ../200205021104.06992.jm.poure@freesurf.fr/
Also on:    postgresql.org/message-id/200205021104.06992.jm.poure@freesurf.fr

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: jm.poure@freesurf.fr, alanjwayne@yahoo.com, sszabo@megazone23.bigpanda.com
  Subject: Re: HOWTO - Random character generation for primary key
  In-Reply-To: <200205021104.06992.jm.poure@freesurf.fr>
  References: <20020502020148.35486.qmail@web21203.mail.yahoo.com> <200205021104.06992.jm.poure@freesurf.fr>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox