I need assistance trying to find a problem where 1 record/row was updated but the entire table got updated with the same value. Need to understand how this could have happened and implement a method to prevent it from doing so again.
update TABLE set pushID='1234567890' where userID='111222333' ;
What would that update statement do if the userID value equaled nothing or equaled NULL? Could that cause the update statement to update every single row with the same pushID?
IE: update TABLE set pushID='1234567890' where userID='' ;
Could a blank userID value cause this? If not, what could cause this? If so, how could I write the query statement to prevent this from happening again?
What would that update statement do if the userID value equaled nothing or equaled NULL?
If the userID is NULL, then the condition becomes userID = NULL. This will always evaluate as false. In other words, no record will be updated.
If the userID is the empty string, then the condition becomes userID = ''. This will only update records where userID is equal to the empty string. I would expect that userID is the primary key of your table, so it would be suprising the find an empty value. And even if there is one, it will be unique, so a unique record will be updated.
As you see, none of the two above use case would generate a massive update of the table. The most probable option is that the query was triggered without an actual WHERE clause, like:
update TABLE set pushID='1234567890'
In case these are altogether integer ID, you should not convert them to string with ''. Doing so may lead to unexpected results (never tried, but it seems to be a possible cause for the comparison in the WHERE condition to fail). For example:
UPDATE TABLE SET pushID=1234567890 WHERE userID=111222333;
When all records are being updated, the query lacks the WHERE condition. Make sure to have posted the correct query, because this query should update nothing when the WHERE condition fails to match.
Related
How to do the following:
if table has column 'name' - update 'name', else - do nothing?
My working sql in a transaction is
UPDATE tmp set name = NULL
tmp table kees the record to update and turning name into NULL is required for duplicate procedure. However, some tables do not have 'name' field.
So, is it possible to update column to null if exists, otherwise, just do nothing?
As far as I know it is not possible. Moreover I would recommend never make any modifications on a number of tables at once. In general each table is unique and setting fields to NULL basing on just a field name could lead to loss of important data.
Like:
INSERT INTO `video_play_counts`
(`id`,`video_id`,`date`,`count`,`created`,`modified`)
VALUES
("",1,"2016-12-01",26,"2016-12-03 17:51:53","2016-12-03 17:51:53")
ON DUPLICATE KEY UPDATE
`count` = GREATEST(`count`,VALUES(`count`)),
`modified` = IF(VALUES(`count`) > `count`,VALUES(`modified`),`modified`)
So, I have a unique key on video_id and date and when I make an UPDATE on this unique key, in the case that the new count value is bigger the existing one, I would like to also update the modified field accordingly.
The count field updates as expected but the modified field does not get the new value in the case of an UPDATE.
Please note that I'm using this to do multiple insert/update in one query, just in this example it has only one set of values.
What am I doing wrong ?
I have not tested, but i am nearly sure that you must switch the last 2 line of your query, so that you first set modified and the set countto VALUES(count), else count is set to the GREATEST and never greater count
Are there any performance gain with this query:
UPDATE tbl SET field = 1 WHERE field != 1
over this
UPDATE tbl SET field = 1
Does the SQL parser already know that he doenst' need to update row that already are field = 1 ?
The condition field != 1 will likely make it faster especially in the case where most values are already 1. Assuming an index is available for optimizing, the database engine will be able to avoid examining most of the records in that case.
More importantly, perhaps, is that the queries may not have the same result. If any of the field values are NULL, the first UPDATE statement will not update those values. The second query would set the NULLs to 1.
Another (fairly obvious) case where they would not be equivalent is that UPDATE triggers would fire for all records for the second query (without the condition) but they would not fire for the first query for the skipped rows.
If there are just a few fields which are != 1, then there are most definitely performance gains in adding the WHERE clause. Even if MySQL did not write the value to disk, it still would need to look at every row to see if it needs to write it or not, but it certainly does not add the WHERE clause by itself.
I'd like to know how to update several records at once where a certain column type is selected.
I have found how to select records. I have found how to update records. But i don't know how to do it both together for it to work.
Selecting:
SELECT * FROM users WHERE 'type'='new'
Updating:
update table
set column = 937
So basically i want to change the info in the 'column' to 937 in the 'table' if another column 'type' is 'new'.
Thanks,
You can do this by simply adding a WHERE clause to your UPDATE statement:
UPDATE `users`
SET `myColumn` = 937
WHERE `type` = 'new'
Of course, change myColumn to match your column name
You can do this with a subquery :
update users
set column = 937
where id in (select id from users where type='new')
Just change the columns name if I got them wrong.
After researching for a while I found another solution to this problem. When referencing the same table or field name in a query, the name space in MySQL ends up with duplicates and fails. To overcome this use the "AS" syntax to name the duplicate items. Also, update queries often require a key field to be used as the parameter for the where clause, this is often your auto-incremented ID field. This example provides a solution for both of these problems.
UPDATE tbl
SET field=newValue
WHERE tbl.key IN
(SELECT idNew FROM
(Select tbl.key AS idNew
FROM tbl
WHERE field=editValue) AS tblNew);
tbl - Table name being updated
field - Target field for update
newValue - Value to replace items in the target field
tbl.key - Field name for the key in your table
idNew - New name for key in your table, to replace the name and prevent failure from duplicating names in query. could be any alphanumeric string. 'id' or 'id_new' or 'id_n'
editValue - The value to change
tblNew - New name for query, to prevent duplicate table names in the query. Could be any alphanumeric string 'tbl_n' or 'tbl_new' or 'tbl_test'
This query gets the key values for all the records that match records that have values you want edited, then changes the names of the key and tbl so they can be processed by MySQL, lastly the update query runs and changes the values based on the keys provided in the sub-query.
Hopefully this saves someone else a few hours!
I have this query:
UPDATE phonecalls
SET Called = "Yes"
WHERE PhoneNumber = "999 29-4655"
My table is phonecalls, I have a column named PhoneNumber. All I want to update is a column named Called to "yes".
Any idea what I am doing wrong? when I return my query it says 0 rows affected.
If the such value already exists, mysql won't change it and will therefore return "0 rows affected". So be sure to also check the current value of called
Another reason for 0 affected rows that I have observed: wrong data type. If the column you want to update is an integer or boolean, and you set it to a string, it won't be updated - but you will also get no error.
To sum up the other strategies/ideas from this post:
Check with a SELECT statement, whether your WHERE works and returns results.
Check whether your columns do already have the value you want to set.
Check if your desired value suits the data type of the column.
If the values are the same, MySQL will not update the row (without triggering any warning or error), so the affected row count will be 0.
The problem might be that there are no records with PhoneNumber == "999 29-4655".
Try this query:
SELECT * FROM phonecalls where PhoneNumber = '999 29-4655'
If it doesn't return anything, then there are no rows that match.
For the benefit of anyone here from Google, this problem was caused by me because I was trying to append to an empty field using CONCAT().
UPDATE example SET data=CONCAT(data, 'more');
If data is NULL, then CONCAT() returns NULL (ignoring the second parameter), so the value does not change (updating a NULL value to be a NULL value), hence the 0 rows updated.
In this case changing to the CONCAT_WS() function instead fixed the problem.
Try select count(*) from phonecalls where PhoneNumber = "999 29-4655"; That will give you the number of matching rows. If the result is 0, then there isn't a row in the database that matches.-
Check to make sure this returns some result.
SELECT * FROM phonecalls WHERE PhoneNumber = '999 29-4655'
If it doesn't return any result than the filter WHERE PhoneNumber = '999 29-4655' is not correct.
Does it say Rows matched: 1 Changed: 0 Warnings: 0? Then maybe it's already set to that value.
Did you try single quotes vs. double quotes?
"999 29-4655" is the space a space or a tab and is it consistent in your query and the database?
That's my sugestion:
UPDATE `phonecalls` SET `Called` = 'yeah!' WHERE `PhoneNumber` = '999 29-4655' AND `Called` != 'yeah!'
And make sure with the case-sensitive name of table and field`s.
Just ran into an obscure case of this. Our code reads a list of records from the database, changes a column, and writes them back one by one. The UPDATE's WHERE clause contains only two conditions: WHERE key=? AND last_update_dt=?. (The timestamp check is for optimistic locking: if the record is changed by another process before we write ours, 0 rows are updated and we throw an error.)
But for one particular row the UPDATE was failing- zero rows effected.
After much hair-pulling I noticed that the timestamp for the row was 2019-03-10 02:59. In much of the U.S. that timestamp wouldn't exist- Daylight Savings Time causes the time to skip directly from 2:00 to 3:00. So I guessed that during the round trip from MySQL to Java back to MySQL, some part of the code was interpreting that timestamp differently from the rest, making the timestamps in the WHERE clause not match.
Changing the row's timestamp by one hour avoided the problem.
(Of course, the correct fix is to abolish Daylight Savings Time. I created a Jira but the U.S. Government has not responded to it yet.)
In my case, I was trying to update a column of text to correct a truncation problem with it. Trying to update to the correct text was yielding 0 rows updated because the text in the row wasn't changing.
Once I extended the column in the table structure to accommodate for the correct number of characters, I was able to see the desired results.