Received: from charon.simplot.com (jrsmailhub.simplot.com [12.18.144.212]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fAFIcfm25694 for ; Thu, 15 Nov 2001 13:38:41 -0500 (EST) (envelope-from earlj@xanadu.simplot.com) Received: from xanadu.simplot.COM (151.113.0.132) by charon.simplot.com (MX V5.1-A AnGp) with SMTP; Thu, 15 Nov 2001 11:38:39 -0700 Received: from npa01zz001.simplot.com by xanadu.simplot.COM; (5.65v4.0/1.1.8.2/20Aug97-1116AM) id AA27067; Thu, 15 Nov 2001 11:38:38 -0700 Received: from earlj by npa01zz001.simplot.com with local (Exim 3.32 #1 (Debian)) id 164RPJ-0002au-00; Thu, 15 Nov 2001 11:38:37 -0700 To: "Wilco Boschman" CC: pgsql-sql@postgresql.org Subject: Re: INSERT question References: <1f5f2b44.0111130130.17e8e57a@posting.google.com> <3bf19679$0$189$e4fe514c@newszilla.xs4all.nl> From: Jason Earl Date: 15 Nov 2001 11:38:36 -0700 In-Reply-To: <3bf19679$0$189$e4fe514c@newszilla.xs4all.nl> Message-ID: <878zd7vpzn.fsf@npa01zz001.simplot.com> Lines: 61 User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/21.1 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Archive-Number: 200111/221 X-Sequence-Number: 5604 Yikes! Don't do that. For one thing: SELECT max(primary_key) FROM parent_table; Will not use the index in PostgreSQL. If you want to get the maximum value of an indexed column it is far better to write something like this: SELECT primary_key FROM parent_table ORDER BY primary_key DESC LIMIT 1; Yes, it takes longer to type, but it will use the index and return your results immediately. Even better, however, is to use the functions nextval() and currval() like so: INSERT INTO parent_table (data) values ('some data'); INSERT INTO child_table (parent, more_data) (currval('parent_table_p_key_seq'), 'more data'); Jason "Wilco Boschman" writes: > Insert the row into the parent table (the one with the primary key), > then do a select max() from parent_table; This will > give you the greatest number in the table, that is if everything > went ok the number from the row you just inserted. Then insert the > row(s) into the second table and use the value you got from the > parent table in the foreign key > > cheers > > Wilco > > "Brian" schreef in bericht > news:1f5f2b44.0111130130.17e8e57a@posting.google.com... > | Hi everyone, > | I've a little question about using insert statements. I've got a > | parent table with a "serial" (automatically incrementing integer) > | primary key. I have a child table with a foreign key which references > | that primary key. My question is: > | > | To insert values into the child table corresponding to an entry in the > | parent table, how do I get a reference to the serial primary key (so > | as I can reference it for the foreign key entry) > | > | Hope you understand what I mean. This should be a regular occurance > | and seeing as I'm not an sql guru, I just don't have a clue! > | > | Any help would be SOOO appreciated. > | Brian > > > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org