我需要以可序列化的方式执行这种逻辑:
account 表包含以下列:'accountName'、'location'、'worth'、...
假设不存在唯一键。
某些帐户逻辑上(根据应用程序)位于同一组中。
例子:
accountNameOne - 组 1 accountNameTwo - 组 1
帐户新一 - 组 2 帐户新二 - 组 2
有许多客户端应用程序试图同时插入其组中的一个帐户成员。我希望保证每个组只插入一个帐户。
BEGIN;
select *
from account
where accountName in (list of account names inside a group)
-- if result is not empty exit there's already an account of the group
-- otherwise we want to insert it
INSERT INTO account (accountName, column2, column3, ...)
VALUES ('accountInAGroup', value2, value3, ...);
COMMIT;
据我所知,可重复读取隔离级别对此无济于事,我猜我基本上只能使用 SERIALIAZABLE。
你能帮我理解我的推理是否正确吗?可重复读取真的不够吗?