MySQL trigger delete field after a period on update - mysql

I want to automatically delete recovery_url from table users 30 minutes after it's been updated. Is there any way of doing this?
Thanks

One way I can think of is using an additional column indicating how long the recovery_url is valid. It can be a datetime column and in your queries you can select the recovery_url depending on that date. So you don't need any triggers or events.
select case when url_valid_until < now()
then null
else recovery_url
end as recovery_url
from your_table

Related

MYSQL query disallowing a timestamp column having two values from the same date

For example if a user inserts '2017-03-13 12:16:18.0' into the timestamp column,
the same user should not be allowed to enter another value in this column IF IT'S ON THE SAME DAY i.e 2017-03-13 (in this case). Or ultimately, update the timestamp column with the previously inserted value ('2017-03-13 12:16:18.0') each time the user tries to insert a timestamp date twice ON THE SAME DAY. I hope I've been explicit enough.
Below is a non-functioning query I came up with, but it shows what I would like the query to do ultimately. Thanks for your help and feedbacks.
INSERT INTO hr.entry(id,entry_time)
VALUES (45,
CASE WHEN '13-03-2017'= CAST(SYSDATE() AS date) THEN
(UPDATE hr.entry
SET entry_time =
(SELECT entry_time
FROM hr.entry
WHERE id=45
AND CAST(entry_time AS date)= CAST(SYSDATE() AS date) )
ELSE
SYSDATE());
You could add a DATE column to your table, and add a unique index to that column. Then, when you insert the timestamp into the timestamp column, you could also insert the date from that timestamp into the DATE column. Attempts to insert a timestamp whose date component already exists in that table would cause MySQL to throw an error.
I think you are going to need a trigger, unless you store the timestamp as a string using YYYY-MM-DD HH:MI:SS format. I don't really recommend that.
So, create a trigger that updates a column called timestamp_date. This simply extracts the date part of the timestamp.
With this column, you can define a unique index:
create unique index entry_userid_timestampdate on entry(userid, timestamp_date);
This will then enforce your condition.
If you decide that you want to store the timestamp as a string, you don't need the trigger (although will need to manually set the "timestamp"). Instead, you can use a prefix:
create unique index entry_userid_timestampstr on entry(userid, left(timestamp_date, 10));

Does MariaDB store a timestamp when a given row is inserted?

I am dealing with a legacy application that is using MariaDB to emulate a queue. One of the key things missing is that the original design doesn't insert the time the messages in the queue were inserted meaning that the order the messages are processed is not guaranteed.
So far the messages appear to be processed in order as we're only using a single MariaDB instance but I would like to add a created_on column to ensure this continues.
My question is that I need to backfill the created_on column and i was wondering if MariaDB stored the time a given row was inserted into the database?
I realise that unless it is in the schema it is unlikely but occasionally databases will have non-standard extensions that capture this sort of thing. Oracle for example has similar functionality to this.
MariaDB does not have a hidden timestamp. If the table has an AUTO_INCREMENT, that might suffice since you are asking for order, not specifically time.
My opinion of queuing via MySQL/MariaDB: "Don't queue it, just do it". The effort of queuing and dequeuing can become a burden, especially in end cases.
Yes you can, if you were to create a field make sure when you create the field you have the following:
create table test_created_on_table(
created_on timestamp default now() on update now()
);
If you already have a field just take off the "CURRENT_TIMESTAMP" flag on the created field. Whenever you create a new record in the table, just use "NOW()" for a value.
Or.
On the contrary, remove the 'ON UPDATE CURRENT_TIMESTAMP' flag and send the NOW() for that field. That way actually makes more sense.
This would track when row is inserted or updated.
There's another way of doing it by db trigger:
Adding a ModifiedTime
Adding a modified timestamp to a table is the most straight forward. All your have to do is create the field of type TIMESTAMP, and by default, MySQL will automatically update the field when the row is modified.
There are a couple of things to be aware of:
While you can have multiple TIMESTAMP fields in a row, only one of
these can be automatically updated with the current time on update.
If your UPDATE query contains a value for your ModifiedTime field,
this value will be used.
So, to add your modified timestamp field to an existing table, all you need is:
ALTER TABLE my_table ADD ModifiedTime TIMESTAMP;
Adding a CreatedTime
Adding a CreateTime value is a little more involved.
On the latest versions of MySQL it is apparently possible to create a DateTime field with a default value of CURRENT_TIMESTAMP. This wasn’t an option for me as I was having to support a somewhat older version, besides, even on the newer versions of MySQL it is not possible to have more than one field using CURRENT_TIMESTAMP, which of course we are in order to get ModifiedTime working.
So, in order to get a created timestamp, firstly we must add a DATETIME field to the table.
ALTER TABLE my_table ADD CreatedTime datetime NOT NULL;
Note, that this must be created as NOT NULL in order for the next part to work (this is because setting NOT NULL forces an automatic all zeros default).
Next, we must create a trigger, which will automatically be fired when we insert a value into our table and set the created timestamp.
DELIMITER //
DROP TRIGGER IF EXISTS my_table_insert_trigger//
CREATE TRIGGER my_table_insert_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF NEW.CreatedTime = '0000-00-00 00:00:00' THEN
SET NEW.CreatedTime = NOW();
END IF;
END;//
DELIMITER ;
Now, when you insert a value into the table, this trigger will fire and, if you’ve not provided a CreatedTime field in your insert query, it will be set to the current time stamp.

Automatically insert timestamp if there isn't any in the table

I have a table where I have a long list of timestamps and there could be chances of timestamps missing for an entire day or days and I want to insert a single timestamp for that particular day
For example if there is no timestamp for 2014-04-04 I want it to insert 2014-04-04 00:00:00 for that particular day.
Can this be done in mysql ?
Yes it can.
Add a trigger for after insert to the table. Make the trigger insert the current timestamp if none has been provided.
Or change the column to have a default value of CURRENT_TIMESTAMP.
Those don't resolve the problem of previous entries having no values, but it does fix the problem for the future. Filling in the rest should be a straightforward update: update mytable set its_timestamp = date(datefield) where its_timestamp is NULL.

mysql event to change a fields value when a date is reached

What I am trying to accomplish is this:
I have table1 which contains user_id,group_id(int with a default value set) and expire_date.Also table2 which between others has a field user_group_id which serves as foreign key to group_id of table1.
When the date is reached I'd like to change values of group_id and user_group_id to default.
Unfortunately it seems I can't figure my way around this since I'm really new to mysql.
Table1 will contain like 500 rows max.Probably the event won't be used to update more than 4-5 rows per run.
Automated alternative solutions are welcome.
mysql 5.2.7
php 5.3.8
CentOs 6
Thanks in advance for any responces!
You need a statement like
update table1 set group_id=<default> where expire_date() > now();
You can run this update query from a cronjob or from a trigger or from a mysql event (http://dev.mysql.com/doc/refman/5.1/en/events.html)
Did it with event.
CREATE EVENT event_name2
ON SCHEDULE
EVERY 24 HOUR
DO
UPDATE test.employees
SET `group`=DEFAULT
WHERE expire_date <= now( )
Firstly i thought to use triggers but they occur only when something is changed on database witch wasnt the case here.

How to select the time between NOW() and last row inserted?

What i basically need is a query that will allow me to display the time that has passed between last row inserted and the current time.
The specific table uses the timestamp field type.
I know that DATE_SUB must be used here, but i have no idea how to in this particular problem
Assuming that table has a timestamp field which records the time-of-insertion for each record, then
SELECT TIMEDIFF(now(), max(timestampfield))
FROM yourtable
select min(now() - timestamp) from table