Unix Technical Forum

Insert without duplicates

This is a discussion on Insert without duplicates within the Pgsql General forums, part of the PostgreSQL category; --> Hi - This has been covered elsewhere, but the typical answers seem to involve using triggers, etc. What's the ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 08:22 PM
John Burger
 
Posts: n/a
Default Insert without duplicates

Hi -

This has been covered elsewhere, but the typical answers seem to
involve using triggers, etc.

What's the best way to insert a row into a table providing it's not
already there? In my client (Python) program, I can do two separate
interactions with the server, the first a query:

select 1 from foo where x = 1 and y = 2;

and the second the actual insert, if the query returns nothing:

insert into foo (x, y) values (1, 2);

Or I can use EXCEPT:

insert into foo (x, y)
select 1, 2
except
select x, y from foo where x = 1 and y = 2;

Are there other variants? What's the "best" method (fastest, etc.).
The query planner will use the same plan in the second case as the
first, no? What if I had a handful of new rows I'd like to
(conditionally) insert - can I do them all in one statement somehow?
Remember, I don't want to use triggers or anything like that, just
standard SQL statements.

Thanks.

- John D. Burger
MITRE



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:20 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com