MySQL - Trigger on Insert HOUR('time') - mysql

I have a table that contains a field 'timeduration'. Currently when I select the 'timeduration' value I'm doing HOUR('timeduration') to get the time in hours. I would like to do this calculation when inserting the data into the table, so putting the hour value into it's own column.
| date | timeduration | hour
12-12-14 01:00:00 1
I've looked into triggers but not sure how to about this, or os there an easier way I'm missing?
Thanks for your help!

Yes, a BEFORE INSERT and BEFORE UPDATE trigger would do it. And that's the only way to get MySQL to automatically populate the value in the hour column, based on the value assigned to the timeduration column.
Here's an example a BEFORE INSERT trigger.
DELIMITER $$
CREATE TRIGGER mytable_bi
BEFORE INSERT ON mytable
FOR EACH ROW
BEGIN
SET NEW.hour = HOUR(NEW.timeduration);
END$$
DELIMITER ;
You'll likely also want a corresponding BEFORE UPDATE trigger, if you want to keep hour in sync when timeduration value is modified by UPDATE statement.

Related

MySQL automatic date generation

Firstly let me say I'm new to and just getting my head around MySQL and finding date manipulation has its challenges. It seems to me it should be possible to have three columns:
column A contains date member joined 2020-01-12 for example, mapped from a form.
column B contains the length of membership in years 1 or 5, currently entered manually
Then calculate expiry 'date A'+ 'integer B' Year inserted in column C on member creation or update
It's OK to do manually but I feel it should be something automatic.
If anyone can give me a start or point to a tutorial that might help I'd be grateful.
In MySql you can use DATE_ADD() funciton:
SELECT columnA, columnB, DATE_ADD(columnA, INTERVAL columnB YEAR) AS EXPIRY;
Use this web page as reference.
You could define triggers BEFORE INSERT and/or BEFORE UPDATE which will do the job for you:
BEFORE INSERT trigger:
DROP TRIGGER IF EXISTS before_insert;
DELIMITER $$
CREATE TRIGGER before_insert
BEFORE INSERT ON `my_table`
FOR EACH ROW
BEGIN
SET NEW.`valid` = DATE_ADD(NEW.`date`, INTERVAL NEW.`membership` YEAR);
END$$
DELIMITER ;
BEFORE UPDATE trigger:
DROP TRIGGER IF EXISTS before_update;
DELIMITER $$
CREATE TRIGGER before_update
BEFORE UPDATE ON `my_table`
FOR EACH ROW
BEGIN
SET NEW.`valid` = DATE_ADD(NEW.`date`, INTERVAL NEW.`membership` YEAR);
END$$
DELIMITER ;
It is quite enough to insert both date and membership values during insert (obviously) or to update membership value on already inserted record(s).
Your Column C is the valid column in both queries.

MySQL Trigger to auto insert week of the year and 001 (autoincrement)

I'd like to set a MySQL trigger to set an ID number with the following format:
week of the year + 001
For example 09002 would be the 9th week of the year and 002 would be the second order for that week.
Any help, comment would be greatly appreciated, Thanks.
USE CONCAT WITH MYSQL DATE FUNCTION WEEK
select CONCAT('0',WEEK(now()),'002');
+-------------------------------+
| CONCAT('0',WEEK(now()),'002') |
+-------------------------------+
| 029002 |
+-------------------------------+
1 row in set (0.05 sec)
DROP TRIGGER IF EXISTS TEST_INSERT;
DELIMITER $$
CREATE TRIGGER TEST_INSERT AFTER INSERT ON TEST FOR EACH ROW BEGIN
NEW.ID= CONCAT('0',WEEK(now()),'002')
-------------------- your insert statement ----------
END$$
Doing this with a trigger is a problem in MySQL because MySQL doesn't have a SEQUENCE object, like some other SQL databases have. You can't generate an incrementing id value in a trigger, it must be generated to populate an integer column that has the AUTO_INCREMENT option.
So here's an unconventional solution: don't think of using a trigger for this, use the primary key. At the start of each week, run a scheduled job to advance the next auto-increment number.
ALTER TABLE mytable AUTO_INCREMENT = WEEK(NOW())*1000;
You can do this from a cron job, or you can use the MySQL Event Scheduler.
I would also comment that you can't rely on the incrementing id being consecutive. You could have inserts that fail, or you could have some inserts be part of transactions that roll back, or you could have later deletions. All these would eliminate a row with a certain id value, and you shouldn't re-use id values.

PL SQL Trigger to update start time for row when a single column is updated

I'm fairly new to triggers and have already tried searching for a solution to my question with little results. I want to update a single row's start time column whenever it's active column is set to 1.
I have two columns ACTIVE (number) and START_TIME (timestamp) in my_table. I would like to create a PL/SQL trigger that updates the START_TIME column to current_timestamp whenever an update statement has been applied to the ACTIVE column - setting it to 1.
So far I have only seen examples for inserting new rows or updating entire tables which isn't what I'm looking to do. I'd have thought there would be a fairly simple solution to my problem.
This is what I've got so far from other examples. I know the structure of my solution is poor and I'm asking for any input to modify my trigger to achieve my desired result.
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (my_table.ACTIVE = 1)
begin
insert my_table.start_time = current_timestamp;
end;
\
you can use like this .it may help you
write the update query instead of insert query
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (new.ACTIVE = 1)
begin
update my_table set start_time =current_timestamp;
end;
I think it should be a BEFORE UPDATE, not AFTER UPDATE, so it saves both changes with a single action. Then you don't need the INSERT or UPDATE statements. I also added the "OF active" clause, so it will only start this trigger if that column was updated, which may reduce the workload if other columns get updated.
CREATE OR REPLACE TRIGGER routine_active
BEFORE UPDATE OF active ON my_table
FOR EACH ROW
BEGIN
IF active = 1
THEN
:NEW.start_time = current_timestamp;
END IF;
END;

MySQL trigger: turn UPDATE into UPDATE and INSERT?

I have several tables that I want to enforce versioning, and have an effective from and effective to date. Whenever an application or user writes an UPDATE to this table I want it redirected into two entirely new commands: UPDATE the targeted record so the EFFECTIVE_TO date is populated with current date and time, and INSERT an entirely new record with the updated attributes.
Is this possible to do with a trigger or do I have to keep controlling this externally with a Java application?
Something like this probably
delimiter |
CREATE TRIGGER versioningcontrol AFTER UPDATE ON yourtable
FOR EACH ROW
BEGIN
INSERT INTO yourtable(col1,col2,...) values(...);
END;
|
delimiter ;

How to automatically update database fields with random values in MYSQL

I understand how to fill a field with random values, but I need my field to automatically update with a random value for each new column. For some reason, I'm having trouble figuring out how to do this.
By the way, as it says above, this is for a database in MySQL.
Thanks everyone
You can use a trigger to update a specific colum after insert like this:
DELIMITER |
CREATE TRIGGER randtrigger AFTER INSERT ON your_table
FOR EACH ROW BEGIN
UPDATE your_table SET some_column = rand()
WHERE id = NEW.id;
END;
|
DELIMITER ;