It's a great goal to avoid making manual changes to your database. It never works 100%, though; there are always software bugs, unexpected interactions and other events that muck up your data, and you have to do one-off corrections. These are inherently hazardous: hard to test for unexpected data corruption, hard to apply consistently and hard to model the application behavior that results from them. Here are some strategies for the first issue, avoiding unexpected data corruption.
UPDATE Products SET Price=0.99 WHERE ProductId=2762 AND Price=1.00;
This ensures that if something else changes Price just before you run your change, you don't destroy that update.#4 can be a challenge if your verification statements are complex. Consider, for example, if you want to update rows in table A for which there is exactly one row for a particular CustomerId. It's relatively easy to do a SELECT statement to verify this by hand:
SELECT
CustomerId,
COUNT(CustomerId)
FROM A
WHERE
CustomerId IN (15, 16)
GROUP BY CustomerId;
To verify this at UPDATE time, however, you either need a subselect or an intermediate table. We'll use the latter:
CREATE TABLE ScratchTable AS
SELECT
CustomerId,
COUNT(CustomerId) AS Customers
FROM A
WHERE
CustomerId IN (15, 16)
GROUP BY CustomerId;
UPDATE A
JOIN ScratchTable USING (CustomerId)
SET Updated=1
WHERE
A.Id=3
AND Customers=1;
The same trick works if your data change inserts new rows:
INSERT INTO A (CustomerId)
SELECT CustomerId
FROM ScratchTable
WHERE
CustomerId=15
AND Customers=1;