Oracle 触发器 插入,更新,删除,数据同步,两表同步
MySql触发器实例 2012-08-01 12:11:52| 分类: MySql | 标签:mysql 资料 问题解决方案 |字号大
中
小 订阅
触发器能进行一些约束.
这是个小例子,当Student表的StudentID列被发生更改时,BorrowStudent表的StudentID列也跟着更改.如果Student表删除某记录,BorrowStudent也删除对应StudentID的记录.
/*先删除将要创建而存在的表*/
drop table if exists Student;
drop table if exists BorrowStudent;
/*创建表*/
create table Student(
StudentID int not null primary key,
StudentName varchar(30) not null,
StudentSex enum('m','f') default 'm'
)engine=myisam;
create table BorrowStudent(
BorrowRecord int not null auto_increment primary key,
StudentID int not null,
BorrorDate date,
ReturnDate date,
foreign key(StudentID) references Student(StudentID)
)engine=myisam;
/*插入记录*/
insert into Student values(1235412,'java','m');
insert into Student values(3214562,'jiajia','m');
insert into Student values(5441253,'purana','f');
insert into BorrowStudent(StudentID,BorrorDate,ReturnDate)
values(1235412,'2007-01-01','2007-01-07');
insert into BorrowStudent(StudentID,BorrorDate,ReturnDate)
values(3214562,'2007-01-01','2007-01-07');
insert into BorrowStudent(StudentID,BorrorDate,ReturnDate)
values(5441253,'2007-01-01','2007-01-07');
/*创建触发器*/
delimiter $$
drop trigger if exists tduStudent$$
drop trigger if exists tddStudent$$
create trigger tduStudent before update
on Student for each row
begin
if new.StudentID!=old.StudentID then
update BorrowStudent
set BorrowStudent.StudentID=new.StudentID
where BorrowStudent.StudentID=old.StudentID;
end if;
end$$
create trigger tddStudent before delete
on Student for each row
begin
delete
from BorrowStudent
where BorrowStudent.StudentID=old.StudentID;
end$$
delimiter ;
摘自:http://hi.baidu.com/gb4215287/item/6d96714b39cd9de61381
分享到:
相关推荐
mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql触发器使用mysql...
mysql触发器+存储过程
MySQL触发器教程
MYSQL触发器,可以使用它用来进行监控,例如商品价格变化,下单后商品库存减少等实用性操作!
MYSQL触发器在PHP项目中用来做信息备份、恢复和清空 MYSQL触发器在PHP项目中用来做信息备份、恢复和清空
我们已经去掉了关于触发器的错误的语句,但是仍旧有很多旧版本的手册在网上,举个例子,这是一个德国的Url上的: http://dev.mysql.com/doc/mysql/de/ANSI_diff_Triggers.html. 这个手册上说触发器就是存储过程,...
MySQL触发器
MySQL触发器说明和编写时的注意事项,其功能越来越强,可以学习
mysql触发器教程
本文实例讲述了mysql触发器之创建多个触发器操作。分享给大家供大家参考,具体如下: 这次记录的内容mysql 版本必须得是5.7.2+的哈,之前的会不好使的。废话不多说,咱们开始正文哈。 在mysql 5.7.2+版本之前,我们...
本文实例讲述了mysql触发器之创建使用触发器。分享给大家供大家参考,具体如下: 我们可以可以使用CREATE TRIGGER语句创建一个新的触发器,来看下具体的语法: CREATE TRIGGER trigger_name trigger_time trigger_...
主要介绍了mysql触发器之触发器的增删改查操作,结合实例形式分析了mysql触发器的定义及使用触发器进行增删改查等相关操作技巧,需要的朋友可以参考下
MySQL触发器、存储过程、自定义函数、视图简单示例
Mysql触发器的例子,包括,创建,删除,修改,三种触发器的实例
mysql触发器使用实例 看下例子基本上就会操作了!
mysql触发器.pdf
MySQL触发器的使用说明,利用函数每秒触发一次...............................................................