触发器 随写

来源:互联网 发布:天下3萝莉捏脸数据 编辑:程序博客网 时间:2024/05/20 09:47

列子:

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
(允许存储程序通过参数将数据返回触发程序)
”,没太看懂,没测试。


如下有个截图:

优缺点:



0 0
原创粉丝点击