WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-03-2006, 01:38 PM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,722
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default Add/Update SQL Query

Hello everyone. I am pretty much of a novice with MySQL, and I am trying to find out if there is a single query that will update an existing record, or if the record does not exist, create the record. I know I could do somthing like
Code:
$query = "UPDATE table SET name = 'new' WHERE id = '123'";
$result = @mysql_query($query);
if (!$result) {
   $query = "INSERT INTO table (name, id) VALUES ('new', '123')";
   $result = @mysql_query($query);
}
But is there a way to accomplish the same thing with a single database query?
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog
Reply With Quote
  #2 (permalink)  
Old 10-06-2006, 01:27 AM
brian.mark's Avatar
Administrator
 

Join Date: Jul 2004
Location: Omaha
Posts: 2,717
brian.mark RepRank 2brian.mark RepRank 2
Default

Sure. The action is "REPLACE INTO".

Quote:
Originally Posted by MySQL.com
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
Source

Brian.
__________________
ToolBarn.com, an Internet Retailer Top 500 and Inc. 500 Company | Tool Parts | Pet Supplies
Reply With Quote
  #3 (permalink)  
Old 10-06-2006, 06:20 AM
WebProWorld Pro
 

Join Date: Sep 2005
Location: Manchester, UK
Posts: 257
mikesmith76 RepRank 0
Default

Quote:
Sure. The action is "REPLACE INTO".
Keep in mind though that this is MySQL specific (AFAIK). May be a pitfall if you ever need to move to another database system
Reply With Quote
  #4 (permalink)  
Old 10-09-2006, 02:44 PM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,722
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default

Ok. Thanks!
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog
Reply With Quote
  #5 (permalink)  
Old 10-09-2006, 07:31 PM
mushroom's Avatar
WebProWorld Veteran
 

Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 351
mushroom RepRank 0
Default

Quote:
Originally Posted by brian.mark
Sure. The action is "REPLACE INTO".

Quote:
Originally Posted by MySQL.com
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
Source

Brian.
Not always the best thing to use as any fields that are not included in the query are set to their default values.
__________________
Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.

Both Security and SuSe start with "S". www.oldslides.com
Reply With Quote
  #6 (permalink)  
Old 10-10-2006, 12:45 AM
brian.mark's Avatar
Administrator
 

Join Date: Jul 2004
Location: Omaha
Posts: 2,717
brian.mark RepRank 2brian.mark RepRank 2
Default

Quote:
Originally Posted by mushroom
Quote:
Originally Posted by brian.mark
Sure. The action is "REPLACE INTO".

Quote:
Originally Posted by MySQL.com
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
Source

Brian.
Not always the best thing to use as any fields that are not included in the query are set to their default values.
Good point there. As I said, the old row is deleted before the new row is inserted. No values come across.

Brian.
__________________
ToolBarn.com, an Internet Retailer Top 500 and Inc. 500 Company | Tool Parts | Pet Supplies
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0