Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在事务处理过程中,两条语句之间是否会应用约束?_Sql_Postgresql_Many To Many_Constraints_Cascading Deletes - Fatal编程技术网

Sql 在事务处理过程中,两条语句之间是否会应用约束?

Sql 在事务处理过程中,两条语句之间是否会应用约束?,sql,postgresql,many-to-many,constraints,cascading-deletes,Sql,Postgresql,Many To Many,Constraints,Cascading Deletes,我有一个配置,一个东西可以有多个属性,一个属性可以属于多个东西: CREATE TABLE thing ( id integer NOT NULL, PRIMARY KEY (id) ); CREATE TABLE property ( id integer NOT NULL, PRIMARY KEY (id) ); CREATE TABLE thingproperty ( thing integer NOT NULL, property integer NO

我有一个配置,一个东西可以有多个属性,一个属性可以属于多个东西:

CREATE TABLE thing (
  id integer NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE property (
  id integer NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE thingproperty (
    thing integer NOT NULL,
    property integer NOT NULL
);

ALTER TABLE thingproperty
ADD CONSTRAINT tp_thing
FOREIGN KEY (thing) REFERENCES thing(id)
ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE thingproperty
ADD CONSTRAINT tp_property
FOREIGN KEY (property) REFERENCES property(id)
ON UPDATE CASCADE ON DELETE CASCADE;
我想确保一个属性只有在它至少属于一个事物时才能存在,为此,我编写了一个事务,该事务删除了事物,必要时还删除了属性,但我不知道它是否正确:

START TRANSACTION;
DELETE FROM thing ... ;
DELETE FROM property
WHERE id NOT IN (
  SELECT property
  FROM thingproperty
);
COMMIT TRANSACTION;
所以我基本上相信引擎在删除之后。。。查询已运行,它立即应用tp_thing约束,并在从属性中删除之前删除属于已删除内容的thingproperty记录。。。被执行


这是一种安全的方法吗?

默认情况下,事务中的每个命令都会立即应用约束。在这种情况下,您可以不希望更改将约束声明为可延迟的行为。请阅读中有关可延迟约束的更多信息

推迟

不可推迟

这控制是否可以延迟约束。不可延迟的约束将在每个命令后立即检查