專案中對於mysql資料庫自定義函式的應用

2021-08-30 21:01:42 字數 2210 閱讀 2642

該函式滿足的乙個需求就是,產生乙個自增的編碼。

首先要建立乙個表。來儲存你上乙個建立欄位的資訊

drop table if exists `dicnotable`;

create table `dicnotable` (

`id` int(11) not null auto_increment,

`databasename` varchar(20) default null,

`tablename` varchar(20) default null,

`prefix` varchar(20) default null,

`total` varchar(20) default null,

`numlength` varchar(20) default null,

`info` varchar(200) default null,

`operid` varchar(20) default null,

`opername` varchar(20) default null,

`registerdate` timestamp null default current_timestamp on update current_timestamp,

primary key  (`id`)

) engine=innodb auto_increment=2 default charset=gb2312;

-- ----------------------------

-- records

-- ----------------------------

insert into `dicnotable` values ('1', 'boss', 'person', 'kgep', '0', '4', '員工編碼', 'kgep9999', '張三, '2010-09-10 14:30:25');

每增加乙個資料,會在所建立表的total欄位上加一,這樣就儲存了上乙個建立的id,

然後寫乙個自定義的函式,通過傳入表名,然後取出total,然後再加上自定義的kgep,

最後得到的自增字段的結果,例如"kgep0001"

delimiter $$

drop function if exists `f_getsysid` $$

create

function `f_getsysid`( tablename varchar(30))

returns varchar(30)

begin

declare tprefix varchar(20);

declare ttotal varchar(20);

declare tnumlength varchar(20);

declare zerostring varchar(20);

set @prefix='';

set @total  ='';

set @length=0 ;

set @totallength =0;

set @zeronum=0;

select  prefix into tprefix from  dicnotable where  tablename=tablename;

select total into ttotal from  dicnotable where  tablename=tablename;

select  numlength into tnumlength from  dicnotable where  tablename=tablename;

set @totallength=length(ttotal+1);

if @totallength>tnumlength

then

set zerostring='1';

end if;

if @totallength<=tnumlength

then

set zerostring=ttotal+1;

while   @zeronumset zerostring=concat('0',zerostring);

set  @zeronum=@zeronum+1;

end while;

set zerostring=concat(tprefix,zerostring);

end if;

return zerostring; 

end$$

delimiter ;

學習資料庫的開始之巨集觀上了解專案中資料庫的設計過程

專案中資料庫設計的步驟 1.需求分析 2.概念結構設計 3.邏輯結構設計 4.資料庫的物理設計 5.資料庫實施和維護 正在做乙個簡單的網頁專案,在自己對資料庫設計一點都懂的情況下隨便設計了兩個資料庫表並已實現基於兩個資料表的簡單的處理過程。後來在老師的提醒下才知道乙個專案的 資料庫不是憑著自己的一點...

django 專案中資料庫的配置,及連線

首先去settings裡邊對資料庫進行配置 default 然後需要去init中輸入以下 import pymysql pymysql.install as mysqldb 現在就可以去建立所需要表和字段了 類名就是建立的表名,下邊的屬性為表中的字段,我們常用的有charfield 字元格式 int...

java canal對於資料庫配置

現場部署專案時候出現問題,排查時候發現是canal啟動失敗 2019 03 15 11 47 38.486 destination example address 127.0.0.1 3306 eventparser error c.a.o.c.p.inbound.mysql.rds.rdsbinl...