title: Mysql trigger for checking duplicate record in Table before insert
body: <p>I am to creating a trigger for my table <code>User</code> which checks duplicate (Mobile number) in User table before inserting new row.</p>
  
  <p>My User table structure is:</p>
  
  <pre><code>USERID | FirstName | LastName | EmailID | Mobile
  </code></pre>
  
  <p>I am using the below code to create trigger</p>
  
  <pre><code>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 ;
  </code></pre>
  
  <p>But this trigger is giving me error as below while inserting new records:</p>
  
  <blockquote>
    <p>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.) </p>
  </blockquote>
  
  <p>I am not getting where i am doing mistake.</p>
  
  <p>How can i write a trigger for checking if the value being inserted is already present in User table?</p>