How to apply automated query after each insert with condition? - mysql

If I have a table like that:
id | name | points
------------------
1 | Hala | 50
2 | Asa | 60
3 | Hala | 1000
How can I apply automated query that applied after each insert but with condition
like this query but only to apply on the latest item after it is inserted
update points set points = points / 10 where name = 'Hala';

Try to create a trigger like this
CREATE TRIGGER MyTrigger AFTER INSERT ON MyTable
FOR EACH ROW BEGIN
SET NEW.points = NEW.points/10;
END

Related

Create Trigger : update table column by inserting data into another table rows

Please Help me to create triggers,
I have 2 tables of
data
+---------+------------+-------+-------+
| id | info_id | rate | qty | total |
+----+---------+------------+----------+
| 80 | 10 | 150 | 5 | 750 |
+----+---------+-------+-------+-------+
| 81 | 10 | 50 | 5 | 250 |
+--------------+-------+-------+-------+
info
+---------+---------------+------------+
| id | name | gtotal | dated |
+-----+--------+----------+------------+
| 10 | Hari | NULL | 2021-05-15 |
+---------+------------+---------------+
I want to create a trigger through phpmyadmin, as soon as data will be inserted, then info.gtotal will be updated by adding data.total from matched info_id from table name- data.
what will be the trigger if update on data happens too. I just want to create both triggers.
I am new with such, please help me. Any help is accepted.
You need to create two triggers one for insert and another for update.
CREATE TRIGGER `insert_trigger`
AFTER INSERT ON `data`
FOR EACH ROW
UPDATE info
SET gtotal = gtotal + new.total
WHERE id = new.info_id;
Here new.info_id will have the info_id value of newly insert record
CREATE TRIGGER `update_trigger`
AFTER UPDATE ON `data`
FOR EACH ROW
UPDATE info
SET gtotal = (gtotal - old.total) + new.total
WHERE id = new.info_id;
Here old.total will have the total value of record before updating. new.total is the total value after updation.
Since you haven't conveyed what to do after update I have added the logic of subtracting the total from old total and added the new total value. Change it as per your requirement.
You can also create these triggers using GUI in phpmyadmin. You have to select the triggers in the menu bar and add the trigger definition.

Mysql Trigger - Update table when insert another table

I have three tables:
Import Table:
import_id | import_date
-------------------------
1 | 2019/07/29
2 | 2019/07/28
ImportItem Table:
import_id | product_id | quantity
-------------------------------------
1 | 1 | 50
1 | 2 | 60
1 | 3 | 20
Product Table:
product_id | quantity
---------------------------
1 | 10
2 | 5
3 | 15
I want to create a trigger update quantity in product table when insert ImportItem table.
Any help much appreciated! Thanks very much!
If you create a trigger in your table import_item that will update your table product after each import_item INSERT you can do this :
CREATE TRIGGER trigger_name
AFTER INSERT
ON import_item FOR EACH ROW
BEGIN
UPDATE product SET quantity = NEW.quantity WHERE product_id = NEW.product_id;
END
Here is some documentation about the trigger key words NEW and OLD : documentation
When you are in a BEFORE/AFTER INSERT trigger, you can use NEW.field to get the value of the new field you just inserted.
Now if you are in a BEFORE/AFTER UPDATE trigger, you can use NEW.field to get the value of the field AFTER the update and OLD.value to get the value of the field BEFORE the update.
And the last, if you are in a BEFORE/AFTER DELETE trigger , you can use OLD.field to get the value fo the field before the delete.
Is it what you are looking for ?

MySQL trigger to update a column by reordering its values

Sorry if the title is miss-leading, I couldn't come up with a better one that is related to my issue.
I've been trying to solve this for a while now, and I couldn't find the solution.
I have a table categories:
+----+--------+----------+
| ID | Name | Position |
+----+--------+----------+
| 1 | Dogs | 4 |
| 2 | Cats | 3 |
| 3 | Birds | 10 |
| 4 | Others | 2 |
+----+--------+----------+
I need to keep the Position column in order, in a way not to miss an values as well, so the final table should look like:
+----+--------+----------+
| ID | Name | Position |
+----+--------+----------+
| 1 | Dogs | 3 |
| 2 | Cats | 2 |
| 3 | Birds | 4 |
| 4 | Others | 1 |
+----+--------+----------+
What I tried doing, is creating a trigger on UPDATE and on INSERT that would try to prevent this. The trigger I created ( same one before INSERT) :
DELIMITER //
CREATE TRIGGER sortPostions BEFORE UPDATE ON categories
FOR EACH ROW BEGIN
SET #max_pos = 0;
SET #min_pos = 0;
SET #max_ID = 0;
SET #min_ID = 0;
SELECT position, id INTO #max_pos,#max_ID FROM categories WHERE position = ( SELECT MAX(position) FROM categories);
SELECT position, id INTO #min_pos,#min_ID FROM categories WHERE position = ( SELECT MIN(position) FROM categories);
IF NEW.position >= #max_pos AND NEW.id != #max_ID THEN
SET NEW.position = #max_pos + 1;
END IF;
IF NEW.position <= #min_pos AND NEW.id != #min_ID THEN
SET NEW.position = #min_pos - 1;
END IF;
IF NEW.position < 0 THEN
SET NEW.position = 0;
END IF;
END//
DELIMITER ;
But unfortunately it's not working as intended. It's not fixing missing values and I think this is not a perfect solution.
I went ahead and created a procedure:
BEGIN
SET #n = 0;
UPDATE categories
SET position = #n:=#n+1
ORDER BY position ASC;
END
But I wasn't able to call this procedure from a trigger, as it seems that MySQL doesn't allow that. I get the following error:
#1442 - Can't update table 'categories' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
mysql -V output:
mysql Ver 14.14 Distrib 5.5.57, for debian-linux-gnu (x86_64) using readline 6.3
What's the perfect solution to solve this problem ?
Thanks a lot!
You can't do this in a trigger. MySQL does not allow you to do an INSERT/UPDATE/DELETE in a trigger (or in a procedure called by the trigger) against the same table for which the trigger was spawned.
The reason is that it can result in infinite loops (your update spawns the trigger, which updates the table, which spawns the trigger again, which updates the table again...). Also because it can create lock conflicts if more than one of these requests happens concurrently.
You should do this in application code if you must renumber the position of the rows. Do it with a separate statement after your initial query has completed.
Another option is don't worry about making the positions consecutive. Just make sure they are in the right order. Then when you query the table, generate row numbers on demand.
SELECT (#n:=#n+1) AS row_num, c.*
FROM (SELECT #n:=0 AS n) AS _init
CROSS JOIN categories AS c
ORDER BY c.position ASC;
+---------+----+--------+----------+
| row_num | id | name | position |
+---------+----+--------+----------+
| 1 | 4 | Others | 2 |
| 2 | 2 | Cats | 3 |
| 3 | 1 | Dogs | 4 |
| 4 | 3 | Birds | 10 |
+---------+----+--------+----------+
In MySQL 8.0, you'll be able to do this with more standard syntax using ROW_NUMBER().
SELECT ROW_NUMBER() OVER w AS row_num, c.*
FROM categories AS c
WINDOW w AS (ORDER BY c.position ASC);
Gives the same output as the query using the user-variable.

mysql - The target table of the UPDATE is not updatable using trigger

update 2nd and 3rd row table after update 1st the row on same table
store procedure or trigger after update in table (using mysql)
id |Is_executed | Is_Ready
1 | 0 |
2 | | 0
3 | | 0
if i update is_executed to '1' of id is 1 then i want following row update
id |Is_executed | Is_Ready
1 | 1 |
2 | | 1
3 | | 1
using trigger or store procedure
Your desired business logic isn't entirely clear from your question, but it sounds like you want a trigger along the following lines:
DELIMITER ;;
CREATE TRIGGER my_trigger AFTER UPDATE ON my_table FOR EACH ROW
IF NEW.id = 1 AND OLD.Is_executed = 0 AND NEW.Is_executed = 1 THEN
UPDATE my_table SET Is_Ready = 1 WHERE id IN (2,3);
END IF
;;
DELIMITER ;

updating a field on update of other field in same row

I have a Table like this:
user_level
uid | level_order | current_level
---------------------------------
1 | 1,2,3 | 1
2 | 4,5,6 | 4
3 | 7,8,9 | 7
now, if I update level_order field for a particular user, i want to update current_level using trigger or procedure.
For Example if I run this query :
update user_level set level_order = '21,22,23' where uid=1;
then table should update like this:
uid | level_order | current_level
---------------------------------
1 | 21,22,23 | 21
2 | 4,5,6 | 4
3 | 7,8,9 | 7
Is it possible using trigger or procedure.
I am using MYSQL.
I think you dont need trigger.You can update it directly like this.
update user_level
set level_order = '21,22,23',
current_level=SUBSTRING_INDEX('21,22,23', ',', 1)
where uid=1;
Sql Fiddle Demo
Try following code
create trigger AutoUpdateCurrentLevelFromUserLevel
after update
on UserLevel
for each row
set CurrentLevel="{your value here}"
You can use trigger to achieve the same. Trigger are very useful in scenario like the one you mentioned above for implementing constraints. Use a update trigger for the same. Also you can embed the update to the new row in your query itself.