I am learning SQL trigger and have read some tutorial on how to delete a row on table B when a row in table a is deleted.
I have the following trigger :
CREATE DEFINER=`root`#`localhost` TRIGGER `testDB`.`test_BEFORE_DELETE`
BEFORE DELETE ON `testDBtable` FOR EACH ROW
BEGIN
Delete from testDBtable2 where id = (select id from deleted);
END
Both testDBtable and testDBtable2 have 2 column, id and name.
When I deleted a row from testDB, I received the following error:
Error Code: 1146. Table 'testDB.deleted' doesn't exist 0.000 sec
I am using MYSQLWorkBench.
I think OLD.id is the one you need instead of select id from deleted.
Full query:
CREATE DEFINER=`root`#`localhost` TRIGGER `testDB`.`test_BEFORE_DELETE`
BEFORE DELETE ON `testDBtable` FOR EACH ROW
BEGIN
Delete from testDBtable2 where id = OLD.id;
END
Related
I have an after insert trigger that is supposed to update the field total in my table "test" where the id_cart is equal to new.id_cart. However my trigger is updating every single row in the table not only the one desired. I would like to know how can I modify my trigger so it only updates the row that I want.
This is my trigger.
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW BEGIN
UPDATE test set total= (select sum(price_product) from test_product_quantity_cart where id_cart=new.id_cart);
END
So if the new row inserted in table "test_product_quantity_cart" has an new.id_cart=1, then only the row in table "test" with id_cart=1 should be uptated.
I think I am missing a "where" clause to indicate the update statement which rows it is suppossed to upate. However I do not know how to add that clause.
Thank you!
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW
UPDATE test
JOIN ( SELECT id_cart, SUM(price_product) total
FROM test_product_quantity_cart
WHERE id_cart=NEW.id_cart ) value_for_update USING (id_cart)
SET test.total = value_for_update.total;
I want to update one table in MySQL based upon another table update and insertions. Scenario is like this, when someone change the status of the column in main table the child table's column field should be update for their matching ID.
Tables look like as below:
po_request
Id po_id status
1 E0001 Requested
2 E0002 Received
PO_LINE
Id po_id is_received
6 E0001 0
7 E0002 0
Need to update the Po_line table each time when status has changed to "Received" OR directly insert the "received" in the table. I have made trigger but it's not working. Trigger
DROP TRIGGER IF EXISTS `t1`;
DELIMITER $$
CREATE TRIGGER `t1`
AFTER UPDATE ON po_request FOR EACH ROW
BEGIN
IF NEW.`status` = 'Received'
THEN
UPDATE po_line JOIN po_request ON po_request.po_id = po_line.po_id SET is_received = '1' WHERE po_request.status = 'Received'; END IF;
END$$
DELIMITER ;
Trigger loaded into table successfully but when I update the table it throws very weird error:
Error Code: 1054. Unknown column 'date_received' in 'field list'.
I think you need to update it by avoid joining the table and match in where clause with the reference of NEW keyword. which refers to the other table id like :
UPDATE po_line
SET po_line.is_received = '1'
WHERE new.po_id = po_id
AND po_request.status = 'Received';
Can we perform count operation inside a trigger?
I have a table TB1 with fields id,title,author,date. I want to create a trigger that when ever I will insert a row in tb1. A trigger should run and the total no of authors in the tb1 should show in a new table total.
I have written the code
DELIMITER $$
CREATE TRIGGER insertintonew2
AFTER
INSERT ON tb1
FOR EACH ROW
BEGIN
UPDATE total
SET total_author = (select count(*) from tb1);
END;
$$
The output I am getting a is populated with total_author column name but the count value is not populated.
Can anyone tell me why is it happening?
I have two tables
Table one: default_sellers_commissions have:
id
value - INT
Table two: marketplace_saleperpartner have:
id
userID
commission - INT default '0.00'
commission_id
What I need is after insert a new row in table marketplace_saleperpartner update the marketplace_saleperpartner.commission value with value from default_sellers_commissions.value where the marketplace_saleperpartner.commission_id = default_sellers_commissions.id
So I create a trigger :
CREATE TRIGGER `marketplacedefaultcommission`
AFTER INSERT ON `marketplace_saleperpartner`
FOR EACH ROW
UPDATE marketplace_saleperpartner
SET `marketplace_saleperpartner`.`commision` = `default_sellers_commissions`.`value`
WHERE `marketplace_saleperpartner`.`comm_id` = `default_sellers_commissions`.`id`
AND `marketplace_saleperpartner`.`commision`='0.00';
P.S:
For some reason I need the marketplace_saleperpartner.commision to insert for the first time with value='0.00'
Now I am getting this error while trying to insert a new row into table marketplace_saleperpartner
MySql Error: Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger
Any help please ?
You could use a BEFORE INSERT trigger that sets marketplace_saleperpartner.commision to value from marketplace_saleperpartner.
CREATE TRIGGER marketplacedefaultcommission
BEFORE INSERT ON marketplace_saleperpartner
FOR EACH ROW
BEGIN
SET new.commission = (
SELECT value
FROM default_sellers_commissions
WHERE id = new.commission_id
);
END;
SQL Fiddle example
I have created a table test1 as
CREATE TABLE test1
(id INT,
name VARCHAR(20))
ENGINE=INNODB;
Now I want to delete a record from this table before inserting a new record having the same id(that is,record already existing with same id,then insert the new record) .I tried the code give below.
create trigger before_insert_test1 before insert
on test1
for each row
begin
delete from test1 where id=NEW.id;
end;
but it shows error
ERROR 1442 (HY000): Can't update table 'test1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Can anyone help.
thanks in advance
I don't think you can update/delete from the same table that you're inserting into. The table is being locked by the insert statement, even though you specified after insert. The insert transaction is not yet completed.
My suggestion would be to do a test and delete the record before you do the insert, or use the MySQL ON DUPLICATE KEY UPDATE statement if it applies
create trigger before_insert_test1 before insert on test1
DELETE FROM test1 WHERE id=NEW.id;
end;
You shan't need for loop, delete statement will delete every record with given id.