i want to be able to update the filed 'activated' to 0 when the 'expireDate' is before today.
can you show me how to do it with a triger? (i want to do this after the user insert the row)
id expireDate activated
1 2011-11-21 1
2 2011-04-22 1
It will look something like this (tested on MySQL 5.1.37):
CREATE TRIGGER my_trigger
BEFORE INSERT ON activity
FOR EACH ROW
IF NEW.deadline < CURDATE() THEN
SET NEW.activity = 0;
END IF
Related
I am making an events system.
Let's say an event is "Go dancing" September 12th 2022.
When an event is added to the database we make a unix timestamp on one of the rows.
We use default value unix_timestamp() to do this
This timestamp say 654213987 is used as part of the url so people can sign up to event 654213987.
People can sign up here for example.
http://myevents/signup/654213987
The event organiser writes a description of the event on September 12 2022.
Next year the event organiser wants to do the same event but does not want to rewrite the descriptions. Just duplicate or clone the original without deleting the original.
This would be easy to do programmatically with PHP but I am using XCRUD which I can't modify so my only option is to use triggers or some hard wired part of MYSQL.
When XCRUD makes the duplicate it uses a normal INSERT with a copy of the original minus the primary.
If I make the column UNIQUE it does not allow the clone.
If not it makes a duplicate of the timestamp.
Is it possible to make a trigger (or some other mechanism) which would recognise that there is a duplicate and replace the duplicate with another fresh timestamp?
I have seen on stackoverflow that it is possible to add timestamps with triggers but I just can't figure out how to do this to avoid duplicates.
A sample.
CREATE TABLE test (id INT, ts DATE);
CREATE TRIGGER tr_bi_test_add_1_week
BEFORE INSERT ON test
FOR EACH ROW
BEGIN
WHILE EXISTS ( SELECT NULL
FROM test
WHERE ts = NEW.ts ) DO
SET NEW.ts = NEW.ts + INTERVAL 1 WEEK;
END WHILE;
END
INSERT INTO test VALUES (1, '2022-01-01');
-- inserted as-is
SELECT * FROM test;
id
ts
1
2022-01-01
INSERT INTO test VALUES (2, '2022-01-02');
-- inserted as-is
INSERT INTO test VALUES (3, '2022-01-15');
-- inserted as-is
SELECT * FROM test;
id
ts
1
2022-01-01
2
2022-01-02
3
2022-01-15
INSERT INTO test VALUES (4, '2022-01-01');
-- 2022-01-01 is present => 2022-01-08
SELECT * FROM test;
id
ts
1
2022-01-01
2
2022-01-02
3
2022-01-15
4
2022-01-08
INSERT INTO test VALUES (5, '2022-01-01');
-- 2022-01-01, -08, -15 is present => 2022-01-22
SELECT * FROM test;
id
ts
1
2022-01-01
2
2022-01-02
3
2022-01-15
4
2022-01-08
5
2022-01-22
fiddle
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.
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.
I have an event table. (I didn't create this table)
The fields are id, event_id, start_date, end_date, e_status
The only thing that is not unique is the id. The rest are the same on all rows.
How do I update the status?
I tried:
UPDATE events
SET e_status =
CASE event_id
WHEN 12830 THEN 0
END
WHERE start_date = '2016-06-24 17:30:00'
AND end_date = '2016-06-24 18:00:00'
AND event_id IN (12830)
No updates were changed.
before editing
ID Event Status
1 USA 0
2 UK 0
3 Canada 0
Your simple query would be
update event set status=1
After Editing
ID Event Status
1 USA 1
2 UK 1
3 Canada 1
and if you wants to target specific record just use where clause
update event set status=1 where id=1
If all the data is the same on all rows except for the id column, you shouldn't need any criteria in the WHERE clause. You should be able to just have this:
UPDATE events
SET e_status =
CASE event_id
WHEN 12830 THEN 0
END
And actually, you don't even need the case statement. If all data is the same except for id, then won't all rows have an event_id = 12830? You can just have ==>
UPDATE events
set e_status = 0
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