什么时候数据库触发器不好?

2022-01-01 00:00:00 sql database sql-server triggers

可能的重复:
数据库触发器是邪恶的吗?

有很多关于数据库触发器的负面信息,只是想了解社区对什么时候好还是坏的看法.

There is lot of negative information on database triggers, just want to get the community's take on when is it good vs bad.

推荐答案

我认为当它们用于填充单独的、一次性的表集以用于日志记录、聚合等以确保安全性或创建例如元数据.

I think they're OK when they are used to populate a separate, one-off set of tables for things like logging, aggregation etc. for security or creating metadata for example.

当您开始更改实时"数据或返回"到您的商业信息表时,它们就会变得邪恶和笨拙.为此,它们也完全没有必要.没有什么是触发器做不到的,而存储过程是做不到的.

When you start altering your "live" data or "looping back" into your biz info tables, that's when they become evil and unwieldy. They are also utterly unnecessary for this. There is nothing that a trigger does that a stored proc cannot do.

我觉得它们是 SQL 的邪恶,相当于编程语言中的 GOTO.合法,但除非绝对必要,否则应避免使用,而且它们从来都不是绝对必要的.

I feel like they are SQL's evil equivalent to GOTOs in programming languages. Legal, but to be avoided unless absolutely necessary, and they are NEVER absolutely necessary.

相关文章