mysql触发器关联两张表的字段_MySQL触发器可以关联到多个表还是所有表?

bd96500e110b49cbb3cd949968f18be7.png

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 ;

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值