X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 312B0565B0 for ; Tue, 15 Mar 2005 21:51:24 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 36436-02 for ; Tue, 15 Mar 2005 21:51:23 +0000 (GMT) Received: from www.lodestonetechnologies.com (lodestonetechnologies.com [69.64.33.50]) by svr1.postgresql.org (Postfix) with ESMTP id CD1215659D for ; Tue, 15 Mar 2005 21:51:22 +0000 (GMT) Received: (qmail 17705 invoked from network); 15 Mar 2005 22:53:27 -0000 Received: from ky-lawrenceburg1c-123.hdbgky.adelphia.net (HELO ?192.168.253.3?) (68.69.152.123) by lodestonetechnologies.com with SMTP; 15 Mar 2005 22:53:27 -0000 Mime-Version: 1.0 (Apple Message framework v619.2) In-Reply-To: <1110920863.42374e9f75aa1@sms.ed.ac.uk> References: <1110920863.42374e9f75aa1@sms.ed.ac.uk> Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <55762d9369dc9fd5b85b8662ccdf8557@lodestonetechnologies.com> Content-Transfer-Encoding: 7bit From: Robert Perry Subject: Re: Check before insert Date: Tue, 15 Mar 2005 16:51:20 -0500 To: pgsql-interfaces@postgresql.org X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200503/46 X-Sequence-Number: 5375 Firstly if it is important to only insert unique rows then I recommend using a unique constraint. Creating a primary key will also accomplish this. I do this even if I am going to be writing software to only insert unique data. (I that way if I mess up I find out about it) I imagine that sooner or later you will be wanting to join other tables with this one or at least find records out of a long list of these things, so you will be needing an index anyway. A unique constraint or primary key will create one for you automatically, just because they each (unique constraints and primary keys) need an index to find the record in a timely manor. Secondly, no I do not know of a single command that will do this. If performance is important I would write a stored proc to check to see if it exists and inserts it if it does not. Note: After you do these do these I would be sure to call analyze after a few thousand records have been inserted, otherwise performance will probably go down hill fast. on second thought, I guess something like Insert (protein_id, name) select 'P04667', 'Albumin' where not exists(select * from protein_table_name where protein_id = 'P04667') might meet your needs too. (Note: I assumed that protein_id was what you wanted to keep unique....you can keep either or both unique) I would still put it in a stored proc. (I like stored procs) On Mar 15, 2005, at 4:07 PM, SG Edwards wrote: > > > Hi, > > I have a table as follows: > > protein_id | name > ___________ ______ > > P04667 Albumin > P45366 Lactoglobulin > .... > etc > > I have a perl script that will insert data into this table from a file > containing a list of protein_id and names. However, I only want to > insert > proteins where they are not already present in the database. Is there > a way to > do this using an SQL command? > > > Thanks > > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster >