如何测试mysql触发器和存储过程
程序员文章站
2023-12-20 22:06:58
1. 为了测试触发器和存储过程,首先建立一张简单的表: 复制代码 代码如下: create table `airuser` ( `userid` int(11) not n...
1. 为了测试触发器和存储过程,首先建立一张简单的表:
create table `airuser` (
`userid` int(11) not null auto_increment,
`username` varchar(128) not null,
primary key (`userid`)
)engine=innodb default charset=utf8
2. 为该表的插入操作,创建一张记录表:
create table `airuser_record` (
`id` int(11) not null auto_increment,
`username` varchar(45) default null,
`edittime` timestamp null default null,
`edittype` varchar(45) default null,
primary key (`id`)
) engine=innodb default charset=utf8
3. 编写一个插入操作的触发器:
drop trigger insert_trigger;
delimiter |
create trigger insert_trigger before insert on airuser
for each row begin
insert into airuser_record set username = new.username, edittime=now(), edittype='insert';
end;
show triggers;
4. 为批量插入编写存储过程:
drop procedure createusers;
delimiter |
create procedure createusers(in count int)
begin
declare i int;
set i=0;
while i<count do
insert into airuser set username=concat('user_',i);
set i=i+1;
end while;
end;
show procedure status;
5. 调用存储过程,验证存储过程是工作的,并验证在插入记录前,触发器能正确被触发:
call createusers(10);
6. 最后通过插入记录表再次验证:
select * from mars_jpa.airuser_record;
复制代码 代码如下:
create table `airuser` (
`userid` int(11) not null auto_increment,
`username` varchar(128) not null,
primary key (`userid`)
)engine=innodb default charset=utf8
2. 为该表的插入操作,创建一张记录表:
复制代码 代码如下:
create table `airuser_record` (
`id` int(11) not null auto_increment,
`username` varchar(45) default null,
`edittime` timestamp null default null,
`edittype` varchar(45) default null,
primary key (`id`)
) engine=innodb default charset=utf8
3. 编写一个插入操作的触发器:
复制代码 代码如下:
drop trigger insert_trigger;
delimiter |
create trigger insert_trigger before insert on airuser
for each row begin
insert into airuser_record set username = new.username, edittime=now(), edittype='insert';
end;
show triggers;
4. 为批量插入编写存储过程:
复制代码 代码如下:
drop procedure createusers;
delimiter |
create procedure createusers(in count int)
begin
declare i int;
set i=0;
while i<count do
insert into airuser set username=concat('user_',i);
set i=i+1;
end while;
end;
show procedure status;
5. 调用存储过程,验证存储过程是工作的,并验证在插入记录前,触发器能正确被触发:
复制代码 代码如下:
call createusers(10);
6. 最后通过插入记录表再次验证:
复制代码 代码如下:
select * from mars_jpa.airuser_record;