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

半世苍凉 提交于 2019-12-31 00:46:31

问题


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 ;

回答1:


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.



来源:https://stackoverflow.com/questions/17437121/can-a-mysql-trigger-be-associated-to-more-than-one-table-or-by-all-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!