Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a MySQL trigger be associated to more than one table or by all tables?

Tags:

mysql

triggers

I have created this trigger to insert a value calculated value to a field in the table in case the user forget to put the data himself:

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 that database. Is it possible to use the same trigger to all the tables and if YES, how do we get the name of the table that triggered to use it in the line 6 and 8 where there is tabela specified?

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 ;
like image 892
NaN Avatar asked Oct 22 '22 06:10

NaN


1 Answers

No. The syntax doesn't provide for it.

It makes no sense to allow it, because the NEW keyword must refer to a particular row definition. If you have two tables with the same row definition, they should be made into the one table, with another column denoting the difference.

like image 116
Bohemian Avatar answered Oct 30 '22 00:10

Bohemian