MySQLTalk.org Forum Index MySQLTalk.org
MYSQL discussions groups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

INSERT RECORD IF NOT EXISTS OR UPDATE

 
Post new topic   Reply to topic    MySQLTalk.org Forum Index -> MySQL Replication Discussion
View previous topic :: View next topic  
Author Message
Djellil
Guest





PostPosted: Sat Dec 03, 2005 12:43 pm    Post subject: INSERT RECORD IF NOT EXISTS OR UPDATE Reply with quote



Dear MySQL developers and users,

I'm replicating data from db Anywhere to MySQL. So I wish to know how I can
write this code in MySQL (does MySQL have a way to INSERT
a new record IF one doesn't exist (not INSERT IGNORE, ON DUPLICATE KEY
etc.).


My source in T-SQL (db Anywhere):
____________________________________________________________________________
______________________

IF NOT EXISTS (SELECT 1 FROM tab WHERE colcode = 'xxx') THEN
INSERT INTO tab (colcode, colname, colversion) VALUES ('tr1', 'MyTree',
'version: 1')
ELSE
UPDATE tab SET colname = 'MyTree', colversion = 'version: 1' WHERE colcode =
'tr1'
END IF
____________________________________________________________________________
______________________

I'm looking forward to hearing from you. Thanks!

Petr Dolansky


Back to top
Kristen G. Thorson
Guest





PostPosted: Mon Dec 05, 2005 5:23 pm    Post subject: Re: INSERT RECORD IF NOT EXISTS OR UPDATE Reply with quote



Djellil wrote:

Quote:
Dear MySQL developers and users,

I'm replicating data from db Anywhere to MySQL. So I wish to know how I can
write this code in MySQL (does MySQL have a way to INSERT
a new record IF one doesn't exist (not INSERT IGNORE, ON DUPLICATE KEY
etc.).


My source in T-SQL (db Anywhere):
____________________________________________________________________________
______________________

IF NOT EXISTS (SELECT 1 FROM tab WHERE colcode = 'xxx') THEN
INSERT INTO tab (colcode, colname, colversion) VALUES ('tr1', 'MyTree',
'version: 1')
ELSE
UPDATE tab SET colname = 'MyTree', colversion = 'version: 1' WHERE colcode =
'tr1'
END IF
____________________________________________________________________________
______________________

I'm looking forward to hearing from you. Thanks!

Petr Dolansky






I believe INSERT IGNORE does do what you wish. Normally, trying to
insert a duplicate record will cause an error and the record will not be
inserted (and the whole statement will be aborted). The IGNORE
statement suppresses only the error. The record is still not inserted
if it is a duplicate.

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

"If you use the |IGNORE| keyword in an |INSERT| statement, errors that
occur while executing the statement are treated as warnings instead. For
example, without |IGNORE|, a row that duplicates an existing |UNIQUE|
index or |PRIMARY KEY| value in the table causes a duplicate-key error
and the statement is aborted. With |IGNORE|, the row is still not
inserted, but no error is issued. Data conversions that would trigger
errors abort the statement if |IGNORE| is not specified. With |IGNORE|,
invalid values are adjusted to the closest values and inserted; warnings
are produced but the statement does not abort. You can determine with
the |mysql_info()| C API function how many rows were actually inserted
into the table."


kgt




Back to top
Display posts from previous:   
Post new topic   Reply to topic    MySQLTalk.org Forum Index -> MySQL Replication Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.