SQL query that is bugged in a way I don't understand - mysql

I know the title is weird, but I am on that same bug for HOURS.
I have this query
UPDATE tournaments SET password_req_count = password_req_count + 1 WHERE id = 20;
(You can replace 20 by anything, it really does not matter)
This query modifies a timestamp field called start_timestamp by ALWAYS setting it the computers current hour.
And this query is perfectly fine, there's no bug with this :
UPDATE tournaments SET password_req_count = 0 WHERE id = 20;
This was happening in the PHP code until I removed that one query, then it stopped. Then I decided to try it by directly executing the query myself, without PHP, and the bug is still here.
password_req_count is an int (I mean, I checked it, the problem isn't here)
This query does not appear in the query history of MySQL (the one you can get by pressing the "UP" key to remake a query quickly...), and this bug doesn't appear locally (it only appears on my server). Note that I exported my local database to the server's one, so everything is exactly the same there and here.
The MySQL server version was 5.5 on my server and 5.7 at home, I thought this was the problem so I updated it, but absolutely nothing changed. I also googled a lot about this, but I found no topic talking about this subject.
I do have query logs, so I am SURE that there is NOTHING that edits the start_timestamp (except this weird bug obviously). It is not supposed to be edited anyway.
Edit : I just edited the field name to password_request_count because password_req_count already exists in another table. But the bug is still here.
RECAP HERE
Edit 2 : Here is a video because apparently the post is not clear enough. Notice that I can't do the UPDATE query again by pressing the "up" touch, and please also notice that start_timestamp gets edited if I increment password_req_count.
http://www.nx-lab.com/bug.mp4
Edit 3 : Apparently this also happens if I edit other fields (such as top_prize)

There could be a couple of things doing this, one is a trigger on the table. This code will show you if there are any...
SHOW TRIGGERS FROM tournaments;
The other thing, which is the correct answer in this case, is an auto update on the datetime column. This causes the date and time in the column to be updated automatically when there is an update to the table.
You can read more about it here:
https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
If you want to remove to auto update then an ALTER table is required to remove it from the column, from Timestamp without change on update...
ALTER TABLE leads MODIFY added_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

Related

Can I use ON UPDATE on a MySQL Table for String - VARCHAR Column

I am developing a web application, where my database is updated through the application Front End.
However, sometimes, due to some errors/ support, I do update my database from queries.
Like, for Last Modified Time column, the timestamp is automatically updated using the, 'ON UPDATE CURRENT_TIMESTAMP', so I have accurate information regarding time of update.
Similarly, is there a way, I can change a column which stores Last Modified By (username) of the user, using the 'ON UPDATE' or anything similar on the VARCHAR field, so that, whenever it is updated from the BackEnd, the Last Modified By column is automatically updated as 'SYSTEM'.
Now the database has correct time of update, but Last Modified By column, stores the username of the user who actually last updated it from the Front End and not something that says it was modified through a query, which is causing a bit of confusion.
Hence, doing this will help me avoid confusion that the Entry was modified from the backend and not the application.
As I read in many places, the ON UPDATE was only used on TimeStamps and did not find anything similar to UPDATE Varchar fields.
Thanks for the help!

MySQL error column cannot be null although default value set

I recently moved my SQL Database to another Amazon RDS server with version 5.7.
Before that, the application was working fine but now I started logging errors:
"ER_BAD_NULL_ERROR: Column xyz cannot be null" - The column already has a default value CURRENT_TIMESTAMP
I checked online and people suggested to have the sql_mode equal to NO_ENGINE_SUBSTITUTION
I checked the existing settings and it is already like that.
Any other reason I am getting this error? Any tricks?
Thanks.
After searching more, the problem was only in timestamp fields with current_timestamp default value. I searched in the parameters and found explicit_defaults_for_timestamp that was enabled (value 1) and with a bit more research, I had to disable this parameter as per the documentation here
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
in order to get the required result and fix the problem.
Simply deactivate explicit_defaults_for_timestamp
SET GLOBAL explicit_defaults_for_timestamp = 0;
I have no idea why it works like that in this particular case, so I would concentrate on fixing a problem.
According to the docs NO_ENGINE_SUBSTITUTION has nothing to do with the error, during application run. I would select rows with column "xyz" of NULL value and update it to something - not null.
Default is applied when row is created. Let's say you have a table with some millions of rows, and want to add column with not null. That would block your table for significant amount of time. So you can create column without not null, but with default. That operation deals only with metadata, so is fast. Default will deal with all new rows. After that you can slowly update all rows. At the end not null constraint can be added. Not sure if DB is checking constraint when adding it at last step. Or maybe prev. version had problem with it? With MySQL things like that happens.

Debbuging SQL query

I have a problem debugging some problem.
I started thread in wordpress.stackexchange.com thinking that I get more wordpress related debugging suggestions but went with totally different way.
You can see topic here: https://wordpress.stackexchange.com/questions/123394/some-ways-to-debug-code
with update: DELETE FROM wp_bp_activity WHERE item_id = 0
My Question is SQL related: Can DELETE statement be triggered from something then DELETE query? for example I rememeber having a bad query which was deleting everything. (but that was everything and not an if statment)
So to extend this question: If I search every query with DELETE FROM will I find it for sure? Can this be written differently? Because for now, I can't find it.
TRUNCATE is also used to DELETE all the records from a table.
You can also have some kind of foreign key cascading, that is triggering the delete in that table. More here.
Addicionally, make sure to search on the database also, on triggers and on stored procedures
One other option is that they were updated to a different value than the one you are looking for.

MySQL 5.1.65 | Select Where Not Finding Something I know Is There

I'm trying to use this disgusting version of MySQL (5.1.65) where its requiring EVERYTHING to be in quotes, and is so strict about everything. I'm trying to look for duplicate entries so I'm trying to make a script that if it returns a value of what the user submitted, then obviously we have a duplicate. Well thats working on MY server, not this one.
This is what I'm trying to execute. This goes through fine without error,
SELECT * FROM `keylist` WHERE 'key' = '7489asdf32749asdf8237492asdf49837249'
The issue is that 7489asdf32749asdf8237492asdf49837249 IS in the database as a varchar...Its exactly the same. But the weird thing is that if I do
SELECT * FROM `keylist` WHERE key = '7489asdf32749asdf8237492asdf49837249'
That does not work while if I did that for selecting my ID it works.
I have no idea what I'm doing wrong, but this stupid MySQL version sure isn't helping.
Overall, my issue is that I'm trying to look for a duplicate, so I do a WHERE key = statement, and a key that IS in there, returns nothing when I KNOW its in there.
key is a reserved word in MySQL - http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-0.html
Your first version works becomes the backtics - ` ` force interpretation to the actual column name.

Using MySQL triggers to update table with last_modified data

How can I use trigger to simply update a table with the last time a table was edited? I know that by using triggers it is "for each row" but if someone's inserting more than one row, it'd be pointlessly inserting or altering the table over and over again. Is there any way to do this without doing it over and over again?
I'd like to be able to just have it do it once for all of the inserts instead of having it done time and time again. If not I guess I can force it, via a wrapper.
edit 1:
Well to explain some more of the design I guess then.
I'm going to be having a table in another database to handle the last_updated data for things like chat, or the players "mailbox", and another one for the development things like tables for quests, skills, items etc. And I want to be able to know when a table was last updated so that I can easily see before I go scan the table to see for new things.
Basically this is what I'd like to do(or something similar), I'm also using PHP so it's likely to be PHP-based approach in the code but the SQL should be kind of standard. I'm not going to do full code but rather semi-runnable.
last_modified=mysql_query("select last_modified from various_stats.table_last_updated where database_name=`database_name` and `table_name`");
if(last_modified>last_checked_time){
data_to_get_updated=mysql_query("select something from various_<something>.table_name where last_modified>last_checked_time");
}
else{
do_nothing;
}
edit 2: I'm using InnoDB, and thus I cannot use the information schema's update_time since it never changes.
will this help you, if im on the right track that is:
SELECT UPDATE_TIME
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tabname'
The above solution is for myisam, for innodb the norm is to set a sceduled script, this can be set as a cron job or a windows scheduled task, if you dont have that kind of control over your web host, you could possibly set up a small server at your work office and run the cron from there. if you do this every say 20 seconds you could simply record the current top auto incremented ID and use this as a guid, if current ID is higher than the last recorded ID you then update your records to show the last changed time to be now.
as this will only be one call to a server every XX seconds, it wont really hammer the server too much and should just run silently in the background.
If you do go down the scheduled task root, it would be wise to add error capture in your script so that you can be alerted via email if something stops working etc.