MySQL 在删除插入 if 条件的插入触发器之前
MySQL before insert trigger that drops insert if condition
我有以下表格:
slds:
+-----------+-----------+
| id | sld_name |
+-----------+-----------+
| 1 | google |
+-----------+-----------+
| 2 | github |
+-----------+-----------+
路径:
+-----------+------------+----------+
| id | path_name | sld_id |
+-----------+------------+----------+
| 101 | /cats/ | 1 |
+-----------+------------+----------+
| 102 | /dogs/ | 2 |
+-----------+------------+----------+
注意sld_name
是唯一索引,sld_id
是slds
的外键。
INSERT IGNORE INTO paths (path_name, sld_id)
VALUES ('/dogs/', 1), ('/dogs/', 2) ... this can be hundreds of rows long
当上面的示例发生时,我需要一种方法来 remove/prevent 插入额外的 ('/dogs/', 2)
行,因为该路径已经存在于 sld_id=2
的位置,并且不阻止 ('/dogs/', 1)
行,因为 sld_id=1
还没有 /dogs/
路径。
为了实现这个,我尝试使用这个触发器:
delimiter $$
create trigger after_insert_paths
after insert on paths
for each row begin
declare path_check INT;
set path_check := (
select sld_id
from paths
where path_name=new.path_name and sld_id=new.sld_id
);
if path_check is not null then
set new.path_name = null;
end if;
end $$
delimiter ;
所有这一切都是为了防止插入发生。
触发器有什么具体的问题吗?还是这种策略通常不起作用?
是否有我缺少的更好的方法?
如有任何建议,我们将不胜感激! :)
您可以简单地在 paths(path_name, sld_id)
上创建一个 UNIQUE
密钥:
ALTER TABLE paths ADD UNIQUE paths_idx(path_name, sld_id);
现在,当尝试插入重复项时,MySQL 将引发错误,您可以使用 ON DUPLICATE KEY UPDATE
feature:
来处理该错误
INSERT INTO paths (path_name, sld_id) VALUES ('/dogs/', 1)
ON DUPLICATE KEY UPDATE sld_id = sld_id;
ON DUPLICATE KEY
比 IGNORE
更安全,因为它只捕获重复键错误,而 IGNORE
基本上将 any 错误变成警告(数据类型错误,NULL
非可空列中的值,...)。
如果您向 table 添加了主键和外键关系,并且还向 table 添加了唯一约束。在 insert
之后删除 ignore
。
然后 foreign key and unique key
关系将帮助您停止添加重复项。希望它能帮助你。谢谢
我有以下表格:
slds:
+-----------+-----------+
| id | sld_name |
+-----------+-----------+
| 1 | google |
+-----------+-----------+
| 2 | github |
+-----------+-----------+
路径:
+-----------+------------+----------+
| id | path_name | sld_id |
+-----------+------------+----------+
| 101 | /cats/ | 1 |
+-----------+------------+----------+
| 102 | /dogs/ | 2 |
+-----------+------------+----------+
注意sld_name
是唯一索引,sld_id
是slds
的外键。
INSERT IGNORE INTO paths (path_name, sld_id)
VALUES ('/dogs/', 1), ('/dogs/', 2) ... this can be hundreds of rows long
当上面的示例发生时,我需要一种方法来 remove/prevent 插入额外的 ('/dogs/', 2)
行,因为该路径已经存在于 sld_id=2
的位置,并且不阻止 ('/dogs/', 1)
行,因为 sld_id=1
还没有 /dogs/
路径。
为了实现这个,我尝试使用这个触发器:
delimiter $$
create trigger after_insert_paths
after insert on paths
for each row begin
declare path_check INT;
set path_check := (
select sld_id
from paths
where path_name=new.path_name and sld_id=new.sld_id
);
if path_check is not null then
set new.path_name = null;
end if;
end $$
delimiter ;
所有这一切都是为了防止插入发生。
触发器有什么具体的问题吗?还是这种策略通常不起作用?
是否有我缺少的更好的方法?
如有任何建议,我们将不胜感激! :)
您可以简单地在 paths(path_name, sld_id)
上创建一个 UNIQUE
密钥:
ALTER TABLE paths ADD UNIQUE paths_idx(path_name, sld_id);
现在,当尝试插入重复项时,MySQL 将引发错误,您可以使用 ON DUPLICATE KEY UPDATE
feature:
INSERT INTO paths (path_name, sld_id) VALUES ('/dogs/', 1)
ON DUPLICATE KEY UPDATE sld_id = sld_id;
ON DUPLICATE KEY
比 IGNORE
更安全,因为它只捕获重复键错误,而 IGNORE
基本上将 any 错误变成警告(数据类型错误,NULL
非可空列中的值,...)。
如果您向 table 添加了主键和外键关系,并且还向 table 添加了唯一约束。在 insert
之后删除 ignore
。
然后 foreign key and unique key
关系将帮助您停止添加重复项。希望它能帮助你。谢谢