0

I have a database (database1) and a table imei_db(IMEI VARCHAR(15), db VARCHAR(40)). I also have other databases (ex: database2) and I want a trigger that, whe inserting a row on database1.imei_db, it verifies before the actual insert if there is a database with a name equal to db field value.

For example:

Existing databases: database1, database2

Succeeds: INSERT INTO imei_db VALUES ("111222333444555","database2")

Fails: INSERT INTO imei_db VALUES ("111222333444555","database3")

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
MagisterMundus
  • 335
  • 4
  • 18

1 Answers1

0

This is how I managed to do it:

USE database1;
INSERT INTO `imei_db`(`IMEI`,`db`)
SELECT 111222333444555,"database2" FROM anytable
WHERE (SELECT count(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = 'database2') AND (TABLE_NAME = 'table_expected_to_exist')) = 1
LIMIT 1;
MagisterMundus
  • 335
  • 4
  • 18