Table already exists
Error 1050 (SQLSTATE 42S01) is raised when a CREATE TABLE statement tries to create a table with a name that already exists in the current database. This commonly occurs during application startup migrations that do not guard with IF NOT EXISTS.
- 1Running a CREATE TABLE migration that was already executed on this environment
- 2A migration tool executed the CREATE TABLE twice
- 3The table name collides with a system or reserved table name
CREATE TABLE is called for an existing table without IF NOT EXISTS.
CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(255) NOT NULL ); -- Running it again: CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(255) NOT NULL);
expected output
ERROR 1050 (42S01): Table 'users' already exists
Fix
Use IF NOT EXISTS
WHEN In idempotent migrations that may run multiple times.
CREATE TABLE IF NOT EXISTS users ( id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(255) NOT NULL );
Why this works
IF NOT EXISTS causes the server to silently skip the CREATE if the table already exists, making the statement idempotent. No warning is raised in recent versions.
✕ Use DROP TABLE before every CREATE TABLE in migrations
DROP + CREATE deletes all existing data. In a production migration this would cause catastrophic data loss.
IF NOT EXISTS is available in all supported MariaDB and MySQL versions.
MariaDB Server error code 1050 / ER_TABLE_EXISTS_ERROR
MariaDB CREATE TABLE ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev