I have a DB that I have cloned, and now all the logging triggers point to log tables in the original schema. I need to delete all of them in one go (there are several dozen) so that I can recreate them. How can this be done with one command?
Execute this script: USE YourDBName GO SELECT ' GO ' + Char(10) + Char(13) + 'DROP TRIGGER ' + QUOTENAME(OBJECT_SCHEMA_NAME(O. [object_id])) + '. ' + QUOTENAME(name) FROM sys.
You can remove triggers using the Remove Physical File Trigger (RMVPFTRG) command, the SQL DROP TRIGGER statement, or System i® Navigator. Use the RMVPFTRG command to remove the association of a file and the trigger program.
A single SQL statement can potentially fire up to four types of triggers: BEFORE row triggers. BEFORE statement triggers.
To destroy the trigger, use a DROP TRIGGER statement. You must specify the schema name if the trigger is not in the default schema: mysql> DROP TRIGGER test.
SELECT Concat('DROP TRIGGER ', Trigger_Name, ';') FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA = 'your_schema';
Copy and paste the generated sql
This is an old question, but since it's the one that keeps popping up in my searches I thought I'd post a solution here. In my case I needed to create a single file that had a full mysqldump
, would then drop any triggers, and then re-add them all. I was able to do it by using the following command to append the DROP TRIGGER
statements to my dump prior to appending the dump of the triggers.
mysql -u [db user] -p[db password] --skip-column-names [db name] -e 'SHOW TRIGGERS;' | cut -f1 | sed -r 's/(.*)/DROP TRIGGER IF EXISTS \1;/' >> dump.sql
To actually drop all the triggers in the same command (as mentioned by @Stephen Crosby in the comments), you can just pipe this back into MySQL like so:
mysql -u [db user] -p[db password] --skip-column-names [db name] -e 'SHOW TRIGGERS;' | cut -f1 | sed -r 's/(.*)/DROP TRIGGER IF EXISTS \1;/' | mysql -u [db user] -p[db password] [db name]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With