SQL database command not updating - mysql

I am trying to execute this:
UPDATE WORKS_ON
SET Hours = 5.0
WHERE Essn=99988777 AND Pno=10;
and it threw an error,
19:07:29 UPDATE WORKS_ON SET Hours=5.0 WHERE Essn=99988777 AND Pno=10 Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect. 0.000 sec
I did what it asks and reconnected, now there is no error but doesn't do anything
19:36:26 UPDATE WORKS_ON SET Hours= 5.0 WHERE Essn=99988777 AND Pno=10 0 row(s) affected Rows matched: 0 Changed: 0 Warnings: 0 0.000 sec
I want all Hours for people that have Essn=999887777 and Pno=10. Hours will change to 5.0

I hope you have already unchecked safe updates in preferences.
From what you have posted it looks like there are no matching rows where Essn=99988777 and Pnp=10 at the same time.If you can provide sample data and expected results it would be better

First set SQL_SAFE_UPDATES=0, It’s because you try to update a table without a WHERE that uses a KEY column, if you use PRIMARY KEY then it would not create that kind of problem, let's try this way
SET SQL_SAFE_UPDATES=0;
UPDATE WORKS_ON
SET Hours = 5.0
WHERE Essn=99988777 AND Pno=10;
EDIT
you can modify your query to follow the rule (use PRIMARY KEY on WHERE clause), if it doesn't work, then precede the (table name with the schema name) like
UPDATE your_schemaname.WORKS_ON
SET Hours = 5.0
WHERE Essn=99988777 AND Pno=10;

Related

MySQL update returns 0 rows affected while select returns results

select * from t_circle
where status = 0 and author_phone = 13511111111
and id in (
1,
2,
3
)
;
returns 3 rows with status of 0.
But the following update query with same conditions returns 0 rows affected:
udpate t_circle set status = 2
where status = 0 and author_phone = 13511111111
and id in (
1,
2,
3
);
Is there a reason for this? I have tried start transaction and commit, but still 0 rows affected. Other questions' answers suggest to run select first and make sure the rows are changed since if new value == old value, it is considered affected. I have excluded these 2 possibilities.
Note: this issue is found in our production server(Yeah I know I shouldn't but I have to) with InnoDB engine. I could modify the contents in GUI client like DBeaver and click save and the changes take effect, but not with sql statements. I wonder if it has anything to do with my account authorization?
Resolved! It is because I misspelled UPDATE. When I use mysql> in command line, update with 'udpate' just gives Query OK, 0 rows affected. My mysql's version is 5.7
I know that there is something wrong with the user permission of your client
By comparing GUI generated sql queries when I change something in GUI and my own queries. I found out the reason is I misspelled update to udpate. Now I begin to wonder how could mysql not complain about syntax error and just returns with 0 rows affected?
Well, at least now I know what to do when something odd happens.

on duplicate key update id=last_insert_id(id) - did the INSERT actually happen?

I need to INSERT a new row into my TABLE(with unique field 'A'), if it already exists(duplicate field, insert failed) - just return the ID of the existing one.
This code works well:
insert into TABLE set A=1 on duplicate key update id=last_insert_id(id)
But now I have another problem: how do I know if the returning ID belongs to a new (inserted) row or it's just an old one?
Yes, I can do "SELECT id WHERE A=1" beforehand, but it would overcomplicate the program code, require two steps, and just looks ugly. Besides, in future I may want to remove some UNIQUE indexes, then I'll have to rewrite the program as well to change all the 'where' checks. Maybe there is a better solution?
[solved, see my answer]
Found this solution. It works in console, but doesn't work in my program (must be a bug in the client, idk) - so probably it will work fine for everyone (except me, sigh)
Just check the 'affected rows count' - it will be 1 for the new record and 0 for the old one
mysql> INSERT INTO EMAIL set addr="test" ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
Query OK, 1 row affected (0.01 sec)<----- 1 = INSERTed
mysql> select last_insert_id(); //returns 1
mysql> INSERT INTO EMAIL set addr="test" ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
Query OK, 0 rows affected (0.00 sec) <----- 0 = OLD
mysql> select last_insert_id(); //returns 1
UPD: it didn't work for me because my program kept sending the 'CLIENT_FOUND_ROWS ' flag when connecting to mysql. Removed it, now everything is fine!

MYSQL UPDATE date field not working

Why won't this work:
USE presentations_db; UPDATE presentations_tbl SET `date` = '2012-12-13' WHERE `date` = '2013-12-12'
I have tried everywhere I could and can't find an answer.
date is the field name so used back ticks as required. date is of DATE data type.
I managed to get it to run through the commandline. I clicked on "command line client" and it asked for a password. I then ran the sql statement and got the following result:
mysql> UPDATE `presentations_db`.`presentations_tbl`SET date_ = '2012-12-13' WHERE date_ = '2013-12-12';
Query OK, 16 rows affected (0.06 sec) Rows matched: 16 Changed: 16 Warnings: 0
When I tried to run the same query by simply running mysql though a shell it came up with an error that the db can not be edited by localhost, which is more explanatory than 'Query interrupted'. This seems to be a gripe previously covered in http://bugs.mysql.com/bug.php?id=67766.
It would be nice if somone can tell me what I am doing wrong in workbench gui. I normally do the following when trying to run queries. I click on 'Edit Table Data' and the select the database and tables. It seems like I can view and run select queries but not update queries.

Why does MySQL allow to update a NOT NULL column to NULL?

I'm running MySql in ubuntu 10.10. I created a table called 'employee' having 3 field names empno, name and salary. Inserted few entities. In the middle of the process i want to change salary attribute as 'NOT NULL'. I Alter the table as
ALTER TABLE employee MODIFY salary int(10) NOT NULL;
Query executed. I wanted to test by using command,
UPDATE employee SET salary=NULL;
Query OK, 15 rows affected, 15 warnings (0.06 sec)
Rows matched: 15 Changed: 15 Warnings: 15
also gave warnings " (Code 1048): Column 'salary' cannot be null "(Repeated for every row)
But when i saw my table , All salaries were Zeros('0').
Same queries result in error instead of warning in WINDOWS XP's MySql
I checked in both INNODB and MYISAM engines but same Result.
Please help me to know what happened beside processing.
You must not have SQL_MODE set to strict on you ubuntu installation.
Issue
SET SQL_MODE='STRICT_ALL_TABLES'
or add
SQL_MODE='STRICT_ALL_TABLES'
under [mysqld] to your my.cnf on Ubuntu.
I don't see the problem, you set the column to NOT NULL, (which doesn't allow NULL values) and now it won't let you set it to NULL, which would be the expected behaviour.
The reason you have 0s in your DB is because 0 would be the result of casting NULL to an int.

MySQL record UPDATE should fail but does not. Why?

here is an interesting situation.
I start a transaction with MySQL. My transaction involves 3 related queries.
Each query must succeed, and if not then none should be written to the database.
Now... on purpose, for the 2nd query...which happens to be an UPDATE query... I changed
the pk value identifying the record to be updated to an invalid (non-existing) PK value. I wanted the 2nd query to fail for testing purposes. The query is fine, it is just that the c_id value is wrong (the record I'm trying to UPDATE does not exits).
The problem is that the query is executed with an "OK"...
mysql> UPDATE tableX SET bal = 4576.99 WHERE c_id = 3789;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
This is a problem because the error (is error from my perspective since a key record that must be updated was not updated in a chain of related queries) was not caught and the transaction thus did not abort and rollback, instead the process goes on to the 3rd query which also succeeds and then the transaction is committed.
So, I find it strange that such an error is not caught by MySQL or not labeled an error by MySQL.
Any insights as to why or how to fix?
It is correct, 0 rows were updated.
If, for your logic, that is an error you should test the number of affected rows and then raise an error if that number is 0:
DECLARE count INT;
UPDATE tableX SET bal = 4576.99 WHERE c_id = 3789;
SELECT ROW_COUNT() INTO count;
IF count = 0 THEN
CALL raise_error;
END IF;
error will make the transaction rollback.
To raise an error just call a routine which doesn't exist as explained on this SO question:
How to raise an error within a MySQL function
further info about row_count():
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count