目的:
当一张表中的数据达到一定条数时,让其自动删除本表固定条数的数据。
代码:
--建表(就以student表为例)
create table student
(
id int auto_increment primary key,
name VARCHAR(20),
age int
)
--触发器
CREATE TRIGGER tri_student
AFTER INSERT
ON student
FOR EACH ROW
BEGIN
declare num INT;
select count(id) INTO num from student;
if num>=20 then
DELETE FROM student WHERE ID>10;
end if;
END;
--测试代码
insert into student(name,age) VALUES('aaa','22');
--错误提示
[SQL] insert into student(name,age) VALUES('aaa','22');
[Err] 1442 - Can't update table 'student' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
雪舞蔷薇
weenhall