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 ;
Related
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
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 created a trigger to delete a row from orders when the user_id and restaurant_id are set to null.
delimiter //
create trigger order_delete
before update on orders
for each row
if new.user_id is null and new.restaurant_id is null then
delete from orders where order_id = new.order_id;
end if;
//
delimiter ;
user_id and restaurant_id are foreign keys from two other tables and will be set to null when they are deleted from their respective tables. However, when I tried deleting them from their tables it does not execute the trigger. So I still have an order with two user_id = NULL and restaurant_id = NULLremaining in my orders table. Does anyone know why my trigger is not triggering?
As MySQL documentation on foreign keys says:
Cascaded foreign key actions do not activate triggers
Therefore, no mtter what trigger you create, it is not going to be called when a value is set to null via cascade.
If you only want to delete a record if both fields are set to null, then you have to perform this action from the application code, you cannot rely on database automation.
Just a side note: you should not delete orders from your system, since they are the basis of your financial transactions and you can get into trouble with the tax authorities and the external financial auditors for doing that.
You want that trigger to be fired when user_id and restaurant_id are set to NULL?
In that case, you should use:
delimiter //
create trigger order_delete
after update on orders
for each row
if new.user_id is null and new.restaurant_id is null then
delete from orders where order_id = new.order_id;
end if;
//
delimiter ;
Using the keyword after instead of before means the trigger will be fired after the fields are set to null. Using before, the trigger will be fired when user_id and restaurant_id still have their previous values. In that case, the condition in the if statement is never true.
I'd like to create a before delete trigger that deletes rows from two different tables. But I can't figure out which parameters to use.
I got a house table, and when I delete a row, I'd like to delete every row in my two other tables: user_house and firm_house, which contains same house id as the one triggering the event.
What does FOR EACH ROW mean? And how can I properly set my trigger up?
USE `mydb`;
DELIMITER $$
CREATE TRIGGER `deleteUnions` BEFORE DELETE ON `house`
FOR EACH ROW
BEGIN
DELETE FROM user_house WHERE ?? = ??;
DELETE FROM firm_house WHERE ?? = ??;
END
Some details about the structure:
user_house is joined by user_id and house_id;
firm_houise is joined by firm_id and house_id.
Refer to the record that gets deleted in the trigger with OLD. Then use the id to delete from the other tables.
DELETE FROM user_house WHERE house_id = OLD.house_id;
DELETE FROM firm_house WHERE house_id = OLD.house_id;
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