List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:August 18 2005 3:02am
Subject:Re: INSERT record IF NOT EXISTS?
View as plain text  
At 19:10 -0700 8/17/05, Daevid Vincent wrote:
>Does mySQL have a way to INSERT a new record if one doesn't exist (based
>upon primary compound key)?

Isn't that how INSERT works already?

If what you mean is that you want no error to occur, perhaps you want
to use INSERT IGNORE instead.

http://dev.mysql.com/doc/mysql/en/insert.html

But note that this requires your index be UNIQUE or a PRIMARY KEY,
which isn't true of your table below.

>
>I see this "EXISTS" but not an example of how to use it with INSERT.
>
>I see "INSERT... ON DUPLICATE KEY UPDATE col_name=expr" which is very close,
>but I want it to do nothing on duplicate key. :(
>
>mysqladmin  Ver 8.40 Distrib 4.0.24, for pc-linux-gnu on i386
>
>CREATE TABLE `release_test` (                            
>   `BID` int(10) unsigned NOT NULL default '0',           
>   `ReleaseID` smallint(5) unsigned NOT NULL default '0', 
>   `Tested` tinyint(1) unsigned NOT NULL default '0',     
>   `CoreID` smallint(3) unsigned NOT NULL default '0',
>   KEY `BID` (`BID`,`ReleaseID`),                         
>   KEY `ReleaseID` (`ReleaseID`)
>) TYPE=MyISAM;

-- 
Paul DuBois, MySQL Documentation Team
Madison, Wisconsin, USA
MySQL AB, www.mysql.com
Thread
INSERT record IF NOT EXISTS?Daevid Vincent18 Aug
Re: INSERT record IF NOT EXISTS?Jasper Bryant-Greene18 Aug
Re: INSERT record IF NOT EXISTS?Paul DuBois18 Aug
Re: INSERT record IF NOT EXISTS?Steve Edberg18 Aug