注册 登录
编程论坛 MySQL论坛

[分享]触发器小例子

purana 发布于 2007-09-10 09:45, 8147 次点击
触发器能进行一些约束.
这是个小例子,当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 ;
8 回复
#2
静思2007-09-10 12:19

以前对触发器不太明白,也不太经常用,看了版主这个帖子之后,感觉自己明白了点...
下面是我写的一个例子,不知道是不是这样写的,要完成的功能是:当表中a,b 两列的值同时为1的时候 同表中的另一列c 的值 自动设成1


CREATE TRIGGER tr_TABLE_Update ON TABLE
FOR UPDATE
AS
BEGIN
DECLARE @A INT
,@B INT
,@ID INT
SELECT @ID=ID, @A=A, @B=B FROM INSERTED
IF @A=@B=1
BEGIN
UPDATE TABLE SET C=1 WHERE ID = @ID
END
END



[此贴子已经被作者于2007-9-10 12:20:24编辑过]

#3
静思2007-09-10 12:31
上面是sql server中的写法,mysql中怎么写 呢?
#4
purana2007-09-10 14:31
你为Table添加触发器.
又用update来更新同一Table?
mysql的触发器不行.
因为mysql的触发器.还是不怎么成熟的.

如果你想这么做.那只有用set new.field_name=value 这样的来做.
#5
purana2007-09-10 14:46
按照你的要求,写了一个.
drop table if exists tbl_temp;
create table tbl_temp(
id int not null auto_increment key,
field_a int,
field_b int,
field_c int
)engine=myisam;

insert into tbl_temp(field_a,field_b,field_c) values(2,3,2);
insert into tbl_temp(field_a,field_b,field_c) values(1,2,3);
insert into tbl_temp(field_a,field_b,field_c) values(0,0,0);

delimiter $$
drop trigger if exists tr_set$$
create trigger tr_set before update
on tbl_temp for each row
begin
if new.field_a=1&&new.field_b=1 then
set new.field_c=1;
end if;
end$$
delimiter ;
#6
静思2007-09-10 15:32
恩,不错,以后应该用点触发器...
斑竹我还有一个问题:比如我们在建表时经常要用到check约束,但Mysql的视图却不支持CHECK约束,但我们能不能用触发器实现下列表中的CHECK约束呢?
CREATE TABLE t25 (s1 INT, s2 CHAR(5), PRIMARY KEY (s1), CHECK (LEFT(s2,1)='A')) ENGINE=INNODB;
这里CHECK的意思是"当s2列的最左边的字符不是'A'时,insert和update语句都会非法"。
#7
purana2007-09-10 16:24
视图?.
但你的是创建表哦?.
mysql也可以check.只不过基本是没用的.

这种情况.那就在客户端里判断了.
#8
静思2007-09-10 16:48

我想用触发器实现,我刚才试着写了一个,在DOS环境下一运行就卡住了,是不是有bug呀?而在mysqlcc中验证此触发器的功能时可以达到要求,不知道是什么原因?
delimiter $$
create trigger t25_bi before insert
on t25 for each row
begin
if left(new.s2,1)<>'a' then set new.s1=0;
end if;
end$$
delimiter ;
我建立的表如下:
create table t25(s1 int,s2 char(5))engine=innodb;

[此贴子已经被作者于2007-9-10 16:56:52编辑过]

#9
purana2007-09-10 17:52
我这里可以.
1