mysql触发器 增删改

#创建触发器trigger_test实现数据库db_test6中students表增加内容时数据库db_test7 中students表同步
create trigger trigger_test3 after insert on students for each row

	insert into db_test7.students(stuNum,stuName,stuGender,stuAge) 
	values(new.stu_num,new.stu_name,new.stu_gender,new.stu_age); 

#测试增加数据库db_test6中students内容
insert into students(stu_num,stu_name,stu_gender,stu_age)
	values("1002","张三","男",20); 

#创建触发器trigger_test实现数据库db_test6中students表删除内容时数据库db_test7 中students表同步
create trigger trigger_test4 after delete on students for each row
begin
	delete from db_test7.students where stuNum=old.stu_num ;
end	
	
	#测试删除数据库db_test6中students中的内容
	delete from students where stu_num="1002";
	
	#创建触发器trigger_test实现students修改内容时studentstest同步
	create trigger trigger_test5 after update on students for each row
begin
	update db_test7.students set stuName=new.stu_name,stuGender=new.stu_gender,stuAge=new.stu_age where stuNum=old.stu_num ;
end

	#测试更新数据库db_test6中students中的内容
	update students set stu_name="lisi",stu_gender="女",stu_age=21 where stu_num=1002;

你可能感兴趣的:(mysql,数据库,sql)