Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 896ED475C28; Thu, 2 May 2002 05:04:18 -0400 (EDT) Received: from www1.translationforge.com (unknown [212.11.36.58]) by postgresql.org (Postfix) with ESMTP id D83AE475960; Thu, 2 May 2002 05:04:16 -0400 (EDT) Received: from linus.translationforge.com ([192.168.1.102]) by www1.translationforge.com (8.12.1/8.12.1) with ESMTP id g42949KO032067; Thu, 2 May 2002 11:04:09 +0200 Content-Type: text/plain; charset="iso-8859-1" From: Jean-Michel POURE Reply-To: jm.poure@freesurf.fr To: Alan Wayne , Stephan Szabo Subject: HOWTO - Random character generation for primary key Date: Thu, 2 May 2002 11:04:06 +0200 X-Mailer: KMail [version 1.4] Cc: pgsql-general@postgresql.org, pgsql-admin References: <20020502020148.35486.qmail@web21203.mail.yahoo.com> In-Reply-To: <20020502020148.35486.qmail@web21203.mail.yahoo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200205021104.06992.jm.poure@freesurf.fr> X-Archive-Number: 200205/5 X-Sequence-Number: 4180 Le Jeudi 2 Mai 2002 04:01, Alan Wayne a =E9crit : > 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= =20 anyone can participate. I don't know if plain SQL supports loops, I guess not (?). Writing a primar= y=20 key generator is quite easy under PostgreSQL using PLpgSQL. So let's go for= =20 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 =3D ''''; IF ($1>0) AND ($1 < 255) THEN FOR iLoop in 1 .. $1 LOOP result =3D 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=20 database. Not all PostgreSQL system functions are commented and described. = A=20 good way to learn how to use these functions is to run pgAdmin2=20 (http://pgadmin.postgresql.org) and choose the "'display system objects"=20 option. Each function has a small comment which provides a small descriptio= n.=20 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= =20 example, is the ability to write server-side code. It is highliy recommanded to migrate some of your existing Foxpro / Access= =20 code to PostgreSQL server-side, for the following reasons: - a server-side application has a better transactional behavior (ex: perfor= m=20 complex actions when you add / drop / update a table within transactions). - a PostgreSQL server-side application is ***much faster*** than a flat tab= le=20 application (PostgreSQL is able to process complex scripts involving severa= l=20 tables in ONE client-side query. In some situations, it can boost the speed= =20 by a 10 to 100 factor). After migration, your Foxpro / Access application should look like a simple= =20 data "viewer" or "browser" application. Furthermore, it allows you to add a= =20 Web interface (ex: PHP) quite easily because, again, PHP will be used for= =20 data viewing, not data processing. If you need more information, do not hesitate to get back to me and/or post= =20 comments on the list. Cheers, Jean-Michel