mariadb使用函式實現oracle的序列功能

2021-10-06 14:58:15 字數 971 閱讀 4742

mariadb使用函式實現oracle的序列功能

由於mariadb中沒有序列,使用函式實現oracle的序列功能

1、建立序列專用表 序列名 初始值 步長1

create table mariadb_seq (

name varchar(50) not null primary key,

start_value int not null,

increment_value int not null default 1

);

2、建立序列

insert into mariadb_seq values('user_seq',1,1);
3、定義獲取序列函式user_nextval

delimiter //

create function user_nextval(str varchar(50)) returns integer

begin

declare i int;

set i=(select start_value from mariadb_seq where name=str);

-- 最大99999999,超過後然後置為1

if i<99999999

then update mariadb_seq

set start_value=i+increment_value

where name=str;

else

update mariadb_seq

set start_value=1

where name=str;

end if;

return i;

end;

4、mybatis中查詢序列

select user_nextval('user_seq');

MariaDB使用說明

一 centos7上安裝mariadb資料庫安裝配置完成。2 許可權授予 授權遠端登入使用者 root使用者不做遠端登入 因為前面設定了 建立遠端訪問使用者 create user x 127.0.0.1 identified by ghj 為遠端使用者授權 grant all privileges...

MariaDB安裝與使用

環境 linux centos7 3.10.0 957.el7.x86 64 安裝mariadb 如果centos安裝的是minimal版本,需要先安裝mariadb yum install mariadb server.x86 64配置 安裝完成後,mariadb沒有啟動,使用 systemctl...

Centos 使用YUM安裝MariaDB

azureuser mono etc cd etc yum.repos d azureuser mono yum.repos d vi mariadb.repo拷貝以下內容 gpgcheck 1如果是其他的作業系統,可以在這裡找到相關資訊。azureuser mono yum.repos d sud...