mysql全局唯一id-凯发k8官方网
因为mysql没有oracle中的sequence概念,所以需要建立一个表来模拟sequence,同时也可以作为高位数据保存。
create table if not exists `sys_sequence` (
`id` int(11) unsigned not null,
`no` varchar(4) not null default '',
`name` varchar(64) not null default ''
) engine=myisam auto_increment=0 default charset=utf8;
alter table `sys_sequence` add primary key (`id`);
创建mysql函数:
create function `get_sequence`(`in_name` varchar(64)) returns varchar(16) charset utf8
begin
declare p_last_id int;
declare p_no varchar(4);
declare p_result varchar(16);
declare p_max_no int;
insert into sys_sequence () values ();
select last_insert_id() into p_last_id;
delete from sys_sequence where id = p_last_id;
if(in_name is not null and in_name <> '') then
select no into p_no from sys_sequence where name = in_name;
if(p_no is null) then
select max(cast(no as unsigned)) into p_max_no from sys_sequence;
set p_no = lpad(p_max_no 1, 4, 0);
insert into sys_sequence (no, name) values (p_no, in_name);
end if;
else
set p_no = '0000';
end if;
set p_result = concat(p_no, lpad(p_last_id, 12, 0));
return p_result;
end
参考资料
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是凯发k8官方网为你收集整理的mysql全局唯一id_mysql实现全局唯一id的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 狂神说mysql笔记_mybatis学习
- 下一篇: protect 继承_c prote