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.
Related
I don't have much experience with MySQL so not sure if it's issue with MySQL or my code.
I have a table lets say data and it has a created_at column of DATETIME type.
This table gets 20-30 new records per second, no updates at all.
I have a Cron job that runs every 15 minute and tries to get all records created in the last 15 minutes.
if it runs at 10:15:06am and the last run was at 10:00:03, it makes this query:
SELECT * FROM `data` WHERE (created_at >= '2021-07-30T10:00:03Z' AND created_at < '2021-07-30T10:15:06Z')
Current time is excluded, hence the created_at < current_time.
But the problem is, once in a while I get duplicate data error. That is it includes a few data rows from the current_time, which should have been excluded.
Like in this case, if 15 records were inserted at 10:15:06, the query result might have 4-5 records included in it. But it does not happen every time.
I am using Golang and for current time, I use time.Now(). Can this be because of millisecond or something else ? I am not making more than 1 database query, so I think it has to do something with DB, if I have extra records.
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
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
My dataset is a table with 3 rows : ID of a hard-drive, percentage of empty space, timestamp. The table is appended with the new state of each HDD (1200 of them) every 20 minutes.
If I want to pick the last state of my HDD pool, I go for a MAX(timestamp), and a MIN(timestamp) if I want the oldest.
But say I have a given timestamp, how can I ask MySQL to retrieve data from more or less X seconds around this timestamp ?
WHERE yourTimeStamp
between TIMESTAMPADD(SECOND,-3,yourtimestamp)
and TIMESTAMPADD(SECOND, 3,yourtimestamp)
where -3 and + 3 was substituted for X
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampadd for more help.
Like this:
WHERE timestamp BETWEEN DATE_SUB('<given_timestamp>', INTERVAL 5 SECOND)
AND DATE_ADD('<given_timestamp>', INTERVAL 5 SECOND);
As mentioned in the other answer, your query is slow when selection is based on the timestamp field.
You can add an INDEX on that column to speed it up:
ALTER TABLE <table_name> ADD INDEX(`timestamp`)
Note that, depending on the size of your table, the first time you add an index it takes a while. Secondly, it slows down INSERT queries and adds to the size of your database. This is different for everybody so you just have to find out by testing.
I have a TIMESTAMP column containing:
2011-10-12 12:00:00
Now I want to be able to take user input to change the time only, and not the date. So, if the user inputs 5:00 pm I first convert the input to 17:00:00, then my problem is UPDATING the TIMESTAMP to 2011-10-12 17:00:00 without overwriting the date. The user can pick any time, but they are restricted to this day, and I need to store the date and time in the same column like this because it's already used in many other parts of the application.
Note: I would prefer not to SELECT the TIMESTAMP first before updating if it's possible to UPDATE just the TIME portion without a SELECT.
I looked at DATE TIME functions and there are lots of ways of adding an interval to the time, but I don't see anyway to set it to something specific.
update table
set datetime_field = concat_ws(' ',date(datetime_field), '17:00:00') where id = x
UPDATE tab
SET ts_field = ADDTIME(DATE(ts_field), new_time)
WHERE id = ?;