Error
Error Code: 1304

MariaDB Error 1304: Stored Program Already Exists

📦 MariaDB
📋

Description

This error indicates an attempt to create a stored procedure, function, trigger, or event using a name that is already in use within the current database. It commonly occurs during schema deployments or when re-executing DDL scripts.
💬

Error Message

`%s %s already exists`
🔍

Known Causes

3 known causes
⚠️
Duplicate Creation Attempt
You are trying to create a stored program (procedure, function, trigger, or event) with a name that already exists in the database.
⚠️
Incorrect Database Context
The stored program might exist in a different database, leading to an attempt to create a duplicate in the currently selected database.
⚠️
Flawed Deployment Script
A schema migration or deployment script is attempting to create objects without proper checks for their prior existence or without dropping them first.
🛠️

Solutions

3 solutions available

1. Drop and Recreate the Stored Program easy

Remove the existing stored program and then create it anew.

1
Identify the type of stored program (e.g., PROCEDURE, FUNCTION, TRIGGER) and its name from the error message. The error message format is `%s %s already exists`, where the first `%s` is the type and the second is the name.
2
Use the `DROP` statement to remove the existing stored program. Replace `program_type` with `PROCEDURE`, `FUNCTION`, or `TRIGGER` and `program_name` with the actual name.
DROP PROGRAM_TYPE program_name;
3
Execute your `CREATE` statement again to recreate the stored program.
CREATE PROGRAM_TYPE program_name (...);

2. Check for Existing Stored Program Before Creating medium

Conditionally create the stored program only if it doesn't already exist.

1
Before executing your `CREATE` statement, use `SHOW CREATE` to check if the stored program already exists. Replace `program_type` and `program_name` accordingly.
SHOW CREATE PROGRAM_TYPE program_name;
2
If `SHOW CREATE` returns a result, the program exists. You can either manually drop it (as in Solution 1) or modify your script to conditionally create it.
3
To conditionally create, you can use `CREATE EVENT IF NOT EXISTS`, `CREATE FUNCTION IF NOT EXISTS`, `CREATE PROCEDURE IF NOT EXISTS`, or `CREATE TRIGGER IF NOT EXISTS` (available in newer MariaDB versions).
CREATE PROGRAM_TYPE IF NOT EXISTS program_name (...) BEGIN -- your code END;

3. Review Script Logic for Duplicate Creation Attempts medium

Examine the script that is attempting to create the stored program to find and remove redundant creation commands.

1
Locate the script or application code that is executing the `CREATE` statement for the stored program.
2
Search for multiple instances of the `CREATE PROGRAM_TYPE program_name` statement within the script.
3
Remove or comment out any duplicate `CREATE` statements. Ensure that the `CREATE` statement is executed only once.
4
Consider adding logic to your script to check for existence before creating, as described in Solution 2, to make it more robust.
🔗

Related Errors

5 related errors