Home >Database >Mysql Tutorial >大区中分配玩家唯一ID的办法_MySQL

大区中分配玩家唯一ID的办法_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:38:031241browse

bitsCN.com


大区中分配玩家唯一ID的办法

 

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连接相关的,所以多进程同时使用时,也不会冲突。

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn