I have a table that has a the following columns:
ID(primary key),
USER,
ACTION
TIME
LOCATION
I am looking to use a trigger or something that runs when the table is updated to insure that there are no duplicate entries based on the grouping of USER, ACTION, TIME, LOCATION. A preavious question was answered for me about how to clear the existing entries which resulted in:
DELETE t1.*
FROM
testlogins t1 INNER JOIN testlogins t2
ON t1.user=t2.user
AND t1.action=t2.action
AND t1.time=t2.time
AND t2.location=t2.location
AND t1.id>t2.id;
This worked to clear the existing records. I thought i might be able to create a trigger based of this and I came up with this:
DELIMITER $$
create trigger del_duplicates
after insert
on test.testlogins for each row
begin
DELETE t1.*
FROM
testlogins t1 INNER JOIN testlogins t2
ON t1.user=t2.user
AND t1.action=t2.action
AND t1.time=t2.time
AND t2.location=t2.location
AND t1.id>t2.id;
end
$$
However that returns:
Error Code: 1442. Can't update table 'testlogins' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I assume this is because I am asking the system to do a delete after a insert. I have also wondered if it would be worth while calling a stored procedure during a trigger to accomplish this.
What would be the best way to keep duplicate rows from being added that match in these four columns: USER, ACTION, TIME, LOCATION