mysql 自定義函式

2021-09-26 04:14:11 字數 1302 閱讀 3805

功能主要供學習。高併發場景不要使用mysql特性(觸發器,儲存過程,外來鍵,自定義函式等)使用會降低系統的併發性,資料庫公升級困難,遷移困難諸多問題。

1.建立生成多個表的序列號的資料維護表

create table `seq_counter` (

`id` int(11) not null auto_increment,

`name` varchar(20) not null comment '計數器名稱,在表中是唯一存在的',

`desc` varchar(255) default null comment '計數器描敘',

`num_length` tinyint(3) unsigned not null default '6' comment '數字長度,不夠左側補0',

`count` int(10) unsigned not null default '0',

primary key (`id`),

unique key `name` (`name`)

) engine=innodb default charset=utf8mb4;

2.插入幾條初始化資料

insert into `seq_counter` values (1,'busc','商家編號',6,0),(2,'plat','訂單編號',6,1000);
3.建立函式以生成序列號

create function seq(seq_name char (20)) returns int

begin

update seq_counter set count=last_insert_id(count+1) where name=seq_name;

return last_insert_id();

end

create function seq(seq_name char (20)) returns varchar(255)

begin

update seq_counter set count=last_insert_id(count+1) where name=seq_name;

return (select concat(seq_name, lpad(last_insert_id(),num_length,'0')) from seq_counter where name=seq_name);

end

4.測試

select seq('busc');

mysql自定義函式優點 MySQL自定義函式

在使用 mysql 的過程中,mysql 自帶的函式可能完成不了我們的業務需求,這時候就需要自定義函式。自定義函式是一種與儲存過程十分相似的過程式資料庫物件。它與儲存過程一樣,都是由 sql 語句和過程式語句組成的 片段,並且可以被應用程式和其他 sql 語句呼叫。自定義函式與儲存過程之間存在幾點區...

mysql自定義函式命名 MySQL自定義函式

在使用 mysql 的過程中,mysql 自帶的函式可能完成不了我們的業務需求,這時候就需要自定義函式。自定義函式是一種與儲存過程十分相似的過程式資料庫物件。它與儲存過程一樣,都是由 sql 語句和過程式語句組成的 片段,並且可以被應用程式和其他 sql 語句呼叫。自定義函式與儲存過程之間存在幾點區...

mysql 自定義函式

今天要做乙個排序,有中文和英文的,資料庫採用utf8編碼,排除來的不對,所以需要將中文轉換成中文的第乙個字母,然後來排序 先小小的看一下mysql的自定義函式 drop function if exists fntable 如果存在就刪除 delimiter 函式開始 create function...