MySQL trigger to create timer - mysql

There is a special data type 'time' in MySQL.
How would a trigger look if I want my 'time' value to start counting when some state_id changes from 1 to 2? For example:
CREATE TRIGGER log_time AFTER UPDATE ON usr
FOR EACH ROW
BEGIN
IF usr.st_id = 2 THEN
#.... - thats what i dont know
END IF;
END;
It would stop counting when the state_id changes back from 2 to 1.

Instead of starting/stopping the counter (I don't know if that's even possible), you should store the value in 2 different columns (and then substract to get the time when needed)
DELIMITER $$
CREATE TRIGGER log_time AFTER UPDATE ON usr
FOR EACH ROW
BEGIN
IF new.st_id = 2 THEN
UPDATE <table> set <log_start_time> = CURTIME() <where_clause>;
elseif new.st_id = 1 THEN
UPDATE <table> set <log_end_time> = CURTIME() <where_clause>;
END IF;
END;
$$
DELIMITER;
OR in 1 column by storing initial value and then updating it in the trigger
DELIMITER $$
CREATE TRIGGER log_time AFTER UPDATE ON usr
FOR EACH ROW
BEGIN
IF new.st_id = 2 THEN
UPDATE <table> set <logtime> = CURTIME() <where_clause>;
else if new.st_id = 1 THEN
UPDATE <table> set <logtime> = subtime(CURTIME(), select statement to get original value) <where_clause>;
END IF;
END;
$$
DELIMITER;

Okay I'm new to coding and was wondering if this would even be possible and if so what would be the best way to get around this problem
Okay in my database I have a row for current time and a row for duration and I am wanting to find a way so that when the time is the value of T + D it would change a colour (green) ? Or even better if it could be done so if equal to or under 2 mins amber colour and over 2 mins red colour ( kind of like a traffic light idea)
Hope this makes sense
T | D
---------------
22:50 | 6 (mins)
At 22:56 this would change colour on website
Thank You

Related

Is updating with inline select atomic in mysql?

I have an app that calculates points for a user, based on certain rules, for every dollar they spent on my portal. I am using the following stored procedure to update the points of the user:
DROP PROCEDURE IF EXISTS `Update_Points`;
DELIMITER $$
CREATE PROCEDURE `Update_Points` (
pUserId BIGINT,
pPoints DECIMAL(13,2), // points accrued for the amount 'pAmount'
pAmount DECIMAL(13,2) // amount spent by the user
)
SPROC:BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
INSERT INTO AuditTable(UserId, Amount, Points)
VALUES (pUserId, pAmount, pPoints);
UPDATE PlayerPoints
SET Points = Points + pPoints
WHERE UserId = pUserId;
SELECT ROW_COUNT() INTO #RowsUpdated;
COMMIT;
SELECT #RowsUpdated;
END$$
DELIMITER ;
I am seeing discrepancies in total points accrued by some of the users. I know this because we have another backend database that stores user transactions.
We have multiple instances of our applications running to handle the load so one idea was that it could this stored procedure that could cause the accrued points discrepancy, based on the idea that the following snippet might not be atomic
UPDATE PlayerPoints
SET Points = Points + pPoints
WHERE UserId = pUserId;
So my question to you gurus of mysql is whether the above UPDATE statement is atomic or not?
The PlayerPoints table has following structure:
UserId
Amount
Points
1
30
50
1
10
20
2
35
30
3
10
20
2
35
30

I want to add an attribute to an while condition on MySQL

i'm doing my first DB on mySQL and I want to make an underground like system. To start the system I want to make them go from one station to another on a fixed time of 2 seconds so I use the following code
delimiter |
CREATE EVENT runBlue
ON SCHEDULE EVERY 2 SECOND
DO
BEGIN
WHILE (SELECT Actual_Station FROM route WHERE Actual_Station) < 311 DO
UPDATE mydb.route SET Actual_Station = (Actual_Station+1);
END WHILE;
WHILE (SELECT Actual_Station FROM route WHERE Actual_Station) >= 300 DO
UPDATE mydb.route SET Actual_Station = (Actual_Station-1);
END WHILE;
END |
delimiter ;
What concerns me is that there is no increment to the Actual_Station that is represented by an INT, the reason why I am doing this is because the underground is divided in 3 routes, this is the example for the first one that goes form station 300 to station 311.

How to fix AFTER UPDATE Trigger

I created a User Point system for my website. The table looks like this:
user_id name article gallery description total
------------------------------------------------------------
1 joe 7 3 0 10
2 hary 3 5 5 13
3 ana 1 1 2 4
I need an AFTER UPDATE trigger that will make the update SUM in the column total when the value changes in the column, article, gallery or description
DELIMITER $$
CREATE TRIGGER total_trg
AFTER UPDATE point_system FOR EACH ROW
BEGIN
UPDATE point_system SET total = (article + gallery + description) FROM point_system WHERE user_id = NEW.user_id;
END
$$
DELIMITER ;
This trigger does not work. Why?
You cannot modify a table's contents from a trigger on that table (except for the row the trigger is operating on, but not in that way); in this case, what you need is a BEFORE UPDATE trigger, in which you would just
SET NEW.total = NEW.article + NEW.gallery + NEW.description;
You use NEW and OLD to access (and modify in BEFORE triggers) the field values of the row responsible for the trigger firing.

add a column to a table with 4 possible default value based on logical argument

How can I add a column to a table with the number 1 to 4 based on each row meeting certain criteria?
Suppose I have a table:
Like this
with two columns (ID) and (RESULTS) and that I want to add a third column called (SCORE).
I want to give a score (between 1 and 4) to each row in my column based on whether the numbers in column (RESULTS) meet certain criteria.
If the RESULT is negative, I want to give it a score of 1,
If the RESULT is less than 30, a score of 2,
less than 100 a score of 3
and greater than 100 a score of 4
I have tried using the CASE statement but cannot seem to get it to work;
I searched on the topics about constraints but they always seem to have two arguments - I need 4
I have updated the answer, as more details were given in the question (and to fix some errors)
Please remember to change table_name and trigger_name to appropriate names :).
SOLUTION:
You should first of all add the third column to the table
ALTER TABLE table_name ADD COLUMN SCORE INT;
You should add the trigger to set the SCORE for the new rows:
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
SET NEW.SCORE = CASE WHEN NEW.RESULTS < 0 THEN 1 WHEN NEW.RESULTS < 30 THEN 2 WHEN NEW.RESULTS < 100 THEN 3 ELSE 4 END;
END$$
DELIMITER ;
And you should initialize SCORE values for rows existing in the table
UPDATE table_name t
SET t.SCORE = CASE WHEN t.RESULTS < 0 THEN 1 WHEN t.RESULTS < 30 THEN 2 WHEN t.RESULTS < 100 THEN 3 ELSE 4 END;
Hope it helps.

insert data into one table from another table on specific condition

I have "table1" in which master data store like -
id name create_date validity expire_date
1 A 2015-08-01 3 2015-11-01
2 B 2015-09-01 12 2016-08-01
3 C 2015-09-15 1 2015-10-15
But now want to insert data in "table2" for expire_date according to validity period like without changing in front end. using trigger or procedure want to achieve this task.
id parent_id expire_date
1 1 2015-09-01
2 1 2015-10-01
3 1 2015-11-01
How can I achieve this using procedure or trigger.
It's hard to be specific because your question is not specific.
In general, here's the procedure to follow to design a query to insert stuff from one table into another.
First, write a SELECT query yielding a resultset containing the rows and columns you want inserted into your table. Use a list of columns to get the right columns, and appropriate WHERE clauses to get the right rows. Eyeball that query and that resultset to make sure it contains the correct information.
Second, prepend that debugged SELECT query with
INSERT INTO target_tablename (col, col, col)
Test this to make sure the correct rows are being inserted into your target table.
Third, create yourself an EVENT in your MySQL server to run the query you have just written. The event will, at the appropriate times of day, run your query.
If you take these steps out of order, you'll probably be very confused.
Can achieve the task using Store Procedure -
CREATE DEFINER=`root`#`localhost` PROCEDURE `addexpire`(IN uname varchar(50), IN cdate date, IN vm int)
BEGIN
insert into table1 (name,create_date,validity) values (uname,cdate,vm);
BEGIN
declare uparent_id INT;
declare v_val int default 0;
SET uparent_id = LAST_INSERT_ID();
while v_val < vm do
BEGIN
declare expire_date date;
SET expire_date = DATE_ADD(cdate,INTERVAL v_val+1 MONTH);
insert into table2 (parent_id,expire_date) values (uparent_id,expire_date);
set v_val=v_val+1;
END;
end while;
END;
END