mysql初学者,有一个表dd,想把dd表的记录数控制在10条之内,大于10的话,把前面的记录删除,我意思是做一个这样的触发器:
CREATE TRIGGER dd1
AFTER INSERT ON dd
FOR EACH ROW
if (select count(*) from dd > 10) then (delete from dd limit 2)//实现这个功能的语句不懂写,请大家帮帮忙!谢谢!
CREATE TRIGGER dd1
AFTER INSERT ON dd
FOR EACH ROW
if (select count(*) from dd > 10) then (delete from dd limit 2)//实现这个功能的语句不懂写,请大家帮帮忙!谢谢!
declare cc int;
select count(*) into cc from dd where 1;
if cc>10 then
delete from dd limit 2;
end if触发器的声明和结束楼主自己加上把。不会的话,查查手册。
create trigger dd1
after insert on dd
for each row
declare cc int;
select count(*) into cc from dd where 1;
if cc>10 then
delete from dd limit 2;
end if
但出现如下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'declare cc int;
select count(*) into cc from dd where 1;
if cc>10 then
dele' at line 4能不能给个能跑的例子啊?
CREATE TRIGGER dd1
AFTER INSERT ON dd
FOR EACH ROW
BEGIN
DECLARE cc int;
select count(*) into cc from dd;
if cc>10 then
delete from dd limit 1;
end if;
END;
但有以下问题,当dd表不足10条记录时,没有出现问题,当然触发器也不产生作用,但当dd表到达10条记录时,再向dd表插入记录的时候,却出现如下问题:
Can't update table 'dd' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
它不让我对dd表插入记录了,这是为什么呢?
这一句放在存贮过程中来调用
我把delete from dd limit 1;改为call delRecord()也是出现同样的错误提醒啊。
delRecord()存贮过程的内容就是delete from dd limit 1;