Error
Error Code:
1359
MySQL Error 1359: Trigger Already Exists
Description
Error 1359 in MySQL indicates that you are attempting to create a database trigger with a name that is already assigned to an existing trigger. This typically occurs when executing a `CREATE TRIGGER` statement for a trigger name that is not unique within its scope on the specified table.
Error Message
Trigger already exists
Known Causes
3 known causesDuplicate Trigger Creation Attempt
You might have accidentally executed a `CREATE TRIGGER` statement more than once, attempting to create the same trigger multiple times.
Trigger Name Conflict
Another trigger with the exact same name already exists on the database table where you are trying to create the new trigger.
Flawed Deployment Script
Automated deployment or migration scripts may attempt to create triggers without first checking if they already exist, leading to re-creation errors.
Solutions
3 solutions available1. Drop and Recreate the Trigger easy
Remove the existing trigger and then create it anew.
1
Identify the exact name of the trigger that is causing the error.
2
Use the `DROP TRIGGER` statement to remove the existing trigger.
DROP TRIGGER IF EXISTS trigger_name;
3
Execute the `CREATE TRIGGER` statement again with your trigger definition.
CREATE TRIGGER trigger_name
BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
-- Your trigger logic here
END;
2. Check for Duplicates Before Creation medium
Query the information schema to see if the trigger already exists before attempting creation.
1
Before executing your `CREATE TRIGGER` statement, run a query to check if a trigger with the same name and for the same event/table already exists.
SELECT TRIGGER_NAME
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA = DATABASE()
AND TRIGGER_NAME = 'your_trigger_name'
AND EVENT_MANIPULATION = 'INSERT' -- Or UPDATE, DELETE, etc.
AND EVENT_OBJECT_TABLE = 'your_table';
2
If the query returns a row, the trigger already exists. You can then choose to drop and recreate it (Solution 1) or skip the creation if it's not intended to be modified.
3
Alternatively, you can incorporate this check into a script that conditionally creates the trigger.
IF NOT EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA = DATABASE()
AND TRIGGER_NAME = 'your_trigger_name'
) THEN
CREATE TRIGGER your_trigger_name
BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
-- Your trigger logic here
END;
END IF;
3. Verify Trigger Definition in Schema medium
Inspect the existing trigger's definition to ensure it's what you intend.
1
Use the `SHOW TRIGGERS` statement to list all triggers in your current database.
SHOW TRIGGERS;
2
Look for the trigger that is causing the error by its name. Examine its `Statement` column to see its current definition.
3
Compare this definition with the one you are trying to create. If they are identical and the trigger is functioning as expected, you might be encountering the error due to a script that is being run multiple times. In this case, consider using `DROP TRIGGER IF EXISTS` before creating.
4
If the existing trigger's definition is different from what you intend, you will need to drop it and recreate it with the correct definition.
DROP TRIGGER IF EXISTS trigger_name;
CREATE TRIGGER trigger_name ...