《Mysql學(xué)習(xí)mysql觸發(fā)器的三個(gè)例子》要點(diǎn):
本文介紹了Mysql學(xué)習(xí)mysql觸發(fā)器的三個(gè)例子,希望對您有用。如果有疑問,可以聯(lián)系我們。
MYSQL實(shí)例本節(jié)內(nèi)容:
mysql觸發(fā)器實(shí)例
MYSQL實(shí)例例1,
?
MYSQL實(shí)例例2,
?
MYSQL實(shí)例例3,???
?
MYSQL實(shí)例mySql觸發(fā)器實(shí)例
MYSQL實(shí)例在mysql中,使用觸發(fā)器能進(jìn)行一些約束.
例子,當(dāng)Student表的StudentID列被發(fā)生更改時(shí),BorrowStudent表的StudentID列也跟著更改.如果Student表刪除某記錄,BorrowStudent也刪除對應(yīng)StudentID的記錄.
?
MYSQL實(shí)例/*先刪除將要?jiǎng)?chuàng)建而存在的表*/
drop table if exists Student;
drop table if exists BorrowStudent;
MYSQL實(shí)例/*創(chuàng)建表*/
create table Student(
StudentID int not null primary key,
StudentName varchar(30) not null,
StudentSex enum('m','f') default 'm'
)engine=myisam;
MYSQL實(shí)例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實(shí)例/*插入記錄*/
insert into Student values(1235412,'java','m');
insert into Student values(3214562,'jiajia','m');
insert into Student values(5441253,'purana','f');
MYSQL實(shí)例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實(shí)例/*創(chuàng)建觸發(fā)器*/
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實(shí)例create trigger tddStudent before delete
on Student for each row
begin
delete
from BorrowStudent
where BorrowStudent.StudentID=old.StudentID;
end$$
delimiter ;
《Mysql學(xué)習(xí)mysql觸發(fā)器的三個(gè)例子》是否對您有啟發(fā),歡迎查看更多與《Mysql學(xué)習(xí)mysql觸發(fā)器的三個(gè)例子》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/14341.html