Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id E85112E003F for ; Mon, 10 Dec 2007 03:43:53 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 20011-07 for ; Mon, 10 Dec 2007 03:43:46 -0400 (AST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.171]) by postgresql.org (Postfix) with ESMTP id 8FA2F2E003B for ; Mon, 10 Dec 2007 03:43:49 -0400 (AST) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrelayeu1) with ESMTP (Nemesis) id 0MKwpI-1J1dIq0ALH-0007Ja; Mon, 10 Dec 2007 08:43:48 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1J1dIp-00010U-00 for pgsql-sql@postgresql.org; Mon, 10 Dec 2007 08:43:47 +0100 Date: Mon, 10 Dec 2007 08:43:47 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: SQL INSERT/TRIGGER Help Message-ID: <20071210074347.GB917@a-kretschmer.de> References: <20071210064425.GA917@a-kretschmer.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX19/3l24SEhejsMiAPmkKLg6AWZZQbYAfaZIktj Xbycmxxml/Epyrg+4T+mMtUVAs2RBlG2vft0P7yiJp/hFm76u6 bRxyn624JEQxkpfeTW/jlycBLFukA4Q X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/33 X-Sequence-Number: 29725 am Mon, dem 10.12.2007, um 9:27:58 +0200 mailte Poovendran Moodley folgendes: > I'm not really sure how to the currval() method. I've read up on it and I > noticed it works with nextval() and setval(). The parameter for currval() is a > regex - is there a regex to represent the most recently automatically generated > number ( i.e. a serial field)? If there isn't, I was thinking that a trigger > could be used so that when an INSERT is executed against the Observation_Value Okay, i explain this a little bit more: first, i create two tables, master and slave. master contains a serial-field, this field is referenced by the slave table: test=# create table master (id serial primary key, name text); NOTICE: CREATE TABLE will create implicit sequence "master_id_seq" for serial column "master.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "master_pkey" for table "master" CREATE TABLE test=*# create table slave(master_id int references master, val text); CREATE TABLE Now i have 2 tables and a sequence named 'master_id_seq', and now the insert's: test=*# insert into master (name) values ('test'); INSERT 0 1 test=*# insert into slave (master_id, val) values (currval('master_id_seq'), 'value'); INSERT 0 1 The parameter for currval() is the name of the sequence. Note: you have to insert in the master table first, this calls the nextval() for the sequence. After, within this database session, you can use currval() to obtain the actual value for this sequence. And yes, this way is safe also for concurrency. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net