Mysql中除了使用auto_increment字段作為自增序號(hào)以外 還有另外一種辦法 可以提供唯一序列號(hào)并且可以由一張表完成對(duì)多個(gè)序列
要求的滿足。
大致如下:
1.創(chuàng)建一個(gè)簡單表
create table SeqTab
( iSeqNo int not null default 0,
iSeqType int not null default 0);
2.插入一調(diào)記錄
insert into SeqTab values(0,13); // 0表示SeqNo初始值,13為SeqType,同一張表可對(duì)多個(gè)應(yīng)用提供Seq序列服務(wù)
3.不管多進(jìn)程 or 單進(jìn)程對(duì)SeqTab表同時(shí)進(jìn)行訪問,使用一下方法獲取序列號(hào)
1st->update SeqTab set iSeqNo=last_insert_id(iSeqNo+1) where iSeqType=13;
2nd->select last_insert_id();
4.因多進(jìn)程對(duì)SeqTab同時(shí)訪問,進(jìn)行update SeqTab set iSeqNo=last_insert_id(iSeqNo+1);時(shí),數(shù)據(jù)庫保證了update的事務(wù)
完整性,且last_insert_id()是與當(dāng)前mysql連接相關(guān)的,所以多進(jìn)程同時(shí)使用時(shí),也不會(huì)沖突。
posted on 2012-12-05 11:54
梨樹陽光 閱讀(1718)
評(píng)論(2) 編輯 收藏 引用 所屬分類:
數(shù)據(jù)庫