Common SQL Errors

MySQL Error 3193: ER_TABLE_REFERENCED - How to Fix and Prevent

Galaxy Team
August 8, 2025

MySQL cannot complete a DDL statement because another session still references the target table.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 3193 ER_TABLE_REFERENCED?

MySQL error ER_TABLE_REFERENCED (3193) occurs when you attempt to drop, rename, truncate, or alter a table that is currently referenced by another connection or foreign key constraint. End or commit the blocking session, or temporarily disable FOREIGN_KEY_CHECKS, to complete the operation.

Error Highlights

Typical Error Message

ER_TABLE_REFERENCED

Error Type

Constraint Error

Language

MySQL

Symbol

by another connection. ER_TABLE_REFERENCED was added in 5.7.14.

Error Code

3193

SQL State

HY000

Explanation

Table of Contents

What is MySQL error ER_TABLE_REFERENCED (3193)?

MySQL raises error 3193 with message "Cannot complete the operation because table is referenced" when a DDL statement tries to drop, rename, truncate, or alter a table that another connection has locked or that is still referenced by a foreign key in the current transaction.

The server blocks the operation to protect referential integrity. The error appears from MySQL 5.7.14 onward and can surface in interactive sessions, automated migrations, or ALTER TABLE scripts.

What Causes This Error?

The most common cause is an open foreign key lock in another session holding a metadata or row lock on the parent or child table.

Long running transactions or uncommitted SELECT ... FOR UPDATE statements often linger and keep the reference active.

Concurrent DDL on related tables, such as adding an index on a parent table while another session drops the child table, also triggers the error.

Misordered migration scripts can create circular FK references that leave dangling dependencies until the full sequence runs.

How to Fix MySQL ER_TABLE_REFERENCED

Identify and end the blocking session using performance_schema or SHOW ENGINE INNODB STATUS, then re-run the DDL once the lock is released.

If the reference is purely at schema level, temporarily disable FOREIGN_KEY_CHECKS for the session, perform the change, then re-enable the check.

When dropping both parent and child tables, always drop the child tables first to avoid dependency conflicts.

Common Scenarios and Solutions

Altering a child table while a reporting connection holds a metadata lock on the parent: close the reporting connection or wait for it to finish.

Renaming a table in a migration script: wrap statements in a single transaction and commit as soon as possible.

Bulk cleanup using TRUNCATE: use DELETE with smaller batches if truncation is blocked by FK locks.

Best Practices to Avoid This Error

Keep transactions short and commit promptly to release metadata locks.

Run disruptive DDL during maintenance windows with minimal concurrent traffic.

Order migration scripts from leaf child tables up to root parent tables.

Use MySQL SET lock_wait_timeout to fail fast rather than hang.

Galaxy shows live locks so you can terminate blocking queries before running DDL, reducing the chance of ER_TABLE_REFERENCED.

Related Errors and Solutions

ER_ROW_IS_REFERENCED_2 (1451) appears when deleting a referenced row. Delete from the child table first or use ON DELETE CASCADE.

ER_CANNOT_ADD_FOREIGN (1215) occurs when adding a foreign key that violates existing data. Clean the data or align column types.

ER_LOCK_WAIT_TIMEOUT (1205) surfaces when waiting for the same metadata locks; shorten competing transactions or raise lock_wait_timeout.

Common Causes

Open foreign key locks in other sessions

Another connection holds a metadata or row lock on the parent or child table, preventing DDL on the table you target.

Long running transactions

Uncommitted transactions keep references active, blocking any schema change until commit or rollback.

Improper migration order

Scripts that attempt to drop or alter parent tables before children leave active references and trigger the error.

Concurrent DDL on related tables

Simultaneous ALTER TABLE operations on tables with relationships can deadlock and surface as ER_TABLE_REFERENCED.

Related Errors

ER_ROW_IS_REFERENCED_2 (1451)

Raised when a DELETE or UPDATE would break a foreign key constraint.

ER_CANNOT_ADD_FOREIGN (1215)

Occurs while adding a foreign key that conflicts with existing data or index definitions.

ER_LOCK_WAIT_TIMEOUT (1205)

Raised when a session times out waiting for a metadata or row lock.

ER_NO_REFERENCED_ROW_2 (1216)

Appears when inserting or updating a foreign key column that lacks a matching parent row.

FAQs

How do I quickly find what is referencing my table?

Run SHOW ENGINE INNODB STATUS or query performance_schema.metadata_locks to see which thread holds a lock on the table.

Is disabling FOREIGN_KEY_CHECKS safe?

Disable checks only in controlled scripts where you preserve referential integrity and re-enable them immediately.

Can I avoid the error without killing sessions?

You can wait until the blocking transaction commits, but long waits hurt uptime. Setting lock_wait_timeout protects the application.

Does Galaxy help with this error?

Galaxy displays live locks and lets you end blocking sessions from the UI before submitting DDL, reducing the chance of ER_TABLE_REFERENCED.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo