NAME
SET CONSTRAINTS - 设置当前事务的约束模式
SYNOPSIS
SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }
DESCRIPTION 描述
SET CONSTRAINTS 设置当前事务里的约束运算的特性。 在 IMMEDIATE 模式下,约束是在每条语句后面进行检查的。 在 DEFERRED 模式下,一直到事务提交时才检查约束。
在你把约束的模式修改成 IMMEDIATE 之后,新的约束模式是反作用式地生效的: 任何尚在等待的,需要在事务结束检查地数据修改的约束(在使用 DEFERRED 的时候)都将在执行 SET CONSTRAINTS 命令的时候马上检查。
从创建的时候开始,一个约束总是表现为下面三个特性之一: INITIALLY DEFERRED, INITIALLY IMMEDIATE DEFERRABLE, 或 INITIALLY IMMEDIATE NOT DEFERRABLE。 第三个特性不会受 SET CONSTRAINTS影响。
目前,只有外键约束被这个设置影响。检查和***约束总是立即检查的而不是可推迟的。
NOTES 注意
这个命令只在当前事务里修改约束的行为。因此,如果你在事务块外面 (BEGIN/COMMIT 对)执行这个命令, 它将显得没有任何作用那样。如果你希望不在每个事务中发出 SET CONSTRAINTS 也能修改约束的行为, 那么在创建这些约束的时候声明 INITIALLY DEFERRED 或者 INITIALLY IMMEDIATE。
COMPATIBILITY 兼容性
这条命令与 SQL 标准里定义的行为兼容,只不过,在 PostgreSQL 里,它只适用于外键约束。
#p#
NAME
SET CONSTRAINTS - set the constraint mode of the current transaction
SYNOPSIS
SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }
DESCRIPTION
SET CONSTRAINTS sets the behavior of constraint evaluation in the current transaction. In IMMEDIATE mode, constraints are checked at the end of each statement. In DEFERRED mode, constraints are not checked until transaction commit.
When you change the mode of a constraint to be IMMEDIATE, the new constraint mode takes effect retroactively: any outstanding data modifications that would have been checked at the end of the transaction (when using DEFERRED) are instead checked during the execution of the SET CONSTRAINTS command.
Upon creation, a constraint is always give one of three characteristics: INITIALLY DEFERRED, INITIALLY IMMEDIATE DEFERRABLE, or INITIALLY IMMEDIATE NOT DEFERRABLE. The third class is not affected by the SET CONSTRAINTS command.
Currently, only foreign key constraints are affected by this setting. Check and unique constraints are always effectively initially immediate not deferrable.
NOTES
This command only alters the behavior of constraints within the current transaction. Thus, if you execute this command outside of a transaction block (BEGIN/COMMIT pair), it will not appear to have any effect. If you wish to change the behavior of a constraint without needing to issue a SET CONSTRAINTS command in every transaction, specify INITIALLY DEFERRED or INITIALLY IMMEDIATE when you create the constraint.
COMPATIBILITY
This command complies with the behavior defined in the SQL standard, except for the limitation that, in PostgreSQL, it only applies to foreign-key constraints.