Re: SELECT and INSERT if no row is returned

2010-03-24 Thread Johnny Withers
Make parent_id a unique key. Doing a select first and inserting if no result will work 99.9% of the time; however, consider 2 rows being inserted at the same time. JW On Tuesday, March 23, 2010, Kyong Kim kykim...@gmail.com wrote: I need to INSERT a row into a table only if it does not exist to

Re: SELECT and INSERT if no row is returned

2010-03-24 Thread Rodrigo Ferreira
First, if you want no duplicate parent_id, make it unique key (as JW saids). Look at INSERT ... ON DUPLICATE KEY UPDATE, I think this will solve the problem with one statement. Rodrigo Ferreira --- On Wed, 3/24/10, Johnny Withers joh...@pixelated.net wrote: From: Johnny Withers

Re: SELECT and INSERT if no row is returned

2010-03-24 Thread Kyong Kim
I needed to give greater detail. parent_id isn't unique. The table has a composite primary key (parent_id, seq_id). Here's a better schema def CREATE TABLE sometable ( parent_id INT(10) NOT NULL, seq_id INT(10) AUTO_INCREMENT, child_id INT(10) NULL, PRIMARY KEY(parent_id, seq_id), UNIQUE

Re: SELECT and INSERT if no row is returned

2010-03-24 Thread Shawn Green
Kyong Kim wrote: I needed to give greater detail. parent_id isn't unique. The table has a composite primary key (parent_id, seq_id). Here's a better schema def CREATE TABLE sometable ( parent_id INT(10) NOT NULL, seq_id INT(10) AUTO_INCREMENT, child_id INT(10) NULL, PRIMARY KEY(parent_id,

SELECT and INSERT if no row is returned

2010-03-23 Thread Kyong Kim
I need to INSERT a row into a table only if it does not exist to insure that there won't be duplicate rows. Due to the the usage case and the design of the primary key, non-unique id + auto increment, I don't think insert ignore is an option. What would be simplest and cheapest way to make sure