《Mysql学习mysql触发器的三个例子》要点:
本文介绍了Mysql学习mysql触发器的三个例子,希望对您有用。如果有疑问,可以联系我们。
MYSQL实例本节内容:
mysql触发器实例
MYSQL实例例1,
MYSQL实例例2,
MYSQL实例例3,
MYSQL实例mySql触发器实例
MYSQL实例在mysql中,使用触发器能进行一些约束.
例子,当Student表的StudentID列被发生更改时,BorrowStudent表的StudentID列也跟着更改.如果Student表删除某记录,BorrowStudent也删除对应StudentID的记录.
MYSQL实例/*先删除将要创建而存在的表*/
drop table if exists Student;
drop table if exists BorrowStudent;
MYSQL实例/*创建表*/
create table Student(
StudentID int not null primary key,
StudentName varchar(30) not null,
StudentSex enum('m','f') default 'm'
)engine=myisam;
MYSQL实例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;
MYSQL实例/*插入记录*/
insert into Student values(1235412,'java','m');
insert into Student values(3214562,'jiajia','m');
insert into Student values(5441253,'purana','f');
MYSQL实例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');
MYSQL实例/*创建触发器*/
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$$
MYSQL实例create trigger tddStudent before delete
on Student for each row
begin
delete
from BorrowStudent
where BorrowStudent.StudentID=old.StudentID;
end$$
delimiter ;
《Mysql学习mysql触发器的三个例子》是否对您有启发,欢迎查看更多与《Mysql学习mysql触发器的三个例子》相关教程,学精学透。维易PHP学院为您提供精彩教程。