mysql中序列怎么写 对其合理取得的财产享有什么权利

MySQL实现类似Oracle序列的方案

MySQL实现类似Oracle的序列

创新互联公司专业为企业提供醴陵网站建设、醴陵做网站、醴陵网站设计、醴陵网站制作等企业网站建设、网页设计与制作、醴陵企业网站模板建站服务,十多年醴陵做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Oracle一般使用序列(Sequence)来处理主键字段,而MySQL则提供了自增长(increment)来实现类似的目的;

但在实际使用过程中发现,MySQL的自增长有诸多的弊端:不能控制步长、开始索引、是否循环等;若需要迁移数据库,则对于主键这块,也是个头大的问题。

本文记录了一个模拟Oracle序列的方案,重点是想法,代码其次。

Oracle序列的使用,无非是使用.nextval和.currval伪列,基本想法是:

1、MySQL中新建表,用于存储序列名称和值;

2、创建函数,用于获取序列表中的值;

具体如下:

表结构为:

drop

table

if

exists

sequence;

create

table

sequence

(

seq_name

VARCHAR(50)

NOT

NULL,

--

序列名称

current_val

INT

NOT

NULL,

--当前值

increment_val

INT

NOT

NULL

DEFAULT

1,

--步长(跨度)

PRIMARY

KEY

(seq_name)

);

实现currval的模拟方案

create

function

currval(v_seq_name

VARCHAR(50))

returns

integer

begin

declare

value

integer;

set

value

=

0;

select

current_value

into

value

from

sequence

where

seq_name

=

v_seq_name;

return

value;

end;

函数使用为:select

currval('MovieSeq');

实现nextval的模拟方案

create

function

nextval

(v_seq_name

VARCHAR(50))

return

integer

begin

update

sequence

set

current_val

=

current_val

+

increment_val

where

seq_name

=

v_seq_name;

return

currval(v_seq_name);

end;

函数使用为:select

nextval('MovieSeq');

增加设置值的函数

create

function

setval(v_seq_name

VARCHAR(50),

v_new_val

INTEGER)

returns

integer

begin

update

sequence

set

current_val

=

v_new_val

where

seq_name

=

v_seq_name;

return

currval(seq_name);

同理,可以增加对步长操作的函数,在此不再叙述。

注意语法,数据库字段要对应上

use

bvboms;

DELIMITER

$$

create

function

setval(v_seq_name

VARCHAR(50),

v_new_val

INTEGER)

returns

integer

begin

update

sequence

set

current_val

=

v_new_val

where

seq_name

=

v_seq_name;

return

currval(seq_name);

end

$$

DELIMITER

$$

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:mysql实现sequence功能的代码Can''t

connect

to

local

MySQL

through

socket

''/tmp/mysql.sock''解决方法Mysql常用函数大全(分类汇总讲解)利用MySQL主从配置实现读写分离减轻数据库压力mysql+spring+mybatis实现数据库读写分离的代码配置Golang中如何对MySQL进行操作详解将图片储存在MySQL数据库中的几种方法MySQL存储文本和图片的方法Ubuntu上mysql的安装及使用(通用版)nodejs同步调用获取mysql数据时遇到的大坑

关于mysql 创建序列

mysql下序列是用关键字auto_crement,起始值及步长增长值由系统以下参数确定:

mysql show variables like '%auto_increment%';

+--------------------------+-------+

| Variable_name | Value |

+--------------------------+-------+

| auto_increment_increment | 1 |

| auto_increment_offset | 1 |

+--------------------------+-------+

2 rows in set (0.00 sec)

mysql

其中auto_increment_offset表示起始值(且必须由1开始),参数表示auto_increment_increment表示步长增长值(只能是正整数)。

建表示例:

create table t111

(id int auto_increment primary key,

remark varchar(50)

);

由上面所说可知,你的需求在mysql下单用auto_crement是实现不了的。建议你考虑别的办法吧,或由一些变通的方式实现。

mysql 如何创建序列?

比如说你创建了一个表userinfos

create table userinfos(

userid int primary key,

username varchar(20)

);

//给userinfos添加序列

update userinfos set userid = last_insert_id(userid+1);

//然后查询序列

select last_insert_id();

或者也可以这样

create table userinfos(

userid int primary key not null auto_increment,

username varchar(20)

);


新闻标题:mysql中序列怎么写 对其合理取得的财产享有什么权利
本文地址:http://ybzwz.com/article/ddsdoei.html