MySql插入触发器报错!!!

yaozhiyong110 2013-01-04 11:05:59
目的就是让表里相同id只有一条数据(最后一条) 所以我建了个触发器 插入之前先删除已经存在的id的数据

CREATE TRIGGER `autoDeleteOld` BEFORE INSERT ON `lastscore` FOR EACH ROW delete from

lastscore where id = NEW.id


执行的时候报错: 1442 Can't update table 'lastscore' in stored function/trigger because

it is already used by statement which invoked this stored function/trigger

求救......
...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rucypli 2013-01-04
  • 打赏
  • 举报
回复
根本不用触发器啊 如果id不是主键 则先把id设成主键或者唯一键 然后直接用replace into代替insert into语句即可
yaozhiyong110 2013-01-04
  • 打赏
  • 举报
回复
rucypli 2013-01-04
  • 打赏
  • 举报
回复
百度一下很多 zz 查官网是这么回答的: Old question, but in case anyone finds this: MySQL triggers can't manipulate the table they are assigned to. All other major DBMS support this feature so hopefully MySQL will add this support soon. answered Feb 22 '10 at 21:16
yaozhiyong110 2013-01-04
  • 打赏
  • 举报
回复
引用 1 楼 rucypli 的回复:
根本不用触发器啊 如果id不是主键 则先把id设成主键或者唯一键 然后直接用replace into代替insert into语句即可
设置主键和唯一是可以用replace代替insert 可是为什么我那个触发器不行呢?

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧