As of MySQL 5.0 there is no SHOW CREATE TRIGGER which is pretty annoying. I don’t like using “show triggers like
“, so I figured I’d write a wrapper around mysqldump. For the record, this was a huge pain the ass. I did this on Fedora, GNU sed version 4.1.5. You need to have your password stored in ~./my.cnf for this to work, I believe. I haven’t tried it without it.


mysqldump -dnt database | sed "/\/\*\!4.*\*\/;*/d" | sed "s_/\*\!5[0-9]*__g" | sed "s_DEFINER=.*\*/__g" | sed "s/\*\/;*//g" | sed "/SET/d" | sed "s/^[ \t]*//" | sed "s/\ \ */ /g" | sed "/--/d"

You will get a dump of ALL the triggers in your database, using this. However, since that’s what I need on this particular example, it’s what I’m sharing. I’m sure I could have cleaned up the code a little and used fewer calls to sed, but frankly I don’t mind, since it works fine.

I hope this helps someone.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
 

4 Responses to SHOW CREATE TRIGGER – Nope

  1. Basi says:

    And what about using INFORMATION_SCHEMA?

    For example, all the triggers from the server:

    mysql> SELECT * FROM TRIGGERS\G
    *************************** 1. row ***************************
    TRIGGER_CATALOG: NULL
    TRIGGER_SCHEMA: control_panel
    TRIGGER_NAME: class_test_update
    EVENT_MANIPULATION: UPDATE
    EVENT_OBJECT_CATALOG: NULL
    EVENT_OBJECT_SCHEMA: control_panel
    EVENT_OBJECT_TABLE: class_test
    ACTION_ORDER: 0
    ACTION_CONDITION: NULL
    ACTION_STATEMENT: BEGIN
    INSERT INTO `historics`.`historic_class_test` ( `id_class_test`, `OLD_varchar_`, `NEW_varchar_`, `OLD_varchar_req`, `NEW_varchar_req`, `OLD_encrypted_`, `NEW_encrypted_`, `user_update`, `ip_update` ) VALUES (OLD.id_class_test, OLD.varchar_, NEW.varchar_, OLD.varchar_req, NEW.varchar_req, OLD.encrypted_, NEW.encrypted_, @REMOTE_USER, @REMOTE_ADDR );
    END
    ACTION_ORIENTATION: ROW
    ACTION_TIMING: AFTER
    ACTION_REFERENCE_OLD_TABLE: NULL
    ACTION_REFERENCE_NEW_TABLE: NULL
    ACTION_REFERENCE_OLD_ROW: OLD
    ACTION_REFERENCE_NEW_ROW: NEW
    CREATED: NULL
    SQL_MODE: NO_FIELD_OPTIONS,MYSQL40,HIGH_NOT_PRECEDENCE
    DEFINER: powfull@%
    *************************** 2. row ***************************
    TRIGGER_CATALOG: NULL
    TRIGGER_SCHEMA: control_panel
    TRIGGER_NAME: class_test_delete
    EVENT_MANIPULATION: DELETE
    EVENT_OBJECT_CATALOG: NULL
    EVENT_OBJECT_SCHEMA: control_panel
    EVENT_OBJECT_TABLE: class_test
    ACTION_ORDER: 0
    ACTION_CONDITION: NULL
    ACTION_STATEMENT: BEGIN
    INSERT INTO `historics`.`historic_class_test` ( `type`, `id_class_test`, `OLD_varchar_`, `NEW_varchar_`, `OLD_varchar_req`, `NEW_varchar_req`, `OLD_encrypted_`, `NEW_encrypted_`, `user_update`, `ip_update` ) VALUES ( ‘DELETE’, OLD.id_class_test, OLD.varchar_, NULL, OLD.varchar_req, NULL, OLD.encrypted_, NULL, @REMOTE_USER, @REMOTE_ADDR );
    END
    ACTION_ORIENTATION: ROW
    ACTION_TIMING: AFTER
    ACTION_REFERENCE_OLD_TABLE: NULL
    ACTION_REFERENCE_NEW_TABLE: NULL
    ACTION_REFERENCE_OLD_ROW: OLD
    ACTION_REFERENCE_NEW_ROW: NEW
    CREATED: NULL
    SQL_MODE: NO_FIELD_OPTIONS,MYSQL40,HIGH_NOT_PRECEDENCE
    DEFINER: powfull@%
    2 rows in set (1.61 sec)

    information_schema, is the meta-database, and it supports SQL queries like another database…

  2. jon says:

    Hi Basi,

    Selecting from the information schema has the same problems as using show triggers like “table”, in that it doesn’t generate for me SQL that I can modify and easily use to modify the trigger.

    I’m lazy, is what it boils down to. Using mysqldump and the script I wrote above, you can easily modify any trigger in your database within seconds. The equivilent of “show create trigger”, except with every trigger.

  3. Brian Aker says:

    Hi!

    This is a bug. I know about it, and its on our list of things to fix. Yeah, I am sure you are thinking “it looks like a feature request”, but no, it is a bug :)

    Cheers,
    -Brian

  4. Scott Noyes says:

    I cobbled this up for the 5.0 to 5.1.20 crowd. As of 5.1.21, there is a built in SHOW CREATE TRIGGER syntax.

    http://forge.mysql.com/snippets/view.php?id=103

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>