I have a table with columns
(product_id,related_product_id,related_counter)
If product A is related to product B then a record should be created,
if the record already exists then the related_counter should be
incremented.
This is very easy to do with MySQL using INSERT... ON DUPLICATE KEY.
Standard or not, it is very usefull.
Is there a way to catch the insert error. For example...
INSERT INTO related_products (product_id,related_product_id) VALUES
(?,?);
IF (???error: duplicate key???) THEN
UPDATE related_products SET related_counter = related_counter + 1;
END IF;
-Nick 8 9448
Nick wrote: I have a table with columns (product_id,related_product_id,related_counter)
If product A is related to product B then a record should be created, if the record already exists then the related_counter should be incremented.
This is very easy to do with MySQL using INSERT... ON DUPLICATE KEY. Standard or not, it is very usefull.
Is there a way to catch the insert error. For example...
INSERT INTO related_products (product_id,related_product_id) VALUES (?,?); IF (???error: duplicate key???) THEN UPDATE related_products SET related_counter = related_counter + 1; END IF;
-Nick
With a rule you can do it easily ( never tried ).
Regards
Gaetano Mendola
UPDATE related_products SET related_counter = related_counter
WHERE .....
only updates if the record exists
INSERT (x,y,z) SELECT ?,?,1 WHERE NOT EXISTS (SELECT 1 FROM
related_products WHERE .....)
Inserts if the key does not exist.
On Sat, 11 Sep 2004 00:02:26 +0200, Gaetano Mendola <me*****@bigfoot.com> wrote: Nick wrote:
I have a table with columns (product_id,related_product_id,related_counter)
If product A is related to product B then a record should be created, if the record already exists then the related_counter should be incremented.
This is very easy to do with MySQL using INSERT... ON DUPLICATE KEY. Standard or not, it is very usefull.
Is there a way to catch the insert error. For example...
INSERT INTO related_products (product_id,related_product_id) VALUES (?,?); IF (???error: duplicate key???) THEN UPDATE related_products SET related_counter = related_counter + 1; END IF;
-Nick
With a rule you can do it easily ( never tried ).
Regards Gaetano Mendola
---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html
I may have short handed this to much. I will assume the product A has
an id of 1 and the related product B has an id of 2. You have a
default on related_counter of 1 I am assuming
INSERT INTO related_products (product_id,related_product_id)
SELECT 1, 2 WHERE NOT EXISTS (SELECT 1
FROM related_products
WHERE
product_id = 1 AND related_product_id = 2)
The insert is plain enough but instead of using values you are getting
the data from the select statement. The select statement returns 1
row of constant values just like the doing the values however no row
is returned if the where clause is not met. If no row is returned
then nothing can be inserted therefore no error is returned.
So let's look at the where clause it is a if the subselect returns any
value then exists will be true but we invert that with the NOT. The
subselect returns 1 if a row already exists with product_id and
related_product_id other wise a null row is returned.
You can think of this as a INSERT if the key doesn't already exist.
If you still need more help just let me know :-)
On Sat, 11 Sep 2004 01:17:29 +0100, Ian Linwood
<ia*@dinwoodie.freeuk.com> wrote: Hello Kevin,
Friday, September 10, 2004, 11:19:58 PM, you wrote:
KB> INSERT (x,y,z) SELECT ?,?,1 WHERE NOT EXISTS (SELECT 1 FROM KB> related_products WHERE .....)
could someone walk me through this one? I do not understand it at all. apologies for my cluelessness ;-)
-- Best regards, Ian
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org INSERT INTO related_products (product_id,related_product_id) SELECT 1, 2 WHERE NOT EXISTS (SELECT 1 FROM related_products WHERE product_id = 1 AND related_product_id = 2)
Should not the SELECT be FOR UPDATE ?
because if no insert is done, the OP wanted to UPDATE the row, so it
should not be deleted by another transaction in-between...
Can the above query fail if another transaction inserts a row between the
SELECT and the INSERT or postgres guarantee that this won't happen ?
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives? http://archives.postgresql.org
Pierre-Frédéric Caillaud wrote: INSERT INTO related_products (product_id,related_product_id) SELECT 1, 2 WHERE NOT EXISTS (SELECT 1 FROM related_products WHERE product_id = 1 AND related_product_id = 2)
Should not the SELECT be FOR UPDATE ? because if no insert is done, the OP wanted to UPDATE the row, so it should not be deleted by another transaction in-between...
Can the above query fail if another transaction inserts a row between the SELECT and the INSERT or postgres guarantee that this won't happen ?
There is no "between" a single statement.
--
Peter Eisentraut http://developer.postgresql.org/~petere/
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org There is no "between" a single statement.
Yes, I know, even if the statement involves mutiple subqueries...
I meant :
The OP wants to UPDATE if the row already exists, and to INSERT otherwise
; we have the INSERT bit here, but to UPDATE he needs to check if the
insert really took place, and if not, issue an UPDATE statement.. so that
makes it two statements.
By the way, do several consecutive queries inside a plpgsql function
count as one statement (the function call) or as several statements (ie.
inside a function are transactions like SERIALIZED ?)
On Sat, 11 Sep 2004 13:56:26 +0200, Peter Eisentraut <pe*****@gmx.net>
wrote:
Pierre-Frédéric Caillaud wrote: > INSERT INTO related_products (product_id,related_product_id) > SELECT 1, 2 WHERE NOT EXISTS (SELECT 1 > FROM > related_products > WHERE > product_id = 1 AND related_product_id = 2)
Should not the SELECT be FOR UPDATE ? because if no insert is done, the OP wanted to UPDATE the row, so it should not be deleted by another transaction in-between...
Can the above query fail if another transaction inserts a row between the SELECT and the INSERT or postgres guarantee that this won't happen ?
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html
Peter Eisentraut <pe*****@gmx.net> writes: Pierre-Frédéric Caillaud wrote: INSERT INTO related_products (product_id,related_product_id) SELECT 1, 2 WHERE NOT EXISTS (SELECT 1 FROM related_products WHERE product_id = 1 AND related_product_id = 2) Should not the SELECT be FOR UPDATE ? because if no insert is done, the OP wanted to UPDATE the row, so it should not be deleted by another transaction in-between...
Can the above query fail if another transaction inserts a row between the SELECT and the INSERT or postgres guarantee that this won't happen ?
There is no "between" a single statement.
Sure there is. In the above example, the EXISTS result will be correct
as of the time of the snapshot that was taken at the start of the
command (or the start of the whole transaction, if using SERIALIZABLE
mode). So it is *entirely* possible for the INSERT to fail on duplicate
key if some other transaction commits a conflicting row concurrently.
AFAIK, all the bulletproof solutions for this sort of problem involve
being prepared to recover from a failed insertion. There are various
ways you can do that but they all come down to needing to catch the
duplicate key error. In the past you have had to code that in
client-side logic. In 8.0 you could write a plpgsql function that
catches the exception.
Given the need for a test anyway, I think the WHERE NOT EXISTS above
is pretty much a waste of time. Just do an INSERT, and if it fails do
an UPDATE; or do an UPDATE, and if it fails (hits zero rows) then do
an INSERT, being prepared to go back to the UPDATE if the INSERT fails.
Which of these is better probably depends on how often you expect each
path to be taken.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives? http://archives.postgresql.org
On Sat, 11 Sep 2004 11:27:02 -0400, Tom Lane <tg*@sss.pgh.pa.us> wrote: There is no "between" a single statement.
Sure there is. In the above example, the EXISTS result will be correct as of the time of the snapshot that was taken at the start of the command (or the start of the whole transaction, if using SERIALIZABLE mode). So it is *entirely* possible for the INSERT to fail on duplicate key if some other transaction commits a conflicting row concurrently.
AFAIK, all the bulletproof solutions for this sort of problem involve being prepared to recover from a failed insertion. There are various ways you can do that but they all come down to needing to catch the duplicate key error. In the past you have had to code that in client-side logic. In 8.0 you could write a plpgsql function that catches the exception.
Given the need for a test anyway, I think the WHERE NOT EXISTS above is pretty much a waste of time. Just do an INSERT, and if it fails do an UPDATE; or do an UPDATE, and if it fails (hits zero rows) then do an INSERT, being prepared to go back to the UPDATE if the INSERT fails. Which of these is better probably depends on how often you expect each path to be taken.
It's not meant to be a bulletproof solution. It's meant to be a
syntactically equivalent to the MySQL statement. You still have to
check for a failure.
Do the update followed by the insert in a serial transaction. If the
transaction fails you redo the same SQL transaction. This eliminates
the need for a this query else this query scenario which is the whole
point of the MySQL bastard syntax in the first place. Not the best
solution but if you have a good DBA and bad programmers it might be
what you actually want.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: F C |
last post by:
I have http://localhost/myweb1 who redirect me to my pages.
I would like to have http://localhost/myweb2 which I would like to use the
same exact set of pages, except a different global.asa...
|
by: jy2003 |
last post by:
1. Does MyISAM support FOREIGN KEY constraints? When I read the online
MySQL Reference Manual, it said "InnoDB also supports FOREIGN KEY
constraints". Does it mean MyISAM does not support FOREIGN...
|
by: jacksuyu |
last post by:
I have two xsd files, in one xsd file, I defined a "key", I'd like to
use "keyref" to refer to that "key" from another xsd file. But I always
get attribute is empty error.
my.xsd is my first xsd...
|
by: microsoft |
last post by:
When I create a instance of web services proxy in winform, I get the
exception.
Stack Trace:
System.ArgumentException: Item has already been added. Key in
dictionary:"winbootdir" Key being...
|
by: susanwinchell |
last post by:
I have a table with only two columns (created from query of a much
larger database). The first column contains many "duplicate" names
or ID, and the second column contains values which need to be...
| |
by: VB Programmer |
last post by:
1. Is there an ASP.NET 2.0 version available yet?
2. Is there a VB.NET version available?
3. How do I install the source version on my dev machine (I have SQL Server
2000, VS.NET, IIS, etc...)...
|
by: Po Eddie Lim |
last post by:
Hello...
Is it possible in PostGre 7.3 to query the size of a text array attribute
of a table? Does anyone know how this is queried in 7.3?
thanks... help is greatly needed.
eddie
-----...
|
by: ericnyc |
last post by:
Hi,
I am a newbee in C++
Please review this is what I wrote , what so ever I understood so far,
My question is that I have to " Write a program C++ array that reads in an integer number and...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |