Comments on: How to write INSERT IF NOT EXISTS queries in standard SQL http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: oleg http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19643 oleg Mon, 19 Sep 2011 16:39:37 +0000 http://www.xaprb.com/blog/?p=8#comment-19643 Jason wrote:

> Here’s one I used that should work in all SQL languages. I was trying to avoid using specific language stuff like EXISTS and DUPLICATE.
>
> It’s very simple. Just insert the value(s) where 0 equals the count of the value(s) already existing.
>
> INSERT INTO audit_type
> SELECT DISTINCT ‘sample’ FROM audit_type WHERE
> 0 = (SELECT COUNT(audit_type_desc) FROM audit_type WHERE audit_type_desc = ‘sample’)

This doesn’t work if audit_type is empty. Also, it can fail with
uniqueness violation when doing it inside transactions, like I
described one post above. Would be interesting to know if it can fail
without transactions involved too.

]]>
By: Jason http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19637 Jason Fri, 16 Sep 2011 15:30:55 +0000 http://www.xaprb.com/blog/?p=8#comment-19637 Here’s one I used that should work in all SQL languages. I was trying to avoid using specific language stuff like EXISTS and DUPLICATE.

It’s very simple. Just insert the value(s) where 0 equals the count of the value(s) already existing.

INSERT INTO audit_type
SELECT DISTINCT ‘sample’ FROM audit_type WHERE
0 = (SELECT COUNT(audit_type_desc) FROM audit_type WHERE audit_type_desc = ‘sample’)

]]>
By: oleg http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19593 oleg Wed, 24 Aug 2011 19:10:19 +0000 http://www.xaprb.com/blog/?p=8#comment-19593 This fails in PostgreSQL as soon as another process adds rows to the
urls table inside transactions. To reproduce:

1. Start process 1 (psql) and process 2 (psql).

2. In both processes, type “\set AUTOCOMMIT on” (this stops psql from
starting implicit transactions).

3. In process 1, type:

begin;
insert into urls(url) values (‘http://www.xaprb.com/blog/’);

4. In process 2, type:

insert into urls(url)
select ‘http://www.xaprb.com/blog/’
from mutex
left outer join urls
on urls.url = ‘http://www.xaprb.com/blog/’
where mutex.i = 1 and urls.url is null;

5. Process 2 hangs waiting for process 1 to commit or roll back.

6. In process 1, type:

commit;

7. Behold error in process 2:

ERROR: duplicate key value violates unique constraint “urls_pkey”

It’s tricky to test this with two processes without a transaction, but
I suspect this can also fail without any transactions involved.

]]>
By: oleg http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19493 oleg Sat, 16 Jul 2011 00:12:50 +0000 http://www.xaprb.com/blog/?p=8#comment-19493 Can somebody please explain why this can’t fail with uniqueness violation on the url column? If another client commits a record with url = ‘http://www.xaprb.com/blog/’ after our “SELECT … OUTER JOIN …” runs but before our INSERT does, then wouldn’t our INSERT later violate uniqueness violation by inserting the duplicate url? Just like in the Microsoft SQL example with the IF statement?

]]>
By: mPHO http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-19321 mPHO Tue, 03 May 2011 14:20:49 +0000 http://www.xaprb.com/blog/?p=8#comment-19321 Hi guys

I have the below syntax but it is always insert duplicates can someone please help

INSERT INTO roshcon_vehicle_v1_0
(VehicleRegNumber,VehicleType,VehicleMake,driver_FstName,driver_Surname,driver_TelNr1Number,driver_SubName,driver_TelNr4Number,driver_SubManagerEmail ,Hire_Time)
SELECT roshcon_vehicle_report_001.VehicleRegNumber, VehicleType,VehicleMake,roshcon_vehicle_drivers_v1_0.driver_FstName, roshcon_vehicle_drivers_v1_0.driver_Surname,driver_TelNr1Number,driver_SubName,driver_TelNr4Number,driver_SubManagerEmail , Hire_Time
FROM roshcon_vehicle_driver_v1_0, roshcon_vehicle_report_001, roshcon_vehicle_drivers_v1_0
WHERE roshcon_vehicle_report_001.Vehicle_Id = roshcon_vehicle_driver_v1_0.Id
AND roshcon_vehicle_drivers_v1_0.driver_Id = roshcon_vehicle_driver_v1_0.driver_FstName

]]>