1050
MariaDBERRORCommonSchemaHIGH confidence

Table already exists

What this means

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.

Why it happens
  1. 1Running a CREATE TABLE migration that was already executed on this environment
  2. 2A migration tool executed the CREATE TABLE twice
  3. 3The table name collides with a system or reserved table name
How to reproduce

CREATE TABLE is called for an existing table without IF NOT EXISTS.

trigger — this will error
trigger — this will error
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.

Use IF NOT EXISTS
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.

What not to do

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.

Version notes
All versions

IF NOT EXISTS is available in all supported MariaDB and MySQL versions.

Sources
Official documentation ↗

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

← All MariaDB errors