I need to create an after insert trigger that increments a field in a different table where the id = the user_id field from the table with the trigger. I tried:
CREATE TRIGGER `update-notify`
AFTER INSERT
ON `notifications`
FOR EACH ROW UPDATE users
SET notify = notify + 1
WHERE id = notifications.user_id
But got Unknown column 'notifications.user_id' in 'where clause'
How can I match the fields across tables in this trigger?
Use NEW for the table record that triggered the trigger
CREATE TRIGGER `update-notify` AFTER INSERT ON `notifications`
FOR EACH ROW
UPDATE users
SET notify = notify + 1
WHERE id = NEW.user_id
Related
I have two tables named del_stu and del_emp.
I want to insert a row into the del_emp table when updating the del_stu table. and I wan to insert the row which updated by the del_emp. I have been created a trigger for that as follows.
create trigger before_update_stu
before update
on del_stu
for each row
begin
insert into del_emp
set
del_emp.emp_id = del_stu.stu_id,
del_epm.emp_name = del_stu.stu_name,
del_emp.salary = stu_emp.salary;
end
But this shows the following error.
ERROR 1054 (42S22): Unknown column 'del_epm.emp_name' in 'field list'
So how to do this task? That means how to change my trigger in order to do this task?
If you not clear about the task,
The task
I want to insert a row(updated by the del_stu table) into the del_emp table
It seems to be a spelling issue for that particular column.
You should have
del_emp.emp_name.
Otherwise syntax looks fine for MySQL.
Need to use "OLD" keyword for that. The following code worked.
create trigger before_update_stu
before update
on del_stu
for each row
begin
insert into del_emp
set
emp_id = OLD.stu_id,
emp_name = OLD.stu_name,
salary = OLD.salary;
end
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';
I have the date type column valid_upto in the users table which is a foreign key referring to the valid_upto column in the subscription table. I need users.valid_upto to update when a change is made to subscription.valid_upto where the corresponding user ID values match. Currently the trigger updates all rows in the users table when a single row is changed in the subscription table. It basically ignores the WHERE clause.
DROP TRIGGER IF EXISTS `valid_date` ;
CREATE DEFINER = `root`#`localhost` TRIGGER `valid_date` AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
UPDATE valid_upto SET users.valid_upto = subscription.valid_upto WHERE users.id = subscription.user_id;
END ;
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 2 tables, Portfolio and Transactions. When you insert Transaction_Amount value into the Transactions, the Portfolio should be updated too. Below is the table structure.
I created a Trigger so whenever I insert a value into the Transactions, the Portfolio will be updated too.
Below is my MYSQL Trigger
USE `custom_sample`;
DELIMITER $$
CREATE TRIGGER `Transactions_AINS` AFTER INSERT ON `Transactions` FOR EACH ROW
UPDATE Portfolio
SET Invest_Amount = Invest_Amount+Transactions.Transaction_Amount
where
Portfolio.idPortfolio = Transactions.idPortfolio
However this didn't work. It says Unknown column Transactions.idPortfolio in where clause
What is wrong with my script?
The record of the triggered table can be referenced by NEW (or OLD for the values before in case of an update) instead of the table name.
CREATE TRIGGER `Transactions_AINS` AFTER INSERT ON `Transactions`
FOR EACH ROW
UPDATE Portfolio
SET Invest_Amount = Invest_Amount + NEW.Transaction_Amount
WHERE idPortfolio = NEW.idPortfolio