I am to creating a trigger for my table User which checks for duplicates (Mobile number) in the User table before inserting a new row.
My User table structure is:
USERID | FirstName | LastName | EmailID | Mobile
I am using the below code to create a trigger:
DELIMITER $$
CREATE TRIGGER Before_Insert_User
BEFORE INSERT ON User
FOR EACH ROW
BEGIN
IF (NOT EXISTS(SELECT * FROM User WHERE Mobile = NEW.Mobile)) THEN
INSERT INTO User (USERID, FirstName, LastName, EmailID, Mobile,)
VALUES (NEW.USERID, NEW.FirstName, NEW.LastName, NEW.EmailID, NEW.Mobile);
END IF;
END$$
DELIMITER ;
But this trigger is giving me an error as below while inserting new records:
Cannot update the user information (Can't update table 'User' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.)
I am not getting where I am doing a mistake.
How can I write a trigger for checking if the value being inserted is already present in User table?