Mysql 儲存程式

2022-05-18 11:05:19 字數 2804 閱讀 5458

#1儲存過程

create procedure greeting()

begin

# 77 = 16 for username + 60 for hostname + 1 for '@'

declare user char(77) character set utf8;

set user = (select current_user());

if inster(user, '@') > 0 then

set user = substring_index(user, '@',1);

end if;

if user = '' then

set = 'earthling';

end if;

select concat('greetings,', user, '!') as greetig;

end;

#時間儲存函式

delimiter $$

create procedure show_time3()

begin

select 'local time is :', current_timestamp;

select 'utc time is :' , utc_timestamp;

end $$

delimiter ;

delimiter eof

create procedure show_time3()

select 'local time is:', current_timestamp;

select 'utc time is:', utc_timestamp;

end eof

delimiter ;

#2儲存函式

delimiter $

create function show_id_ord(id int)

returns int

reads sql data

begin

return (select count(*) from test where id = id)

end$

delimiter ;

delimiter $

create procedure update_test_test_id(p_id int, p_date int)

begin

update test set test_id = p_date where id = p_id;

end$

delimiter ;

delimiter $

create procedure select_test_id (out p_id int)

begin

select count(*) from test where id = p_id into p_id;

end $

delimiter ;

drop table if exists `user_info`;

create table `user_info` (

`id` int(10) unsigned not null auto_increment comment '使用者id',

`user_name` varchar(50) not null default '' comment '使用者名稱',

`gender` tinyint(1) not null default '0' comment '使用者性別;1:男,2:女',

`add_time` int(10) unsigned not null default '0' comment '使用者註冊時間',

`update_timne` timestamp not null default current_timestamp on update current_timestamp comment '使用者最後更新時間',

primary key (`id`) using btree,

key `user_name` (`user_name`),

key `gender` (`gender`),

key `add_time` (`add_time`)

) engine=myisam auto_increment=10 default charset=utf8 comment='使用者資訊表'

#3觸發器

delimiter $

create trigger ai_t after insert on user_info

for each row begin

inster into test(`test_id`) values((select test_id from test_id order by id desc limit 1 )+1)

end $

delimiter ;

delimiter $

create trigger bi_t before insert on t

for each row begin

set new.dt = current_timestamp;

if new.parent <0 then

set new.parent = 0;

elseif new.parent >100 then

set new.parent = 100;

end if;

end$

delimiter ;

#4建立事件

create event id_defined_min

on schedule every 0.02 hour

do delete from test where id <9 ;

mysql儲存程式包括 Mysql 儲存程式

1儲存過程 create procedure greeting begin 77 16 for username 60 for hostname 1 for declare user char 77 character set utf8 set user select current user if...

MySQL 儲存程式之儲存過程

1.儲存引擎簡介是mysql的核心,一組軟體包括快取軟體,語法語義分析,解析的軟體等,不同的儲存引擎提供不同的儲存機制,索引技巧,鎖定水平等功能。可以根據不同的功能來設定不同的儲存引擎。innodb 事務性資料庫的首選,執行安全性資料庫,行鎖定和外來鍵。顯示支援的儲存引擎 show engines ...

MySQL儲存程式許可權控制

mysql儲存程式許可權控制 mysql5.0引入了一些管理儲存程式的許可權 code create routine 允許使用者建立新的儲存程式 alter routine 允許使用者修改security mode sql mode comment execute 允許使用者只需儲存程式 code ...