Databases :: MySql :: Insert and/or update. |
|||
| By: RQuadling |
Date: 06/03/2003 00:00:00 |
Points: 100 | Status: Answered Quality : Excellent |
|
Simplistically, I have some data which equates to 1 row in table. If the row exists (I have a key which is unique for this data and is used as the primary key for the table), then I want to update some of the fields. If the row does NOT exist, I want to add all the data. What is the most effecient way to do this as an SQL statement? Can it be done in 1 statement? I am using PHP (if that helps). Regards, |
|||
| By: VGR | Date: 06/03/2003 03:20:00 | Type : Answer |
|
| ok first in two statements, I won't offense you in telling you how to do the check (with mysql_num_rows() for instance) of existence of the line, then either UPDATE or INSERT it :D Now in one statement only... Using IF EXISTS perhaps I thought soem time ago that an UPDATE would in fact do an INSERT if the key did not pre-exist, but I'm not sure... I would try if I were you. |
|||
| By: Squibi | Date: 06/03/2003 04:32:00 | Type : Comment |
|
| You have to use two statments. UPDATE won't do INSERT if key you choose don't exist. There is a resolution - you can prepare set of keys (for example in one loop), buy you have to control if number of records filled with data is lesser than number of keys - in that case you have to increase number of pre-created records in your table. IMHO - it's not a good idea, so do it if you REALLY have to use one statement. |
|||
| By: sumotimor | Date: 06/03/2003 06:39:00 | Type : Comment |
|
| REPLACE should fit yours needs : <A HREF="http://www.mysql.com/doc/en/REPLACE.html">http://www.mysql.com/doc/en/REPLACE.html</a> |
|||
| By: VGR | Date: 06/03/2003 19:23:00 | Type : Comment |
|
| YES ! That's what I was thinking of when stupidly fast-writing about UPDATE doing an INSERT :D hu hu hu stupid me |
|||
| By: TheFalklands | Date: 06/03/2003 19:37:00 | Type : Comment |
|
| REPLACE does not ever do an update - at most a DELETE and then an INSERT. So REPLACE would work if you dont care about the old row... |
|||
| By: sumotimor | Date: 06/03/2003 19:55:00 | Type : Comment |
|
| you're right ultraslacker for the delete and insert. but if you want to replace a row you shouldn't care about the old row. of course you could have problems if you have an autoincremented primary key and there is a foreign key based upon this key (the new record have a new autoincrement value). In all others cases (99% in my opinion) all should be fine. |
|||
| By: RQuadling | Date: 07/03/2003 22:00:00 | Type : Comment |
|
| The best method I've come up with is this ... // Does the item already exist? $sQuery = "SELECT COUNT(Item.ID) AS ItemCount FROM Items WHERE Items.ID=$iAIID;"; $result = mysql_query($sQuery) or die('Invalid check query - ' . mysql_error()); $row = mysql_fetch_assoc($result); mysql_free_result($result); $sAIDescription = addslashes($sAIDescription); if ($row['ItemCount'] == 0) { $sQuery = "INSERT INTO Items(ID,Description,Done) VALUES ($iAIID, '$sAIDescription',0)"; $aCount['Items']++; } else { $sQuery = "UPDATE Items SET Description='$sAIDescription' WHERE ID=$iAIID"; } if (($row['ItemCount'] == 0) || (strlen($sAIDescription) > 0)) { $result = mysql_query($sQuery) or die('Invalid update query - ' . mysql_error()); } Any advances? Regards, Richard. |
|||
| By: VGR | Date: 07/03/2003 22:36:00 | Type : Comment |
|
| that's exactly the path I would have followed. It's your (dispreffred) option "in two statement). That's what I wrote in the first answer. Checking for existence with mysql_num_rows() on a SELECT, then choosing either to INSERT INTO, either to UPDATE Classical solution to a classical problem |
|||
| By: RQuadling | Date: 11/03/2003 20:51:00 | Type : Comment |
|
| I would have preferred an atomic method. Ho hum! Thanks all. Richard. |
|||
| By: VGR | Date: 11/03/2003 21:17:00 | Type : Comment |
|
| I'm afraid there is none using pure SQL |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








