I have 3 tables named opproduct, opcategory and opproduct_to_category.The opproduct table has an after insert and an after delete trigger. These triggers modify the category table content column, but it gets the category_id trough opproduct_to_category table. The statments of triggers roughly the same. The after update is working well, but after insert doesn't modify the category table.
Thanks for your answers!
DROP TRIGGER IF EXISTS opproduct_AINS;
DELIMITER $$
CREATE TRIGGER `opproduct_AINS` AFTER INSERT ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF new.`status`='1' THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory
SET content = content+1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS opproduct_AUPD;
DELIMITER $$
CREATE TRIGGER `opproduct_AUPD` AFTER UPDATE ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF NEW.`status`<> OLD.`status` AND NEW.`status`=1 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content+1
WHERE category_id=_category_id;
ELSEIF NEW.`status`<> OLD.`status` AND NEW.`status`=0 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content-1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;
Related
i am setting triggerafter_inser and i dont know how to resolve it
DELIMITER $$
USE `solocloud_dev_2`$$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW BEGIN
DECLARE var INT;
SET var=0;
SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id;
IF var > 0
THEN
UPDATE table_10_summary SET records = records+1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary (new.client_id,1);
END$$
DELIMITER$$
There are several problems with your trigger :
you need to SET var from the results of the query ; as it is now, you are not setting this value
the IF block must be terminated with END IF
The following codes compiles successfully in this DB Fiddle :
DELIMITER $$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW
BEGIN
DECLARE var INT;
SET var = (SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id);
IF (var > 0) THEN
UPDATE table_10_summary SET records = records + 1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary VALUES(new.client_id,1);
END IF;
END$$
DELIMITER ;
I've created this trigger, but I have two problems. One it does not actively populate the Person table and two. If I enter more than one row into my Party table I get an error in access, Result of Subquery returns more than one row.
Anybody know my problem(s)?
DELIMITER $$
Create TRIGGER Trigger1
AFTER
INSERT
ON Party
FOR EACH ROW
BEGIN
declare partytypeid int;
declare partyid int;
set #partyid:= (select partyid from party);
set #partytypeid:= (select partytypeid from party);
IF partytypeid = 1
THEN INSERT INTO Person
(PartyId) VALUES (PartyId);
END IF;
END$$
delimiter ;
When you set your variables, you're doing a select against the whole table instead of the inserted record.
DELIMITER $$
Create TRIGGER Trigger1
AFTER INSERT ON Party FOR EACH ROW
BEGIN
declare partytypeid int;
declare partyid int;
set #partyid:= new.partyid;
set #partytypeid:= new.partytypeid;
IF partytypeid = 1 THEN
INSERT INTO Person
(PartyId) VALUES (PartyId);
END IF;
END$$
delimiter ;
I have a problem with my trigger before insert.
I try to control the row number of my table 'user_search' for one 'id_user' and if this row number > 4, the oldest row should be deleted.
My actual trigger is this :
DELIMITER $$
CREATE TRIGGER before_insert_user_search
BEFORE INSERT
ON user_search FOR EACH ROW
BEGIN
DECLARE sDate DATETIME;
SELECT MIN(date_search) FROM user_search WHERE id_user = NEW.id_user INTO sDate;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = NEW.id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE date_search = sDate;
END;
END IF;
END$$
DELIMITER ;
However, it does not work. Do someone have a solution to help me please.
Thanks.
You can not delete same table row during using INSERT trigger because it holds the lock of table. So try to write a Stored Procedure to insert new record.
Stored Procedure should be something like bellow:
CREATE PROCEDURE insert_user_search(new_id_user INT, ......other variable to inert)
BEGIN
DECLARE #minId INT;
//INSERT using parameter
SET #minId = SELECT MIN(id) FROM user_search WHERE id_user = new_id_user;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = new_id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE id_user = #minId;
END;
END IF;
END;
I have a MySQL table , look like this
t id lang title
1 7 en_UK my_title
1 7 kh_KH
I want write a trigger that update title to my_title with the same id is 7
Result
t id lang title
1 7 en_UK my_title
1 7 kh_KH my_title
From my understanding.
DELIMITER $$
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
IF (NEW.title IS NULL OR NEW.title= '') THEN
SET NEW.title= ??? ;
END IF;
END$$
DELIMITER ;
[UPDATE1]->not works (trigger not being created)
DELIMITER $$
DROP TRIGGER IF EXISTS `update_category_after_insert`
CREATE TRIGGER `update_category` AFTER INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
[UPDATE2]
DELIMITER $$
CREATE TRIGGER ``update_category_after_insert`` BEFORE INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
Finally , I found the good solution for my case
UPDATE categories c INNER JOIN categories c2 ON (
c.num_noeud = c2.num_noeud
) SET c.libelle_categorie = c2.`libelle_categorie`
Can you clarify?
Are you trying to pull the value from the title column in the 'en_UK' row that exists when you insert an new row with the same id that HAS the title column not entered?
okay
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
DECLARE loc_title VARCHAR(20);
IF (NEW.title IS NULL OR NEW.title= '') THEN
select title into loc_title from term where NEW.id = id and lang = 'en_UK';
SET NEW.title= loc_title;
END IF;
END
This should do the trick.
This was my trigger def:
CREATE DEFINER = CURRENT_USER TRIGGER `therinks`.`glreturndata_BEFORE_UPDATE` BEFORE UPDATE ON `glreturndata` FOR EACH ROW
BEGIN
DECLARE myVal VARCHAR(20);
if NEW.DESCRIPTION IS NULL or new.description = '' THEN
SELECT min(description) into myVal from glreturndata where category = NEW.category and new.idglreturndata <> idglreturndata;
Set NEW.DESCRIPTION = myval;
end if;
END
It seems that you can't do all this in a trigger. According to the documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
According to this answer, it seems that you should:
create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction.
With a stored proc you'll manually commit the changes (insert and update). I haven't done this in MySQL, but this post looks like a good example.
I have a problem with a MySQL Trigger.
I have 3 tables Customers, Products and Sales.
In Sales I reference customer and product and I want to update the some counts on Products and Customers after a new sale is inserted.
The following trigger fails to update both tables... I cannot figure out what I am doing wrong.
DELIMITER $
CREATE TRIGGER OnSalesInsert AFTER INSERT ON Sales
FOR EACH ROW BEGIN
UPDATE Products SET Products.sold=Products.sold+NEW.amount WHERE Products.id=NEW.product_id;
UPDATE Customers SET Customers.amount=Customers.amount+NEW.amount WHERE Customers.id=NEW.customer_id;
END $
DELIMITER ;
Try this:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `OnSalesInsert` BEFORE INSERT ON `Sales`
FOR EACH ROW BEGIN
UPDATE Products
SET sold = sold + new.amount
WHERE id = new.product_id;
UPDATE Customers
SET amount = amount + new.amount
WHERE id = new.customer_id;
END;
$$
DELIMITER ;
DELIMITER $$
create trigger UpdateAvail after insert on product_details
for each row
Begin
Declare a1 INT;
Declare d1 VARCHAR(1);
Declare d2 VARCHAR(100);
Select count(0) INTO a1 from prod_available where P_Id=new.P_Id;
Select P_Name,P_Brand INTO d1,d2 from product where P_Id=new.P_Id;
IF a1>0 THEN
Update prod_available set P_quantity=P_quantity+new.quantity where P_Id=new.P_Id;
ELSE
insert into prod_available (P_Id,P_Name,P_Brand,P_quantity) values (new.P_Id,d1,d2,new.quantity);
END IF;
END;
$$
DELIMITER ;