如何在SELECT上锁定行后跟UPDATE

时间:2016-01-14 05:08:56

标签: oracle locking

在以下情况中锁定单个行的最佳方法是什么:

--TODO - LOCK THIS ROW
-- Return the next id
SELECT  next_id
INTO    next_id_out
FROM owner.my_id_table
WHERE   app_id = app_id_in;

-- Update the next id on the table
UPDATE owner.my_id_table
SET next_id = next_id_out + 1
WHERE app_id = app_id_in;

我需要确保没有任何内容改变我之间的id表 选择id并使用下一个可用id更新表。

PS。是的我是oracle的新手:)

1 个答案:

答案 0 :(得分:2)

您只需在public function postForm() { $input = Input::all(); $profile = new Profile(); $profile->first_name = $input['first_name']; $profile->last_name = $input['last_name']; $profile->password = $input['password']; $profile->save(); //so when the admin create new form, I want to create new table on database //and this save() will change into dynamic like //$education = new Education(); //base on form on DB }

中添加FOR UPDATE即可
SELECT

当然,在更新之前使用SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in FOR UPDATE; 来锁定行没有多大意义。只需运行SELECT即可锁定该行。