I have a table of lat, lng and deleteflag. Its MariaDB 5.3...
If someone inserts a value "close to existing items" I want it rejected or the deleteflag set.
I thought I could do it with a trigger?? Is this possible in mysql which seems has only limited trigger capabilities??
CREATE TABLE `test` ( `lat` decimal(10,8) NOT NULL, `deleted` decimal(11,0) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TRIGGER mytrigger BEFORE INSERT ON test FOR EACH ROW BEGIN DECLARE distance INT; SELECT COUNT(*) INTO distance FROM test WHERE lat between new.lat-.1 and new.lat+.1; IF distance > 0 THEN UPDATE test SET new.deleted=1; END IF; END
It works. lat 39.2 39.5 39.8 40.0 40.2 40.4 40.6 40.8 41.0
But throws this error on closedups/dups.
#1442 - Can't update table 'test' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Q: How to silence that error??
You almost have it and the restriction is that triggers can't modify the table on which the trigger is has been applied (avoid many cases of cascading infinite recursion failures).
Instead of:
IF distance > 0 THEN
UPDATE test
SET new.deleted=1;
END IF;
Use:
IF distance > 0 THEN
SET new.deleted=1;
END IF;
When the trigger completes the NEW.x
values are used.
Ref; syntax of triggers per manual
Other notes;
deleted
is a decimal.If only a boolean is needed for deletion:
SELECT 1
INTO distance
FROM test
WHERE
lat between new.lat-.1 and new.lat+.1
LIMIT 1
;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.