Can a MySQL trigger associate with more than one table or all tables?

I created this trigger to insert the value of the calculated value into a field in the table if the user forgets to put the data themselves:

DELIMITER // CREATE TRIGGER OnNewTableRegistry BEFORE INSERT ON eduardo8_plataforma.tabela FOR EACH ROW BEGIN IF NEW.ut = null THEN SET NEW.ut = GetUT('tabela'); ELSEIF NEW.ut = '' THEN SET NEW.ut = GetUT('tabela'); END IF; END; // DELIMITER ; 

But I need to do the same with every table in this database. Is it possible to use the same trigger for all tables, and if YES, how do we get the name of the table that caused its use on lines 6 and 8, where tabela is tabela ?

I need something like this:

 DELIMITER // CREATE TRIGGER OnNewTableRegistry BEFORE INSERT ON (* as _TableName) FOR EACH ROW BEGIN IF NEW._TableName.ut = null THEN SET NEW._TableName.ut = GetUT(_TableName); ELSEIF NEW._TableName.ut = '' THEN SET NEW._TableName.ut = GetUT(_TableName); END IF; END; // DELIMITER ; 
+4
source share
1 answer

No. The syntax does not provide for it.

It makes no sense to allow this, because the NEW keyword should refer to a specific string definition. If you have two tables with the same row definition, they should be made into one table, and the other column indicates the difference.

+2
source

Source: https://habr.com/ru/post/1489375/


All Articles