I have a table loaded in MySQl workbench that I would like to refresh on a daily basis as the data changes.
Is there a way to refresh the data already held in SQL without having to remove the table and reload a fresh copy?
If you want to update the table every day in MySQL Workbench you can create an event.
Something like this
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL recurring Event',NOW());
From Source
Related
I have a mysql database with a table storing logs. This table receives records every 10 second.
I would like to truncate my table every week, that means after 80.000 records. I'd like to do a stored procedure which checks at every insert if there are more than 80.00. If there are more, the table is truncated.
I can't find how to use the if statement with SQL.
I know it would be better to do a script in a crontab to connect to the db and delete the table once a week, but i have the constraint to do it directly by SQL stored procedure.
Thank you in advance for any help !
You can do this with MySQL Event Scheduler, which is basically like cron, but runs as a thread within the MySQL Server. With it you can schedule the execution of an SQL statement or procedure.
First you have to turn it on:
SET GLOBAL event_scheduler = ON;
Then you can create an event:
CREATE EVENT MyEvent
ON SCHEDULE EVERY 1 WEEK
DO
TRUNCATE TABLE MyTable;
Read more about it here:
https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
https://dev.mysql.com/doc/refman/5.7/en/create-event.html
I am trying to find the best way to process a record with a timestamp field whereby it would automatically move from one table to another table after 30 minutes.
If you are using MySQL 5.1.6 and later, check out MySQL events.
Events work the same with other routines, you can CREATE, ALTER, and even DROP them.
Martin Psinas has basic examples of this. (Just found online, we're not friends, lol)
First, you'll have to enable events scheduler:
SET GLOBAL event_scheduler = ON;
Then, you can create your event like (base from your question):
CREATE EVENT ProcessRecord
ON SCHEDULE EVERY 30 MINUTE
--STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
--ENDS CURRENT_TIMESTAMP + INTERVAL 1 YEAR
DO
BEGIN
INSERT INTO ...
SELECT ...
END
Otherwise, you can go with windows scheduled tasks, if you're using windows platform.
I'm creating a database where i want to create a trigger.
This trigger will delete a row if the current time is 5 min over the requesttime in the table.
The requesttime is a datetime attribute in the table.
How can i make a trigger which do this?
You don't want to do this. First, triggers only run when something is happening inside the database (such as an insert or update). They are not the same as scheduled jobs. Second, you generally don't want to do this in a scheduled job. Instead, just create a view:
create view v_table as
select t.*
from table t
where requesttime >= now() - interval 5 minute;
This will return valid requests.
Then, at your leisure, delete the old rows -- if you really need to. There is a good chance you might want to keep them around to know what happened in the past. But you can delete them on Sunday morning at 3:00 a.m. or some other slow time, if you want.
You may want to consider Event Scheduler added in MySQL 5.1.6. Events can be seen as an alternative to Cronjob.
Their main advantages are:
Platform independent; directly written in MySsql
Ability to list all events SELECT * FROM INFORMATION_SCHEMA.EVENTS;
Built in error logging option
Syntax is something similar to:
CREATE EVENT my_event
ON SCHEDULE
EVERY 1 MINUTE
STARTS '2015-01-01 00:15:00'
COMMENT 'Removes forgotten password tokens older thank 1 week.'
DO
DELETE FROM my_table WHERE some_timestamp > NOW();
How can we trigger a mysql trigger when the system date is changed? Let's say
We have a mysql table and it has a dateTime column and at a specific time of each day we need to update that dateTime column according to a specific condition. So is it possible to achieve this task in mysql?
How can we trigger a mysql trigger when the system date is changed?
You cannot create this trigger. There are only table INSERT/UPDATE/DELETE triggers.
One more thing - as I know, the MySQL server should be restarted after system date changing.
I want to remove a table row from my table new_data once the row is 45 mins old and then input it in another table called old_data.
The only way i can think for this to work, it to query the database lets say every min and remove any row thats (current_time - time inserted) > 45 mins.
Is there any other way of doing this? if not how could i set up a table to record inserted_time?
edit added
How could i write this statement to retrieve the correct data into the old_data table
SELECT * FROM new_spots WHERE (NOW()-created_at)>45mins
and then insert the above into the old_data table
you can specify value of time column upon insertion:
INSERT INTO x (created_at) VALUES (NOW());
additionally you can setup VIEW to show you only recent entries.
you are asking for some kind of auto expiration feature, it is not built into mysql. Memcached provides this feature. So it might be cleaner to achieve your goal as:
when you insert data into your system, you do:
insert your data into memcached with 45 minutes expiration time -- after 45 minutes, the data automatically disappear from memcached.
insert the data into the old_data table with a created_at column -- in case you need to rebuild your memcached when your memcached have to restart or other issue.
So everytime you just need to get the new data from the memcached -- as a side effect, it is faster than get the data from mysql :).
#keymone showed you how to capture the insert time. Then, periodically (every minute seems excessive - every 5 mins? 15 mins?) go through and build a list that meets the criteria, and for each entry, insert into your second table and delete from your first table.
I don't think there is an automatic way to do this. Here are some alternative ideas.
Use CRON
I have a similar scenario where we need to aggregate data from one table into another. A simple command line tool running via CRON suffices. We receive a few messages a second into our Web server and each results in a database insert. So volumes aren't huge but they are reasonably similar to your scenario
We use the NOW() function to record the insert time and after the records are 1hr old, we process them. It isn't exactly an hour but it is good enough. You can see the created_on field below.
CREATE TABLE glossaries (
id int(11) NOT NULL auto_increment,
# Our stuff ...
created_on datetime default NULL,
KEY owner_id (owner_id),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Use CRON and a Trigger
Alternatively you could use a database trigger to kick off the processing. You would still need something scheduled to cause the trigger to fire but you would get max performance/
Chris