1

Is there a way to create a trigger in mysql without hardcoding the database name?

Doing a typical create trigger will stop working if I dump the database and then import it into a database of a different name.

2 Answers 2

1

I guess I'll guess an answer to my question. A lot of mysql documentation just references a table name without specifying a database, so I am assuming the database is optional and if you don't include it, mysql will use the current one.

1
  • 2
    This is the default behavior on most database systems I've encountered: "If the database name / schema namespace is not specified, assume the current one."
    – voretaq7
    Commented Dec 10, 2013 at 22:31
0

If you mysqldump a table with

mysqldump --triggers ...

the code for the trigger definition is included.

However, there is a hardcoding situation you want to be cognizant of

DELIMITER $$

CREATE TRIGGER trigger1 AFTER UPDATE ON mytable FOR EACH ROW 
BEGIN 

  insert into outsidedb.audir_trail values (...);

END $$

DELIMITER ;

If you mysqldump mytable and import it into another DB Server, the trigger works only if and only if the database outsidedb exists on the new DB Server and has the table called audit_trail.

Therefore, please keep aware of all hardcoding situations inside and outside of your triggers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.