How to check if table exists in MySql db?

MySQL Trigger check if value exists in table of another DB

  • I need to create a trigger that before insert will check if the username that's trying to be added, already exists (case sensitive) in the table of another database (same mysql server). This is what I have so far: CREATE TRIGGER trig_user_insert BEFORE INSERT ON users FOR EACH ROW BEGIN IF ( ) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error message'; END IF; END So, how can I make that only if it exists in the table of the other database then insert the row. and is there any better way to stop the insert other than SIGNAL?

  • Answer:

    Try: CREATE TRIGGER tg_bi_users BEFORE INSERT ON users FOR EACH ROW SET NEW.user = IF(EXISTS ( SELECT * FROM othertable WHERE user IN(NEW.user) ), NULL, NEW.user);

Balto at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.