Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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_Transactions_Insert - Fatal编程技术网

Sql 等待查询完成,直到在表上注册新数据

Sql 等待查询完成,直到在表上注册新数据,sql,postgresql,transactions,insert,Sql,Postgresql,Transactions,Insert,我正在从sql迁移信息 Table1 Table2 Table3 Table4 表1是独立的 表2需要表1 id 表3需要表2 id 表5需要表1 id 等等 有许多桌子。我正在使用SQL INSERT查询迁移所有信息 insert into table1 (data1,data2) values ('val','val') insert into table2 (data1,data2,id_table1) values ('val','val', (select id from t

我正在从sql迁移信息

Table1
Table2
Table3
Table4
表1是独立的
表2需要表1 id
表3需要表2 id
表5需要表1 id
等等

有许多桌子。我正在使用SQL INSERT查询迁移所有信息

insert into table1 (data1,data2) values ('val','val')
insert into table2 (data1,data2,id_table1) values ('val','val',
    (select id from table1 order by id desc limit 1)
)
insert into table3 (data1,data2,id_table1) values ('val','val',
    (select id from table2 order by id desc limit 1)
)
insert into table5 (data1,data2,id_table1) values ('val','val',
    (select id from table1 order by id desc limit 1)
)
问题是:如果一些用户在我执行查询时插入数据,那么我的迁移逻辑就会失败


在查询完成之前,如何停止表1、表2、表3、表4的数据注册?也许可以使用事务表,如laravel?

您的设置取决于这样一个事实,即
从表1中选择id按id描述限制1
始终会为您提供插入的
id
。因此,为了使设计正常工作,您需要一个一致的状态/快照来查看数据。Postgres的MVCC框架可以帮助您实现这一点:围绕
BEGIN。。COMMIT
块应该帮助您实现这一点——您还必须首先执行
设置事务隔离级别repeatable read在发出第一个
插入
查询之前。这样,其他用户仍然可以插入,但您将始终看到相同的快照。

“Table5需要Table1 id”-这是一个输入错误吗?是的,可能会发生这种情况。