mysql - 我的触发器插入了许多记录

标签 mysql sql triggers

我有以下触发器:

    begin
 insert into user_has_competence (user_id,competence_id)
 select u.id,c.id from competence c
 join user u on u.organization_id = c.organization_id and c.organization_id = new.organization_id;
end

涉及的表如下:

    Table: competence
Columns:
id  int(11) AI PK
name    varchar(400)
organization_id int(11)
competence_type_id  int(11)
competence_category_id  int(11)


Table: user
Columns:
id  int(11) AI PK
username    varchar(100)
password    varchar(100)
is_active   int(11)
user_type_id    int(11)
token   varchar(445)
organization_id int(11)
title_id    int(11)
image_path  varchar(100)
division_id int(11)

    Table: user_has_competence
Columns:
user_id int(11) PK
competence_id   int(11) PK
competence_level_id int(11)
progression varchar(45)
id  int(11) AI PK

现在我有 8 个用户,当我插入 64 个 competence 时,我最终将超过 30k 行插入到 user_has_competence

谁能告诉我为什么会这样?

更准确地说,它插入了 626433 行。

最佳答案

看起来每个组织的用户只能拥有一种能力?如果是这样,我建议对 user_has_competence 中的 user_id、competent_id、organization_id 表进行复合 PK。 (您需要在那里添加organization_id)。然后,当触发器尝试插入重复值时,您会看到错误

编辑:部分问题可能是您的触发器不包含您刚刚插入的新权限_id 的 where 子句,因此它将为所有现有的用户权限关系添加重复项

关于mysql - 我的触发器插入了许多记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30248798/

相关文章:

mysql - Django mysql 连接太多

mysql - 比较 psql 中的两个日期

sql-server - 触发更新另一个sql server上的数据

mysql - 在 Mysql- PhpMyAdmin 中从它自己的触发器修改表

mysql - 最后排序空值

mysql - 来自不同mysql表的最小值和最大值

sql - 在 SQL Server 2008 中获取 3 个表,避免使用两个循环和动态 SQL

Mysql 如何通过将查询字符串与全文搜索匹配来排序 MATCH AGAINST

mysql - 将两行结果合并为一行 MYSQL

sql-server - 如何获取 SQL Server 中触发器的定义(主体)?