how to perform a 'quiet' insert?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to perform a 'quiet' insert?

Post by jasongr »

Hello

I was wondering if it is possible to perform a 'quiet' insert in MySQL.
Here is what I mean by a 'quiet' insert.

if row with ID 1 already exists in a table, then its value should be updated using UPDATE TABLE, otherwise it should be added using INSERT INTO

It is possible to perform a SELECT to see if the row exists and then to do an UPADTE or INSERT based on the results but it seems like a non-efficient way to do it.

Is there a way to do it in a single command?
The command will update the table if the row already exists, or insert it otherwise

thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[mysql_man]REPLACE[/mysql_man]
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

thanks
this is a very usefull command
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Feyd is a man of few words..
But wise these words are
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

remeber

Post by phpScott »

just remeber last time a checke replace wasn't a standart sql command so portablity might be an issue if you plan to move your code.
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

I also found the following:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

Maybe it will be useful to some people
Post Reply