且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

触发器与检查约束

更新时间:2023-11-27 23:34:46

经验法则是在诉诸触发条件之前使用 CHECK 约束。

The rule of thumb is to use CHECK constraint before you resort to triggers.

A CHECK 约束更快,更简单,更便于携带,需要更少的代码,更容易出错。触发器可以很容易被其他触发器所规避。

A CHECK constraint is faster, simpler, more portable, needs less code and is less error prone. Triggers can easily be circumvented by other triggers, for instance.

触发器复杂得多

如果 CHECK 约束对您的情况太限制或导致重新加载转储的麻烦,您可以使用 NOT VALID 修饰符作为中间(pg 9.2+)。详细信息:

If a CHECK constraint is too restrictive for your case or causes trouble reloading a dump, you could use the NOT VALID modifier as middle ground (pg 9.2+). Details:

  • Disable all constraints and table checks while restoring a dump