如果 table 中已经存在字符串,如何设置触发器?
How to set trigger if string is already exist in table?
例如,我正在尝试创建一个触发器,如果 table 中已经存在一个字符串,则将数据值插入到旧的现有值中。
CREATE TRIGGER t
BEFORE INSERT
ON PARTS
WHEN NEW.Part_name IN(SELECT Part_name FROM PARTS)
BEGIN
...
...
...
END;
如果 Part_name 类型是整数,这将起作用。但是我想问一下,如果 Part_name 类型是字符串,我该如何使用这个触发器?
在 mysql 中,您可能希望使用 if exists。例如
delimiter $$
CREATE TRIGGER t
BEFORE INSERT
ON PARTS
for each row
BEGIN
if exists (SELECT Part_name FROM PARTS where part_name = new.part_name) then
##do stuff
end if;
END $$
delimiter ;
例如,我正在尝试创建一个触发器,如果 table 中已经存在一个字符串,则将数据值插入到旧的现有值中。
CREATE TRIGGER t
BEFORE INSERT
ON PARTS
WHEN NEW.Part_name IN(SELECT Part_name FROM PARTS)
BEGIN
...
...
...
END;
如果 Part_name 类型是整数,这将起作用。但是我想问一下,如果 Part_name 类型是字符串,我该如何使用这个触发器?
在 mysql 中,您可能希望使用 if exists。例如
delimiter $$
CREATE TRIGGER t
BEFORE INSERT
ON PARTS
for each row
BEGIN
if exists (SELECT Part_name FROM PARTS where part_name = new.part_name) then
##do stuff
end if;
END $$
delimiter ;