数据库触发器是邪恶的吗? [英] Are database triggers evil?

查看:20
本文介绍了数据库触发器是邪恶的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库触发器是个坏主意吗?

Are database triggers a bad idea?

根据我的经验,它们是邪恶的,因为它们会导致令人惊讶的副作用,并且难以调试(尤其是当一个触发器触发另一个时).通常,开发人员甚至不会考虑查看是否存在触发器.

In my experience they are evil, because they can result in surprising side effects, and are difficult to debug (especially when one trigger fires another). Often developers do not even think of looking if there is a trigger.

另一方面,如果在数据库中创建新的 FOO 时必须发生逻辑,那么放置它的最简单的地方是 FOO 表上的插入触发器.

On the other hand, it seems like if you have logic that must occur evertime a new FOO is created in the database then the most foolproof place to put it is an insert trigger on the FOO table.

我们唯一一次使用触发器是为了非常简单的事情,比如设置 ModifiedDate.

The only time we're using triggers is for really simple things like setting the ModifiedDate.

推荐答案

触发器的主要问题是

  • 它们是完全全球性的——无论表活动的上下文如何,它们都适用;
  • 他们很隐蔽;很容易忘记他们的存在,直到他们以意想不到的(而且非常神秘的)后果伤害了你.

这只是意味着它们需要在适当的情况下谨慎使用;根据我的经验,这仅限于关系完整性问题(有时比您可以声明的粒度更细);通常不用于商业或交易目的.天啊.

This just means they need to be carefully used for the proper circumstances; which in my experience is limited to relational integrity issues (sometimes with finer granularity than you can get declaratively); and usually not for business or transactional purposes. YMMV.

这篇关于数据库触发器是邪恶的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆