Mysql中除了使用auto_increment字段作為自增序號以外 還有另外一種辦法 可以提供唯一序列號并且可以由一張表完成對多個序列
要求的滿足。
大致如下:
1.創(chuàng)建一個簡單表
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,同一張表可對多個應(yīng)用提供Seq序列服務(wù)
3.不管多進(jìn)程 or 單進(jìn)程對SeqTab表同時進(jìn)行訪問,使用一下方法獲取序列號
1st->update SeqTab set iSeqNo=last_insert_id(iSeqNo+1) where iSeqType=13;
2nd->select last_insert_id();
4.因多進(jìn)程對SeqTab同時訪問,進(jìn)行update SeqTab set iSeqNo=last_insert_id(iSeqNo+1);時,數(shù)據(jù)庫保證了update的事務(wù)
完整性,且last_insert_id()是與當(dāng)前mysql連接相關(guān)的,所以多進(jìn)程同時使用時,也不會沖突。
posted on 2012-12-05 11:54
梨樹陽光 閱讀(1713)
評論(2) 編輯 收藏 引用 所屬分類:
數(shù)據(jù)庫