{"Entry":{"collection":"sql","key":"set-constraints","name":"SET CONSTRAINTS","aliases":["set-constraints"],"metadata":{"aliases":["set-constraints"],"changed_in":["7.4"],"changes":[{"from":"7.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"7.1"},{"from":"7.1","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":null,"to":"7.2"},{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":["notes"],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }"],"removed":["SET CONSTRAINTS { ALL | constraint [, ...] } { DEFERRED | IMMEDIATE }"]},"to":"7.4"},{"from":"7.4","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description","notes","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"8.0"},{"from":"8.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"8.2"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"8.3"},{"from":"8.4","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description","notes","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"9.0"},{"from":"9.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.4"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"10"}],"content_hash":"c978ae0cc7544301981cae0ce4c9e0a5326c9fe2898c19998f8a180c9dade4d0","editorial":{},"first_version":"7.1","group":"transaction","imported_at":"2026-09-27T17:57:27.309883+08:00","last_version":"20","name":"SET CONSTRAINTS","object":"CONSTRAINTS","position":13007,"present_in":["7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"set constraint check timing for the current transaction","purpose_zh":"","related":[],"slug":"set-constraints","source_rev":"b7bd9cda","synopsis":"SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }","verb":"SET"}},"Definition":{"Collection":"sql","Key":"set-constraints","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"set-constraints","SourceRevision":"b7bd9cda","Facts":{"anchor":"SQL-SET-CONSTRAINTS","file":"sql-set-constraints.html","lang":"en","name":"SET CONSTRAINTS","purpose":"set constraint check timing for the current transaction","purpose_zh":"","related":[],"sections":[],"sections_same_as":"10","slug":"18","synopsis_html":"SET CONSTRAINTS { ALL | \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [, ...] } { DEFERRED | IMMEDIATE }","synopsis_text":"SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"set-constraints","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"SET CONSTRAINTS","Summary":"为当前事务设置约束检查时机","BodyHTML":"\u003cpre\u003eSET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e设置当前事务中约束检查的行为。\u003ccode\u003eIMMEDIATE\u003c/code\u003e约束会在每条语句结束时检查。\u003ccode\u003eDEFERRED\u003c/code\u003e约束要等到事务提交时才检查。每个约束都有各自的\u003ccode\u003eIMMEDIATE\u003c/code\u003e或\u003ccode\u003eDEFERRED\u003c/code\u003e模式。\u003c/p\u003e\u003cp\u003e创建约束时，它会被赋予以下三种特性之一：\u003ccode\u003eDEFERRABLE INITIALLY DEFERRED\u003c/code\u003e、\u003ccode\u003eDEFERRABLE INITIALLY IMMEDIATE\u003c/code\u003e或者 \u003ccode\u003eNOT DEFERRABLE\u003c/code\u003e。第三类始终是 \u003ccode\u003eIMMEDIATE\u003c/code\u003e，不会受到 \u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e命令的影响。前两类在每个事务开始时都处于其指定的模式，但其行为可以在事务中通过 \u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e修改。\u003c/p\u003e\u003cp\u003e带约束名称列表的\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e只会修改这些约束的模式（它们都必须是可延迟的）。每个约束名称都可以带模式限定。如果未指定模式名称，就会使用当前模式搜索路径查找第一个匹配的名称。\u003ccode\u003eSET CONSTRAINTS ALL\u003c/code\u003e会修改所有可延迟约束的模式。\u003c/p\u003e\u003cp\u003e当\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e将某个约束的模式从 \u003ccode\u003eDEFERRED\u003c/code\u003e改为\u003ccode\u003eIMMEDIATE\u003c/code\u003e时，新模式具有追溯效力：任何原本会在事务结束时才检查的未决数据修改，都会改为在执行\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e命令期间检查。如果违反了任何此类约束，\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e就会失败（并且不会改变该约束的模式）。因此，可以利用\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e强制在事务中的特定点执行约束检查。\u003c/p\u003e\u003cp\u003e当前，只有\u003ccode\u003eUNIQUE\u003c/code\u003e、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e、\u003ccode\u003eREFERENCES\u003c/code\u003e（外键）以及\u003ccode\u003eEXCLUDE\u003c/code\u003e 约束受到这个设置的影响。\u003ccode\u003eNOT NULL\u003c/code\u003e和\u003ccode\u003eCHECK\u003c/code\u003e约束总是在一行被插入或修改时立即检查（\u003cspan\u003e\u003cem\u003e不是\u003c/em\u003e\u003c/span\u003e在语句结束时）。未声明为\u003ccode\u003eDEFERRABLE\u003c/code\u003e的唯一约束和排他约束也会立即检查。\u003c/p\u003e\u003cp\u003e被声明为\u003cspan\u003e“\u003cspan\u003e约束触发器\u003c/span\u003e”\u003c/span\u003e的触发器，其引发也受此设置控制 — 它们会在相关约束应当被检查的同时引发。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e因为\u003cspan\u003ePostgreSQL\u003c/span\u003e并不要求约束名称在同一模式内唯一（只要求在每个表内唯一），所以指定的约束名称有可能匹配到多个约束。在这种情况下，\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e会作用于所有匹配项。对于未带模式限定的名称，一旦在搜索路径中的某个模式里找到一个或多个匹配项，就不会再搜索路径中更靠后的模式。\u003c/p\u003e\u003cp\u003e这个命令只会改变当前事务中约束的行为。在事务块之外发出该命令会产生一条警告，除此之外不会有任何效果。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e这个命令符合 SQL 标准定义的行为，但有一个限制：在 \u003cspan\u003ePostgreSQL\u003c/span\u003e中，它不会应用在 \u003ccode\u003eNOT NULL\u003c/code\u003e和\u003ccode\u003eCHECK\u003c/code\u003e约束上。此外，\u003cspan\u003ePostgreSQL\u003c/span\u003e会立即检查不可延迟的唯一约束，而不是像标准所暗示的那样在语句结束时检查。\u003c/p\u003e\u003c/section\u003e","SourceRevision":"ca936764","ContentHash":"01793ce02efe6b3e1b97e4de6480ca5991386caaa9d476ca7525dfba6fda754d","Payload":{"purpose_zh":"为当前事务设置约束检查时机","sections":[],"sections_same_as":"10","synopsis_html":"SET CONSTRAINTS { ALL | \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [, ...] } { DEFERRED | IMMEDIATE }","synopsis_text":"SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
