如何在不犧牲完整性的情況下在MySQL DB表中獲得最新的set ID ?

[英]How do I get the latest set ID in a MySQL DB table without sacrificing integrity?


I'm using PHP to insert groups of records into a MySQL DB.
Whenever I insert a group of records, I want to give that group a unique set ID that is incremented by 1 for each group of records in the DB.

我使用PHP將記錄組插入到MySQL DB中。每當我插入一組記錄時,我想給該組一個唯一的集合ID,在DB中的每組記錄中增加1。

Currently, I'm checking the latest set ID in the DB and incrementing it by 1 for each new set of records.
The thing that scares me though is what happens if I query the DB to get the latest set ID, and before I can insert a new set of records with that set ID + 1, another insert occurs on the table thus taking the set ID I was about to use?
While fairly unlikely, something like that could greatly sacrifice the integrity of the data.

目前,我正在檢查DB中的最新set ID,並為每一組新記錄增加1。讓我害怕的事情是如果我查詢數據庫獲取最新的設置ID,和之前我可以插入一個新記錄的集合ID + 1,另一個發生在插入表因此采取集合ID我使用嗎?雖然不太可能,但這樣的事情可能會極大地犧牲數據的完整性。

What can I do to prevent such a thing from happening? Is there any way to temporarily lock the DB table so that no other inserts can occur until I have performed a SELECT/INSERT combo?

我能做些什么來阻止這樣的事情發生呢?是否有辦法暫時鎖定DB表,以便在執行SELECT/INSERT組合之前不會發生其他插入?

2 个解决方案

#1


4  

Locking the table is one option, but that approach impacts concurrency.

鎖定表是一種選擇,但是這種方法會影響並發性。

The approach I would recommend is that you use a separate table with AUTO_INCREMENT column, and use a separate INSERT into that table, and a SELECT LAST_INSERT_ID() to retrieve the auto_increment value.

我推薦的方法是使用帶有AUTO_INCREMENT列的單獨的表,並在該表中使用單獨的插入,並使用SELECT LAST_INSERT_ID()來檢索AUTO_INCREMENT值。

And then use that value as the group identifier for the group of rows you insert into your original table.

然后使用該值作為插入到原始表中的行組的組標識符。

#2


3  

The basic approach is:

基本方法是:

LOCK TABLE foo WRITE;
SELECT MAX(id) + 1 FROM foo
INSERT ...
INSERT ...
UNLOCK TABLES;

Locking the table prevents any other process from changing the table until you explicitly unlock it.

鎖定該表可防止任何其他進程更改該表,直到顯式解鎖該表。

Having said that, seriously consider just using a table with an AUTO_INCREMENT column. MySQL will do the work of maintaining unique keys wholly automatically, and then you can simply refer to those keys from your existing table.

說到這里,請認真考慮使用帶有AUTO_INCREMENT列的表。MySQL將完全自動地維護唯一鍵,然后您可以簡單地從現有表中引用這些鍵。

关注微信公众号

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/07/15/7df1bf2ed6a4817d975b3c34e03e7fe4.html



如何在不嘗試加載的情況下驗證Sybase轉儲文件的完整性? - How do I verify the integrity of a Sybase dump file, without trying to load it? 如何在不查找AspNetUserRoles表的情況下在WebAPI方法中獲取用戶角色? - How can I get a users role inside a WebAPI method without a lookup to the AspNetUserRoles table? 如何在PHP中獲得MySQL表的最后插入ID ? - How do I get the last inserted ID of a MySQL table in PHP? 如何在Postgres 8.2中禁用參照完整性? - How do I disable referential integrity in Postgres 8.2? Geeky在不犧牲傳入鏈接的情況下在IIS中搜索引擎友好的URL - Geeky urls to Search Engine Friendly urls in IIS without sacrificing incoming links 如何在沒有python或ruby的情況下在Vim中獲得計數器變量? - How can I get a counter variable in Vim without python or ruby? 如何在沒有小部件工廠的情況下在Yii2中設置GridView的默認配置? - How do I set a default configuration for GridView in Yii2 without the widget factory? 如何在沒有LOAD DATA INFILE的情況下在MySQL表中插入大量數據? - How to insert large amount of data in a MySQL table without LOAD DATA INFILE? 如何在不刷新html頁面的情況下在表中顯示數據(使用nodejs和mysql ajax) - How to Display data on a table without refreshing html page (using nodejs & mysql ajax) 如何在沒有params的情況下獲得文件名? - How do I get the filename without the params?
 
粤ICP备14056181号  © 2014-2020 ITdaan.com