Mysql 触发器和存储过程

本文详细介绍了如何在数据库中创建表、复制表、建立主键自增触发器,并通过编写存储过程实现数据的批量插入与更新。重点展示了使用游标和LOOP循环的存储过程,最终验证了数据成功插入到新表中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先建表:
create table tababin(
id int not null auto_increment,
name varchar(100),
constraint pk primary key(id)
)

2、拷贝一张相同的表:
create table tababin1 like tababin;

3.建立主键自增触发器:
create trigger triabin before insert on tababin for each ROW
begin
set @new=new.id;
end

4、插入记录:
insert into tababin (name) values ('abin1')
insert into tababin (name) values ('abin2')
insert into tababin (name) values ('abin3')

5‘编写存储过程(带游标和LOOP循环的存储过程):
CREATE PROCEDURE pabin()
begin
declare id,status int ;
declare name varchar(100);
declare mycur cursor for select * from tababin;
declare continue handler for not found set status=1;
open mycur;
set status=0;
loopLabel:loop
fetch mycur into id,name;
if status=0 then
if id is not null then
if name is not null then
insert into tababin1 values (id,name);
end if;
end if;
end if;
if status =1 then
leave loopLabel;
end if;
end loop;
close mycur;
end

6、测试存储过程:
call pabin()


结果:tababin1表里面新增了数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值