Mysql 触发器

Posted cd ~/Homestead

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 触发器相关的知识,希望对你有一定的参考价值。

insert_trigger

	BEGIN
	if(NEW.id not in (select sourceid from site_newsall where catid = NEW.catid and sourceid = NEW.id ))then
		insert into table1 (value1,value2,value3)
		values (NEW.value1,NEW.value2,NEW.value3);
	end if;
	END

update_trigger

	BEGIN
	if(NEW.id not in (select sourceid from site_newsall where catid = NEW.catid and sourceid = NEW.id )) then
		insert into table1 (value1,value2,value3)
		values (NEW.value1,NEW.value2,NEW.value3);
	 ELSE
		UPDATE table1  SET value1=NEW.value1,value2=NEW.value2,value3=NEW.value3 where  id=NEW.ID;
	end if;
	END

delete_trigger

	BEGIN
	if( OLD.id in (select id from table1 where id = OLD.id  ))
		then
			DELETE FROM table1 where  id=OLD.id;
	end if;
	END

当前表更新后修改当前表

BEGIN
    set NEW.field1 = (SELECT * FROM (SELECT id from table1 WHERE id = NEW.id ) AS TEM1);
    set NEW.field2 = (select *from ( select sum(number) from table3 where userid = NEW.userid) );
END;

以上是关于Mysql 触发器的主要内容,如果未能解决你的问题,请参考以下文章

怎么在Dbeaver中创建mysql 触发器

请教mysql中触发器的写法

mysql触发器使日期相加减?

mysql添加触发器后无法插入数据的问题

MySQL触发器

猎八哥浅谈MYSQL触发器