目的:当一张表中的数据达到一定条数时,让其自动删除本表固定条数的数据。代码:--建表(就以student表为例)create table student(id int auto_increment primary key,name VARCHAR(20),age int)--触发器CREATE TRIGGER tri_studentAFTER INSERT ON studentFOR 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.
添加回答
举报
0/150
提交
取消