nicholaides
nicholaides

Reputation: 19489

Create trigger only if it doesn't exist on MySQL

I'm writing a script to create a trigger on a table in MySQL, but there's a possibility that the trigger might already exist.

Is this something I need to worry about, or will creating the trigger overwrite any trigger with the same name?

I considered using a DROP TRIGGER IF EXISTS statement before the CREATE TRIGGER.... Will that have any performance penalties or other downsides?

Upvotes: 1

Views: 753

Answers (1)

RichardTheKiwi
RichardTheKiwi

Reputation: 107706

Using DROP TRIGGER IF EXISTS is exactly the way to do it. The only downside is if you did not know about a trigger, and it has exactly the same name (possible if following naming conventions), then you would effectively lose the definition and functionality behind it.

will creating the trigger overwrite any trigger with the same name?

No, it will fail with an error, actually.

Upvotes: 4

Related Questions