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
Related Q & A:
- How To Check Whether An Email Exists?Best solution by Stack Overflow
- How to show a table on another page?Best solution by Stack Overflow
- How to insert data from one table to another?Best solution by Stack Overflow
- How to declare variable in trigger with MySQL?Best solution by Stack Overflow
- How to Migrate from SQLite DB to MySQL?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.