How do I add multiple updates to a trigger?

I hope you can help me here. I am using MySQL + PhpMyAdmin and I have 2 tables in this problem.

Table 1: Accounts - id, account_name, website, etc. etc.

Table 2: Domains - id, domain_name, account_name

and I inserted these queries into 2 triggers.

Update
domains before upgrading , accounts
set domains.account_name = NULL
where accounts.website! = Domains.domain_name

Update
domains after upgrade , accounts
set domains.account_name = accounts.account_name
where domains.domain_name = main_accounts.website

That being said, when I update something in the accounts table, it will automatically drop the account_name table from the domains table and add a new account name if a specific account is updated.

The images below show an example.
The tables have not been updated yet:
----------
Accounts table
enter image description here

Domains Table
enter image description here ----------
----------
Updated View.
----------
----------
Account table
enter image description here

Domain table
enter image description here

So, in the second image of the table of accounts, I changed the domain and automatically, the table of domains was updated. I would like to make only 1 trigger that will contain these 2 update requests. I don't know if this is possible, because after I complete this, I may also need to know how to update multiple different tables from 1 trigger. The reason I am asking is because I need to assign an account name to all the sub-tables used. For example, the main table is the accounts table and the subcategories to update are:
domains.account_name
ip_address.account_name
phones.account_name
payments.account_name

So, I don't know if it is possible to update these sub-tables in the "account_name" column when the master "account" is updated.

Thank. I hope my question is clear.: D: D

+3


source to share


1 answer


You can group multiple statements into a trigger with BEGIN and END.

Example:



DELIMITER $$

CREATE TRIGGER my_trigger BEFORE INSERT ON accounts
FOR EACH ROW
BEGIN
    -- Statement one
    UPDATE domains
    SET domains.account_name = accounts.account_name
    WHERE domains.domain_name = main_accounts.website;
    -- Statement two
    UPDATE another_table
    SET another_table.column_name = accounts.account_name
    WHERE another_table.domain_name = accounts.some_column;
    -- More UPDATE statements
END$$

      

+3


source







All Articles