<p>MySQL raises ER_TABLESPACE_EXISTS (error 1813) when you attempt to create a tablespace that already exists in the data directory.</p>
<p>MySQL Error 1813: ER_TABLESPACE_EXISTS appears when a CREATE TABLESPACE or ALTER TABLE ... TABLESPACE statement names a tablespace that is already present. Confirm its presence in information_schema.FILES, then either DROP TABLESPACE, use IF NOT EXISTS, or choose a new name to resolve the conflict.</p>
Tablespace '%s' exists.
Error 1813 fires when MySQL detects that a tablespace file with the specified name already exists in the server's data directory or file-per-table path. The server blocks the CREATE TABLESPACE or ALTER TABLE operation to prevent data corruption.
Because InnoDB treats a tablespace as a physical .ibd or .ibdata file, naming collisions can damage storage metadata. Fixing the issue quickly keeps DDL operations predictable and protects data integrity.
The error surfaces during CREATE TABLESPACE, CREATE UNDO TABLESPACE, ALTER TABLE ... TABLESPACE, or CREATE TABLE statements that implicitly create a file-per-table tablespace. It also appears after incomplete DROP TABLESPACE attempts that leave orphaned files on disk.
Developers often see the error after schema migrations, restore scripts, or automation reruns where the same tablespace name is reused without cleanup.
A pre-existing .ibd or .ibdata file with the same tablespace name is the primary trigger. This can result from aborted DDL, failed backups, or manual file copies.
Configuration mismatches like duplicate innodb_data_file_path entries and permission issues that prevent cleanup can also leave ghost tablespaces behind.
First, verify that the tablespace truly exists by querying information_schema.FILES or inspecting the data directory. If it is not needed, run DROP TABLESPACE. If it is required, adjust your DDL to reference or rename it.
Using IF NOT EXISTS in your CREATE TABLESPACE statement or creating an alternative name eliminates the collision. Always back up critical data before dropping or renaming any tablespace.
CI/CD pipelines rerunning the same migration should wrap CREATE TABLESPACE in IF NOT EXISTS to stay idempotent. Restore scripts that copy .ibd files must run DISCARD TABLESPACE before an import to avoid duplicates.
When cloning databases, ensure the data directory is empty or use different innodb_file_per_table settings to keep names unique.
Adopt naming conventions that include environment or timestamp suffixes. Automate cleanup with DROP TABLESPACE during rollbacks. Monitor information_schema.FILES for orphaned entries and alert on duplicates.
Galaxy users can run guarded DDL through the editor, share audited migrations, and rely on version history to spot accidental duplicates before deployment.
Errors 1814 (ER_TABLESPACE_MISSING) and 1007 (ER_DB_CREATE_EXISTS) also signal object collisions. Understanding their causes helps build resilient deployment scripts.
A migration script ran twice without IF NOT EXISTS, leaving the first tablespace intact.
An earlier DROP failed due to foreign key constraints, leaving the physical file behind.
Copying .ibd files between servers without DISCARD TABLESPACE created conflicting names.
A restore process recreated the tablespace while the original still existed.
Raised when a referenced tablespace cannot be found on disk. Opposite of ER_TABLESPACE_EXISTS.
Occurs when trying to create a database that already exists. Similar name collision but at the schema level.
Thrown when creating a duplicate index name within a table.
No. Even empty tablespace files register in InnoDB metadata. Remove or rename them before proceeding.
Yes, IF NOT EXISTS instructs MySQL to skip creation when a tablespace with the same name is already present.
For file-per-table InnoDB tables, DROP TABLE deletes the .ibd file, but a failed drop can leave remnants that trigger ER_TABLESPACE_EXISTS.
Galaxy's versioned queries and endorsements let teams reuse safe DDL blocks and surface duplicate tablespace names during code review.