MySQL - Change a row value using a timer - mysql

I have been developing ban system with temporary banning system and I'm looking if there's anyway to update a row to specific value using a timer within MySQL. For instance,
I need my field banned to be set to 0 where the rowid is X.
UPDATE mytable SET banned=0 WHERE rowid=X
- But this query has to be done within X minutes or seconds specified, like if we could delay it.
Banning a user temporarily for 60 seconds, query sent at that time:
"UPDATE mytable SET banned=1, unbanon=NOW()+60 WHERE rowid=X".
Could I possibly get MySQL to automatically set banned variable to 0 after 60 seconds has passed?
Thank you.

Set a cron job to run a query every minute that will do something like :
UPDATE mytable SET banned=0 WHERE unbanon <= NOW() AND banned=1

Related

How can i update a mysql db column that only depends on a timestamp?

Lets say I have a Table tbl_Room with a column taken (boolean) and a Customer wants to rent this room for a short period.
Now, can I tell mysql to change the value of taken automatically depending on the timestamp, e. g. if the rent time/period is over, the value of taken should set automatically to false.
Or do I need to update my database with CRON or some other script that runs on the server periodically?
Please use mysql event to manage it.
CREATE EVENT [IF NOT EXIST] event_name
ON SCHEDULE schedule
DO
event_body
Reference
Under event_body you can write select statement to check period and then update table if period is over.
The best way to handle this sort of time-based request is counterintuitive.
Don't try to update the table at a specific time. Instead, include a timestamp column called something like lease_expires_at.
When you rent a room, update the row to set the value of lease_expires_at to the time at which the rental period expires. For example, if you rent a room for 30 minutes, starting now, do this.
UPDATE room
SET lease_expires_at = NOW() + INTERVAL 30 MINUTE
WHERE room_number = whatever
If you want to know whether a room is presently (NOW()) taken, do this:
SELECT room_number,
CASE WHEN lease_expires_at IS NULL THEN 0
WHEN lease_expires_at <= NOW() THEN 0
ELSE 1 END taken
FROM room
WHERE room = whatever
If you want to know whether a room will be available one hour from now (NOW() + INTERVAL 60 MINUTE), do this:
SELECT room_number,
CASE WHEN lease_expires_at IS NULL THEN 0
WHEN lease_expires_at <= NOW() + INTERVAL 60 MINUTE THEN 0
ELSE 1 END taken
FROM room
WHERE room = whatever
Then, once in a while, but not in any time-critical way, you can clean things up using a query like this
UPDATE room SET lease_expires = NULL WHERE lease_expires <= NOW()
You can use an event, or an overnight cronjob, or whatever you wish, to do this cleanup. The integrity of your application doesn't depend on exactly when this job runs.
The advantage of this should be clear: If you rely on some regularly running process to set an taken column value, and that process doesn't run or runs late, you get bad results. When you rely on the time, you get accurate results.
There's a small boundary-condition detail in this design. By using <= in my queries, I'm choosing to have the lease_expires_at timestamp represent the very first moment at which the room is available for another lease, not the last moment of the present lease. That's a handy choice, because if you put something like 2017-11-2017 11:00:00 into lease_expires_at, and somebody says "is the room available at 11:00?" you want to be able easily to say "yes." The guy who rented it at 10:30 gets it until the moment before 11:00.
you can use jquery time picker....after u can create a if loop in which JavaScript time function will check current time...to its original time...if condition is satisfied...we can change the mysql taken function

phpmyadmin logic command to unpublish records in table stored before set period of time

I have date as DB column
Can someone advise on a command in phpmyadmin which will unpublish all records more than 6 month old
For Instance -
v_id : unique record value given to every record
Adddate : Date field - values stores as 2015-11-23 09:39:28 (Year-Month-Date Time)
v_status : 1 means published and 0 means unpublished
Table name xyz_used
What command can i run in my phpadmin so that it can unpublish i.e make V_id record status i.e. v_status as 0 having adddate as before 2015-05-31 in table xyz_used
Pls am not aware of command at all - hence asking question for help
thanks
first of all phpmyadmin is a tool or webapp to access mysql databases. you can say it is a client utility.
Second
To update a table you will need to write Update Sql Query where you can put the date and can set the status.
query would be like
Updaate <tbl> set v_status = 0 where datefield < date_sub(now(), interval 6 month);

mysql check when a row was last updated

I am writing a MySQL Database and I wish to do the title within the MySQL database itself via Events if possible. I am updating the database via a Windows Service written in VB.net and have no desire to do the below via a separate 'server control' type program.
Basically, I have written the below in to the Database, but it doesn't quite do what I want:
Create Event MachineOffline
On
SCHEDULE Every 2 Minute
Do
UPDATE maindb.monitortable SET Online='1';
I would like it to say something like the below:
Create Event MachineOffline
On
SCHEDULE Every 2 Minute
Do
IF **RowX updated < 2 minutes ago** then
UPDATE Database.Table SET Online='0' where RowX(ID);
I have no idea how to achieve this hence my question to everyone. I have no desire to create
If you give your Database.Table an extra column, lets say, last_updated timestamp, you can then update it like so:
update Database.Table
set online=0, last_updated = now()
where last_updated <= now() - interval 2 minute
and online=1

Updating field value on MySQL table after 24 hours automatically

I have a table that stores some sensitive information but I would like that information to change to NULL when it exceeds 24 hours. How can I do that? I have a column named "last_updated" and stores value like this "2014-02-26 16:25:58".
How can I compare the last_updated value with the current time and if it exceeds 24 hours, the other field will change to "NULL".
Should I put something like UPDATE table SET info=NULL WHERE last_updated > 24hour? I don't know how to compare the last_updated when its 24 hours later.
or is there a function inside MySQL to check automatically without running the query using phpmyadmin?
You can do this with a scheduled job that resets the data. Of course, the time span would be 24-48 hours to the change, if you run the job only once per day.
There is another option. That is to do all the data access via views. Then the view could say:
create view v_table as
select (case when last_updated > now() - interval 1 day then col1 end) as col1,
. . .
from table;
Then, you can then update the data at your leisure -- if you still find that necessary. Access to the data won't be dependent on a job and job scheduler. If all accesses to the data are through the view, then after 24 hours, the data will appear as NULL.

Update a value using MySQL Events

I would like to create an event in MySQL in order to change a value in a table. The event checks whether a date (particularly, the TIME() part of a TIMESTAMP field) is more than the current date. Obviously, I have a time_stamp and an active column in a table named 'active'. This is what I have
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT #time:= time_stamp FROM active WHERE user_id = 1;
SELECT #time2:= TIMESTAMP(#time);
SELECT #active:=TIMEDIFF(#time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=if(TIME_TO_SEC(TIME(#active))>=0,1,0) WHERE user_id=1;
As far as I have seen, the SELECT part with the variables works fine, that is, TIME_TO_SEC(TIME(#active)) looks like a regressive count starting with 15 (provided I updated the 'time_stamp' field properly).
The intended behavior is that when TIME_TO_SEC(TIME(#active)) reach 0, the UPDATE query would change the value of the 'active' field from 1 to 0 (the default value is 1). However, it doesn't do anything.
UPDATE: To be precise, sometimes it changes the value to 0 but when TIME_TO_SEC(TIME(#active)) is still positive.
SECOND UPDATE: I recently tried this one:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT #time:= time_stamp FROM active WHERE user_id = 1;
SELECT #time2:= TIMESTAMP(#time);
SELECT #active:=TIMEDIFF(#time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=0 WHERE user_id=1 AND TIME_TO_SEC(TIME(#active))>=0;
which didn't work either.
THIRD UPDATE: I used the suggestion by Adam and I even if it doesn't work as expected, it certainly changes the value 'active' to 0, immediately after I start the event. Ideas?
SOLUTION: Thanks to Adam, I used his code with an IF statement:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
Do
UPDATE active AS t
SET t.active=IF(TIME_TO_SEC(TIMEDIFF(
t.time_stamp, DATE_ADD(NOW(),
INTERVAL -15 SECOND)))>=0,1,0)
WHERE user_id=1
which it works as intended. I don't why his suggestion didn't work, though.
Where did I make a mistake?
Of course, another approach to get this functionality would be greatly appreciated.
Thanks in advance.
What is the value in active.time_stamp where active.user_id = 1?
Also, I think that your statement might be written more simply.
If time_stamp in fact contains timestamps then you don't need the call to TIMESTAMP().
Remove TIME() function from UPDATE query because TIMEDIFF(expr1,expr2) returns expr1 – expr2 expressed as a time value.
So this is how I wrote your statement:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
UPDATE active t
SET t.active = 0
WHERE t.user_id = 1
AND TIME_TO_SEC(
TIMEDIFF(
t.time_stamp, DATE_ADD(NOW(),INTERVAL -15 SECOND)
)
)>=0;
I've tested that when the value in active.time_stamp is greater than now, this event sets the value in active.active to 0.
If that still isn't working you might also try something very basic like:
CREATE EVENT update_status_test
ON SCHEDULE EVERY 1 SECOND
DO
UPDATE active t
SET t.active = 0
WHERE t.user_id = 1;
And if that still isn't working then make sure the event scheduler is actually running. There are several ways to start it, this is one: SET GLOBAL event_scheduler = 1
Other notes:
This may not matter now, but as time goes on you will eventually run into the upper-limit of the TIME type in MySQL.