问题描述
我想在一段时间内锁定一个表以进行写入,同时使其可供读取。
可能吗?
理想情况下,我想用谓词锁定表(例如,防止写行 where country = France
)。
I would like to lock a table for writing during a period of time, while leaving it available for reading.Is that possible ?Ideally I would like to lock the table with a predicate (for example prevent writing rows "where country = france
").
推荐答案
如果您真的想针对此类插入内容锁定,即查询应该挂起并且仅在允许时继续执行,在表上放置 SHARE
锁并保持交易打开。
If you really want to lock against such inserts, i.e. the query should hang and only continue when you allow it, you would have to place a SHARE
lock on the table and keep the transaction open.
通常这不是一个好主意。
This is usually not a good idea.
如果要防止插入任何此类插入,即尝试插入时抛出错误,请创建插入前
触发器,如果 NEW
行满足条件,则会引发异常。
If you want to prevent any such inserts, i.e. throw an error when such an insert is attempted, create a BEFORE INSERT
trigger that throws an exception if the NEW
row satisfies the condition.
这篇关于如何锁定表格进行写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!