在mysql命令行下执行:
DELIMITER $$ --将结束符定义成$$
CREATE TRIGGER `trigger_not_allow_del` --创建一个名字叫trigger_not_allow_del的触发器
BEFORE DELETE ON `merchant_objects` FOR EACH ROW --在删除merchant_objects表数据之前触发
BEGIN
DECLARE msg varchar(255); --定义一个msg变量
SET msg="不允许删除账单标准表信息"; --报错提示信息
SIGNAL SQLSTATE "HY000" SET MESSAGE_TEXT = msg; --在删除数据时引发 SQLSTATE HY000 的错误以及报错信息
END $$
DELIMITER ;--将结束符定义成;
当这张表的数据被删除时:
mysql> delete from merchant_objects where object_id = 57804 limit 1;
ERROR 1644 (HY000): 不允许删除账单标准表信息
mysql>
注意:Mysql 5.5 开始为我们提供了SIGNAL函数来实现这个功能,请确认你的mysql版本是否>=5.5
删除触发器:
drop trigger trigger_not_allow_del;
查询哪些库有哪些表有哪些触发器
select TRIGGER_SCHEMA,TRIGGER_NAME, EVENT_OBJECT_TABLE from information_schema.triggers;
如果你mysql版本是5.5以下的,试下下面这个:
DROP PROCEDURE if exists fbd_delete;#删除存储过程
--创建禁止删除的存储过程;回滚
DELIMITER $$
create procedure fbd_delete()
begin
rollback;
end $$
drop trigger if exists `trigger_not_allow_del`;#删除触发器
--在删除操作之前,调用存储过程;引起回滚
CREATE TRIGGER `trigger_not_allow_del`
BEFORE DELETE ON `merchant_objects` FOR EACH ROW
BEGIN
call fbd_delete ;
END $$
DELIMITER ;
还有一种办法,