MySQL 8.4Lab-verified · 2026-12-01

MySQL ERROR 1050 (42S01): Table already exists

A table or view with that name already exists in the database.

MySQL 8.4.13
1050 (42S01): Table 'logs' already exists

Jump to a cause

  1. 01Running the same CREATE TABLE twice (re-running a migration or setup script)
  2. 02RENAME TABLE onto a name that is still in use
  3. 03A view with the same name already exists
  4. 04A procedure creates a TEMPORARY table and is called twice on the same connection

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-12-01 · container image mysql:8.4

MySQL tables and views share one namespace in a database, so 1050 appears both when a table and when a view already has the name. Check what is there with SHOW FULL TABLES — the Table_type column tells you whether it is a table or a view.

01Running the same CREATE TABLE twice (re-running a migration or setup script)

logs was created by the first run. The second CREATE TABLE logs fails. SHOW TABLES LIKE 'logs' shows it is already there.

Setup
Setuprun as lab_a
CREATE TABLE logs (id INT PRIMARY KEY, msg VARCHAR(100));
OUTPUT
Query OK, 0 rows affected (0.02 sec)
Reproducerun as lab_a
CREATE TABLE logs (id INT PRIMARY KEY, msg VARCHAR(100));
✕ ERROR
ERROR 1050 (42S01): Table 'logs' already exists
Checkrun as lab_a
SHOW TABLES LIKE 'logs';
OUTPUT
+------------------------+
| Tables_in_lab_a (logs) |
+------------------------+
| logs                   |
+------------------------+
1 row in set (0.01 sec)

Use CREATE TABLE IF NOT EXISTS. When the table exists, the statement succeeds with 0 rows affected, 1 warning, and SHOW WARNINGS shows the same 1050 as a Note instead of an error. It does not create anything, so if the existing table has different columns, you still have to ALTER it.

After the fixrun as lab_a
CREATE TABLE IF NOT EXISTS logs (id INT PRIMARY KEY, msg VARCHAR(100));
SHOW WARNINGS;
✓ SUCCESS
Query OK, 0 rows affected, 1 warning (0.00 sec)

+-------+------+-----------------------------+
| Level | Code | Message                     |
+-------+------+-----------------------------+
| Note  | 1050 | Table 'logs' already exists |
+-------+------+-----------------------------+
1 row in set (0.00 sec)

02RENAME TABLE onto a name that is still in use

You built users_new to replace users, but users still exists, so RENAME TABLE users_new TO users fails.

Setup
Setuprun as lab_a
CREATE TABLE users (id INT PRIMARY KEY);
CREATE TABLE users_new (id INT PRIMARY KEY, email VARCHAR(100));
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)
Reproducerun as lab_a
RENAME TABLE users_new TO users;
✕ ERROR
ERROR 1050 (42S01): Table 'users' already exists
Checkrun as lab_a
SHOW TABLES LIKE 'users%';
OUTPUT
+--------------------------+
| Tables_in_lab_a (users%) |
+--------------------------+
| users                    |
| users_new                |
+--------------------------+
2 rows in set (0.00 sec)

Move the old table out of the way and the new one in within one statement: RENAME TABLE users TO users_old, users_new TO users. Afterwards there are users and users_old.

After the fixrun as lab_a
RENAME TABLE users TO users_old, users_new TO users;
SHOW TABLES;
✓ SUCCESS
Query OK, 0 rows affected (0.02 sec)

+-----------------+
| Tables_in_lab_a |
+-----------------+
| users           |
| users_old       |
+-----------------+
2 rows in set (0.00 sec)

03A view with the same name already exists

order_summary is a view, but the error still says Table 'order_summary' already exists. information_schema.tables shows its TABLE_TYPE as VIEW.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, amount INT);
CREATE VIEW order_summary AS SELECT COUNT(*) AS cnt FROM orders;
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)
Reproducerun as lab_a
CREATE TABLE order_summary (cnt INT);
✕ ERROR
ERROR 1050 (42S01): Table 'order_summary' already exists
Checkrun as lab_a
SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = 'lab_a' ORDER BY table_name;
OUTPUT
+---------------+------------+
| TABLE_NAME    | TABLE_TYPE |
+---------------+------------+
| order_summary | VIEW       |
| orders        | BASE TABLE |
+---------------+------------+
2 rows in set (0.00 sec)

Pick a different name for the table (here order_summary_snapshot), or drop the view first if it is no longer used. SHOW FULL TABLES then lists the view and the new BASE TABLE side by side.

After the fixrun as lab_a
CREATE TABLE order_summary_snapshot (cnt INT);
SHOW FULL TABLES;
✓ SUCCESS
Query OK, 0 rows affected (0.02 sec)

+------------------------+------------+
| Tables_in_lab_a        | Table_type |
+------------------------+------------+
| order_summary          | VIEW       |
| order_summary_snapshot | BASE TABLE |
| orders                 | BASE TABLE |
+------------------------+------------+
3 rows in set (0.00 sec)

04A procedure creates a TEMPORARY table and is called twice on the same connection

monthly_report() runs CREATE TEMPORARY TABLE tmp_sales and selects from it. The first CALL worked. The second CALL on the same connection failed with Table 'tmp_sales' already exists. A temporary table lives until the connection closes, not until the procedure ends. With a connection pool, the same connection is reused, so this shows up from the second request on.

Setup
Setuprun as lab_a
CREATE TABLE sales (id INT PRIMARY KEY, item VARCHAR(10), amount INT);
INSERT INTO sales VALUES (1, 'pen', 300), (2, 'note', 500), (3, 'pen', 200);
DELIMITER //
CREATE PROCEDURE monthly_report()
BEGIN
  CREATE TEMPORARY TABLE tmp_sales AS SELECT item, SUM(amount) AS total FROM sales GROUP BY item;
  SELECT item, total FROM tmp_sales ORDER BY item;
END//
DELIMITER ;
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)
Reproducerun as lab_a
CALL monthly_report();
CALL monthly_report();
✕ ERROR
+------+-------+
| item | total |
+------+-------+
| note |   500 |
| pen  |   500 |
+------+-------+
2 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

ERROR 1050 (42S01): Table 'tmp_sales' already exists
Check the causerun as lab_a
CALL monthly_report();
SHOW TABLES LIKE 'tmp_sales';
SELECT COUNT(*) AS rows_in_tmp FROM tmp_sales;
OUTPUT
+------+-------+
| item | total |
+------+-------+
| note |   500 |
| pen  |   500 |
+------+-------+
2 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Empty set (0.00 sec)

+-------------+
| rows_in_tmp |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

After one CALL, SHOW TABLES LIKE 'tmp_sales' returned Empty set (temporary tables are not listed), yet SELECT COUNT(*) FROM tmp_sales returned 2. The table is still there in this session.

Fixrun as lab_a
DROP PROCEDURE monthly_report;
DELIMITER //
CREATE PROCEDURE monthly_report()
BEGIN
  DROP TEMPORARY TABLE IF EXISTS tmp_sales;
  CREATE TEMPORARY TABLE tmp_sales AS SELECT item, SUM(amount) AS total FROM sales GROUP BY item;
  SELECT item, total FROM tmp_sales ORDER BY item;
END//
DELIMITER ;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Recreate the procedure with DROP TEMPORARY TABLE IF EXISTS tmp_sales; as its first statement. Two CALLs in a row then both returned the result.

After the fixrun as lab_a
CALL monthly_report();
CALL monthly_report();
✓ SUCCESS
+------+-------+
| item | total |
+------+-------+
| note |   500 |
| pen  |   500 |
+------+-------+
2 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+------+-------+
| item | total |
+------+-------+
| note |   500 |
| pen  |   500 |
+------+-------+
2 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

FAQ

How do I fix MySQL error 1050 Table already exists?

If the existing table is the one you want, use CREATE TABLE IF NOT EXISTS so a re-run does not fail. If you want to replace it, rename or drop the old table first.

Does CREATE TABLE IF NOT EXISTS update the table structure?

No. If the table exists it only leaves a Note 1050 warning. Change the existing table with ALTER TABLE.

Why does MySQL say table already exists when there is no such table?

A view may use that name. Run SHOW FULL TABLES and check Table_type.

How do I swap two tables in MySQL?

Use one RENAME TABLE statement with two pairs: RENAME TABLE users TO users_old, users_new TO users.

Why does CREATE TEMPORARY TABLE give 1050 on the second call?

A temporary table stays until the connection closes. If a procedure creates it and the pooled connection is reused, the next call finds it still there. Put DROP TEMPORARY TABLE IF EXISTS before the CREATE.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13