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