How to create a trigger to do a math for two columns. Below is an example.
CREATE TRIGGER `POBRANY` BEFORE INSERT ON `TAB1`
FOR EACH ROW SET
NEW.POBR_SUROW = (NEW.Column_1_ILO + NEW.Column_2_ILO + NEW.Column_3_ILO + NEW.Column_4_ILO + NEW.Column_5_ILO + NEW.Column_6_ILO)
AND
NEW.SUMA_GODZIN = (NEW.LICZBA_GODZIN_PRACY * NEW.ILO_OS_B_NA_PRODUKCJI)
Related
I'm trying to create a trigger in phpmyadmin and I have 2 tables, Items and Inventory. This is for a Library Database. When i insert a new item, if the items isbn already exists in the inventory, i want to add to the totalCopies of that item in the inventory. If not, i want it to insert into inventory a new row with the new items isbn. This is my first time using triggers and I'm getting syntax errors. This is what i have right now.
BEGIN
IF ((SELECT COUNT(*) FROM inventory WHERE inventory.isbn = NEW.isbn) > 0) THEN
(
UPDATE inventory
SET inventory.totalCopies = inventory.totalCopies + 1
WHERE inventory.isbn = NEW.isbn
SET inventory.totalAvailable = inventory.totalAvailable + 1
WHERE inventory.isbn = NEW.isbn
)
ELSE
INSERT INTO inventory VALUES( , NEW.isbn, 1, 1, 0)
END
The inventory columns are : inventoryID (PrimaryKey)(autoincrement), isbn, totalCopies, totalAvailable, totalCheckedOut.
I assume for this trigger that inventory id is an auto_increment value,
or else the insert gives an error.
the UPDATE query can be a little simplifoed
DELIMITER $$
CREATE TRIGGER insorup
AFTER INSERT
ON ORDERS FOR EACH ROW
BEGIN
IF (EXISTS(SELECT 1 FROM inventory WHERE inventory.isbn = NEW.isbn) ) THEN
UPDATE inventory
SET inventory.totalCopies = inventory.totalCopies + 1 ,
inventory.totalAvailable = inventory.totalAvailable + 1
WHERE inventory.isbn = NEW.isbn;
ELSE
INSERT INTO inventory VALUES ( NEW.isbn, 1, 1, 0);
END IF;
END$$
DELIMITER ;
as you see in the comment, Shadow pointed out that it is also possible to reduce the inner Part. When you have the column ISBN declared as UNIQUE
DELIMITER $$
CREATE TRIGGER insorup
AFTER INSERT
ON ORDERS FOR EACH ROW
BEGIN
INSERT INTO
inventory VALUES ( NEW.isbn, 1, 1, 0)
ON DUPLICATE KEY UPDATE inventory.totalCopies = inventory.totalCopies + 1 ,
inventory.totalAvailable = inventory.totalAvailable + 1;
END$$
DELIMITER ;
How can I update table's column in a trigger after update on the same table?
Here's the trigger:
CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)
Now when I update the table like
UPDATE products_score SET votes_1 = 5 WHERE id = 0
this doesn't work, as I get the following:
#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So how on earth I can get this to work?
If you change your trigger to BEFORE instead of AFTER you could do it like this:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score
FOR EACH ROW
BEGIN
SET new.votes_total = new.votes_1 + new.votes_2 + new.votes_3 + new.votes_4 + new.votes_5
END
;
You can't have this the way you have setup because a trigger can't query other rows of the same table that it is defined on.
Istead you can use a Before Update Trigger toachieve what you want:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score FOR EACH ROW
BEGIN
SET NEW.votes_total = NEW.votes_1 + NEW.votes_2 + NEW.votes_3 + NEW.votes_4 + NEW.votes_5;
END;
Or use a stored procedure to update the table.
I am trying to create a SQL trigger which after updating the table will execute a piece of SQl to update it again
I have 5 fields that can be updated that each contain 0 or 1:
step1_complete, step2_complete, step3_complete, step4_complete and step5_complete
after any of these columns are updated I want to run a trigger that will update percent_complete within the same table with the following query:
SELECT sum( step1_complete +
step2_complete +
step3_complete +
step4_complete +
step5_complete ) * 20 AS Sum
FROM completed_part
GROUP BY id
this query will return either 20, 40, 60, 80 or 100 which is what i expect but when i try to run the update query to see if it works i get this error message
#1093 - You can't specify target table 'completed_part' for update in
FROM clause
I would also like to know if this is the correct syntax for creating a trigger
DELIMITER $$
CREATE
TRIGGER `completed_part_after_update` AFTER UPDATE
ON `completed_part`
FOR EACH ROW BEGIN
UPDATE completed_part
SET percent_complete = (
SELECT sum( step1_complete +
step2_complete +
step3_complete +
step4_complete +
step5_complete ) * 20 AS Sum
FROM completed_part
GROUP BY id
);
END$$
DELIMITER ;
try to use BEFOR UPDATE
BEGIN
SET NEW.percent_complete = ( NEW.step1_complete + NEW.step2_complete + NEW.step3_complete + NEW.step4_complete + NEW.step5_complete ) * 20;
END
I have the following schema : SQL Fiddle.
The trigger is updating the articles.votes field. I also need it to update the members.points field and the members lifetime_points fields with the same formula as updates the articles.votes field. How do I do that?
Are you looking for this?
DELIMITER $$
CREATE TRIGGER tg_ai_article_votes
AFTER INSERT ON article_votes
FOR EACH ROW
BEGIN
UPDATE articles
SET votes = COALESCE(votes, 0) + 1
WHERE id = NEW.article_id;
UPDATE members
SET points = COALESCE(points, 0) + 1,
lifetime_points = COALESCE(lifetime_points, 0) + 1
WHERE id = NEW.member_id;
END$$
CREATE TRIGGER tg_ad_article_votes
AFTER DELETE ON article_votes
FOR EACH ROW
BEGIN
UPDATE articles
SET votes = COALESCE(votes, 0) - 1
WHERE id = OLD.article_id;
UPDATE members
SET points = COALESCE(points, 0) - 1,
lifetime_points = COALESCE(lifetime_points, 0) - 1
WHERE id = OLD.member_id;
END$$
DELIMITER ;
Here is SQLFiddle demo
How can I update table's column in a trigger after update on the same table?
Here's the trigger:
CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)
Now when I update the table like
UPDATE products_score SET votes_1 = 5 WHERE id = 0
this doesn't work, as I get the following:
#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So how on earth I can get this to work?
If you change your trigger to BEFORE instead of AFTER you could do it like this:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score
FOR EACH ROW
BEGIN
SET new.votes_total = new.votes_1 + new.votes_2 + new.votes_3 + new.votes_4 + new.votes_5
END
;
You can't have this the way you have setup because a trigger can't query other rows of the same table that it is defined on.
Istead you can use a Before Update Trigger toachieve what you want:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score FOR EACH ROW
BEGIN
SET NEW.votes_total = NEW.votes_1 + NEW.votes_2 + NEW.votes_3 + NEW.votes_4 + NEW.votes_5;
END;
Or use a stored procedure to update the table.