updated according to the time zone - mysql

I have the program below whose goal is to update col2 every day at 23h59min59s. the problem is that we have accounts all over Africa and europe. I would like the program to run every day at 23:59:59 depending on the timezone of each account. For example, if the account is in France, the DBMS should automatically change the value of col2. if the account is in cameroon and it is 23h, col2 must be updated.
DROP EVENT IF EXISTS `updateColumn`;
DELIMITER $$
CREATE EVENT `updateColumn`
ON SCHEDULE EVERY 1 DAY STARTS '2019-04-15 23:59:59' // I Want 23h59min59s du timezone
ON COMPLETION PRESERVE
DO BEGIN
update ma_table set col2=col1;
END$$
DELIMITER ;
the column is actually updated but the update dates aren't good.

I think, that You need to add information about record timezone in the row (ACCOUNT). I.e. column GMT_TIMEZONE_INFO. Then in this event update on each hour where Server time + GMT_TIMEZONE_INFO is midnight(or whatever time you want).

Related

Adding days to monthly events in MySQL

DELIMETER $$
CREATE EVENT loan_balance ON SCHEDULE EVERY '1' MONTH AND '5' DAYS
DO BEGIN UPDATE users SET loan_balance = total_loans_collected -
total_loans_paid;
END$$
DELIMETER;
Hello! I want to update this table on the 5th of every month and not just monthly. The "AND '5' DAYS isn't making it work.
You need a slightly different ON SCHEDULE phrase.
AT '2018-04-01 03:01' + INTERVAL 5 DAY EVERY MONTH
fires your event at 03:01 local time on the fifth day of every month.
03:01 is a good time for a scheduled job because it doesn't get messed up by standard-time / daylight-time switchovers.
Instead of using a trigger to update the loan_balance column, you can turn it into a generated column.
ALTER TABLE users MODIFY COLUMN loan_balance INT GENERATED ALWAYS AS
(total_loans_collected - total_loans_paid) STORED;
Unless I'm missing something, I believe this is the most optimal approach, and it is certainly easier to maintain.

How To UPDATE 2 MySQL Tables With Server Event

I'm creating a website for a simple poll from registered users. Every month, registered users only allowed to vote 5x for a poll.
Now, i'm going to create a server event with HEIDI SQL on 2 tables which will reset their 5 times votes on each month to 0
Here's my exe code:
CREATE EVENT `resetvalue`
ON SCHEDULE
EVERY 1 MONTH STARTS '2017-10-01 00:00:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Im going to reset this every month'
DO BEGIN
UPDATE tbl_users SET statusmonth = 0
UPDATE tbl_poll SET status1 = 0
UPDATE tbl_poll SET status2 = 0
END
Is this the correct code?
Unfortunately, I asked my friend and then he gave another code that look like this:
DELIMITER $$
CREATE EVENT `dbname`.`resetvalue`
ON SCHEDULE EVERY 1 MONTH
STARTS '2011-06-21 01:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
/(sql_statements)
END$$
DELIMITER ;
I got confused, which one is the correct code? What is a DELIMiTER anyway?
Below is a difference for your event with respect to your friend's event.
Your event will run on 1st of every month at 12:00 am and start date of event is 2017-10-01 00:00:00 but your friend's event will run on 21st at 12:00 am of every month and start date is 2011-06-21 01:00:00.
Your event will not deleted once event is completed. But your friend's event code will be removed once it will done. Here both events are never ending it's affect only if it's 5 times to run or you disable an event.
Other then this I think it's same. For more details you can refer MySQL event scheduler documentation.
https://dev.mysql.com/doc/refman/5.7/en/events-overview.html

MySQL dynamic date change

I want to create table with name "dynamicdate" in which first column name is "datecolumn" which will contain 15 rows i.e. first row should represent today's date for example 08/08/2017 and following column shows subsequent date for example, 08/09/2017, 08/10/2017 till 15th row contains 08/23/2017.
Question 1: How do I fill 15 rows in a column with consecutive date simultaneously.
Now, for example date becomes 08/09/2017 (because august 8 is over) is the today'date and 08/24/2017 is date of 15th day.
Question 2: How do i update database everyday dynamically i.e. without querying database.
This you can do by creating a job. Every morning or night schedule this Job.
A job will execute this procedure "p_Update_dynamicdate".
create proc dbo.p_Update_dynamicdate
as
Begin
Declare #date as datetime, #count as int
set #date =getdate()
set #count =1
truncate table dynamicdate --Delete old data
while #count<=15
Begin
insert into dynamicdate(Ddate)
select Dateadd(d,#count,getdate())
set #count=#count+1
End
End
Solution to problem #1 :
Already solved on StackOverflow :
MySQL - How can I add consecutive dates to many existing records?
Solution to problem #2 :
Please look at Schedule and events on MySql documentation :
https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
syntax: CREATE EVENT my_event
ON SCHEDULE
EVERY 1 DAY
STARTS '2014-04-30 00:20:00' ON COMPLETION PRESERVE ENABLE
DO
# Your Update query
Please refer to below links for similar problem solution :
https://dba.stackexchange.com/questions/64208/scheduling-an-event-every-day-at-a-given-time
How to schedule a MySQL query?

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

How do I clear old entries from a MySQL database table?

I have a MySQL database with one big table in it. After a while, it becomes too full and performance degrades. Every Sunday, I want to delete rows whose last update is older than a certain number of days ago.
How do I do that?
Make a Scheduled Event to run your query every night. Check out Event Scheduler as well.
CREATE EVENT `purge_table` ON SCHEDULE
EVERY 1 DAY
ON COMPLETION NOT PRESERVE
ENABLE
COMMENT ''
DO BEGIN
DELETE FROM my_table WHERE my_timestamp_field <= now() - INTERVAL 5 DAY
END
What is the table design? Do you have a column with a timestamp?
Assuming you do, you could use that timestamp value with a datediff(your_date,CURDATE()) in a delete command.
Delete from table where datediff(date_col, CURDATE ()) > your_num_days.
Self Answer
Make a web server that sends the following SQL to the database every weekend:
DELETE FROM table WHERE timestamp < DATE_SUB(NOW(), INTERVAL 7 DAY);
or
DELETE FROM table
WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))
I might need locking to prevent accumulation of jobs, like so:
DELIMITER //
CREATE EVENT testlock_event ON SCHEDULE EVERY 2 SECOND DO
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
DO RELEASE_LOCK('testlock_event');
END;
IF GET_LOCK('testlock_event', 0) THEN
-- add some business logic here, for example:
-- insert into test.testlock_event values(NULL, NOW());
END IF;
DO RELEASE_LOCK('testlock_event');
END;
//
DELIMITER ;
Final answer:
CREATE EVENT `purge_table` ON SCHEDULE
EVERY 1 DAY
ON COMPLETION NOT PRESERVE
ENABLE
COMMENT ''
DO BEGIN
IF GET_LOCK('purge_table', 0) THEN
DELETE FROM table WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY));
END;
Maybe you can provide more information on how you are pushing the data to the DB and how you are working on the DB in general? Therefore we can help you and don't have to struggle with the dark...
I'll provide an easy solution: It's kind of workaround, but works:
Everytime you touch the data you update a time stamp in the updated rows.
Therefore you could easily filter them out every sunday.
UPDATE
The answer, the author provided by himself, was discussed at Stackoverflow and seems not to work in exactly that way, compare the discussion.