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

2021-10-19 06:45:46 字數 2793 閱讀 5000

#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

dodelete from test where id <9 ;

mysql變數包括什麼意思 什麼是mysql變數?

什麼是mysql的變數?mysql變數分為幾種?在mysql資料庫中佔據了怎樣的地位?今天將為你講解mysql的變數。mysql變數是mysql資料庫中的基礎知識,下面就為您詳細介紹mysql變數,如果您對此方面感興趣的話,不妨一看,相信對您有所幫助。mysql變數介紹 mysql伺服器的系統變數,...

Oracle 程式包裡 直接呼叫 儲存過程

建立乙個包 test1 create or replace package test1 is procedure t1 function t2 return number end test1 建立包的主體 test1,裡面有個儲存過程 t1,還有乙個呼叫 t1 create or replace p...

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...