第七节:MySQL触发器及备份与还原

您所在的位置:网站首页 数据备份的内容包括 第七节:MySQL触发器及备份与还原

第七节:MySQL触发器及备份与还原

2024-05-24 03:28| 来源: 网络整理| 查看: 265

一、触发器简介

​ 触发器是为了响应某个事件执行的任务,事件包括添加修改删除操作,任何前后触发器不能手动调用,创建好以后由SQL数据库自动触发

二、触发器分类

前触发器:before 在执行某个事件成功之前 而在个事件的不会真的操作表单

后触发器:after 在执行某个事件成功之后 先操作表单,而且操作成功以后才会执行after触发器

种类:insert、update、select

三、语法 create trigger 触发器名称-- 创建触发器 before/after -- 指定触发器类型 insert/update/select-- 指定触发器操作类型 on 表名 -- 指定该出发器针对那张表 for each row -- 指定对于在张表的所有行都起作用 begin 代码块 end; *注意:1、在触发器中获取更新前或更新后的数据可以使用new.字段名或old.字段名 2、在触发器中不能使用select输出 四、案例 -- insert 添加触发器 create trigger tr_Insert -- 创建触发器 after -- 指定触发器类型 insert -- 指定触发器操作类型 on student -- 指定该出发器针对那张表 for each row -- 指定对于在张表的所有行都起作用 begin declare stuName varchar(20); declare stuPwd varchar(20); declare stuAge varchar(20); declare stuSex varchar(20); declare msg varchar(200); set stuName = new.stuName; set stuPwd = new.stuPwd; set stuAge = new.stuAge; set stuSex = new.stuSex; set msg = concat('添加触发器:姓名:',stuName,',密码:',stuPwd,'性别:',stuSex,',年龄:',stuAge); -- 拼接字符串 insert into logInfo values(null,msg); end; -- update 修改触发器 drop table if exists Student; create table Student( id int not null primary key auto_increment, stuName varchar(20) not null, stuAge int not null, stuSex varchar(2) not null, remark varchar(50) ); -- 添加数据 insert into Student values(0,'xiaoge',18,'男','数据测试'); select * from Student; drop table if exists StudentLog; create table StudentLog( id int not null primary key auto_increment, log_Type varchar(20) not null, log_msg varchar(2000) not null ); -- 添加数据 insert into StudentLog values(0,'now','123456789'); select * from StudentLog; drop trigger if exists tr_xiaogeinsert; create trigger tr_xiaogeinsert after update on Student for each row begin declare id int; declare stuName varchar(20); declare stuAge int; declare stuSex varchar(2); declare remark varchar(50); declare newmsg varchar(2000); declare oldmsg varchar(2000); set id = new.id; set stuName = new.stuName; set stuAge = new.stuAge; set stuSex = new.stuSex; set remark = new.remark; set newmsg = concat('id:',id,',stuName:',stuName,',stuAge:',stuAge,',stuSex:',stuSex,',remark:',remark); insert into StudentLog values(0,'now',newmsg); set id = old.id; set stuName = old.stuName; set stuAge = old.stuAge; set stuSex = old.stuSex; set remark = old.remark; set oldmsg = concat('id:',id,',stuName:',stuName,',stuAge:',stuAge,',stuSex:',stuSex,',remark:',remark); insert into StudentLog values(0,'old',oldmsg); end; update Student set stuAge = 19 where id = 1; select * from StudentLog; -- deletee 删除触发器 *注意,再删除触发器中只能使用old.字段名获取数据 create trigger tr_update -- 创建触发器 after -- 指定触发器类型 delete -- 指定触发器操作类型 on student for each row -- 指定对于在张表的所有行都起作用 begin insert into logInfo values(null,concat('删除的数据编号是:',old.id)); end; 五、备份与还原

方法一、使用客户端软件,客服端软件备份小编在理就不多说了

方法二、命令操作(dos界面) 备份:mysqldump -uroot -p123456 数据库名 >D:/xxx.sql 还原:mysql -uroot -p123456 数据库名< D:/xxx.sql *注意:使用指令的需求,未登录状态下,并且要求配置有mysql_home并path的环境条件下


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3