触发器 随写

本文介绍了一个MySQL触发器的具体实现案例,包括如何在插入和更新数据前利用CRC32函数自动计算并设置字段值,以及更新关联表中的数据。此外,还讨论了触发器与存储过程之间的交互可能性。

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

列子:

create table testproc if not exists (

`i_id` int(11) primary key,

`i_name` varchar(30) character set utf8 not null,

`url` varchar(40) character set utf8 not null

)engine=myisam default charset=utf8;


create table tree_test if not exists(

`form_id` int(11) not null,

`parent_form_id` int(11) not null

)engine=myisam default charset=utf8;

drop trigger if exists pseudohash_crc_ins;
create trigger pseudohash_crc_ins before insert on testproc  for each row
begin set @x = "hello trigger";
      
       set NEW.i_name=crc32(NEW.url);
       set new.url=@f;
end;
drop trigger if exists pseudohash_crc_upd;
create trigger pseudohash_crc_upd before update on testproc  for each row
begin set @x = "hello trigger";
      update tree_test set  parent_form_id=crc32(NEW.url)where form_id=new.i_id;
end;

前提是  必须有这两张表  tree_test,testproc


before insert  :是说 在往自己表里插入一条数据之前  需要做的事情,这里的逻辑是  插入之前  现在要插入的 url字段参数 用crc32 转成一串数字 然后再赋值给当前要插入的i_name字段,如果你插入值  写了 i_name 参数,那么这儿会把你写的那个参数值覆盖掉。

new : 是代表  你当前将要修改的  或者插入的  一条数据

old:  是代表   之前最后一次 修改过的  或者插入过的数据

before update:  修改之前

after   update : 修改之后

不知道  存储过程 和函数 能不能和触发器 在一块用,因为有的文章说“  触发程序不能调用将数据返回客户端的存储程序,也不能使用采用CALL语句的动态SQL
(允许存储程序通过参数将数据返回触发程序)
”,没太看懂,没测试。


如下有个截图:

优缺点:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值