MySQL Error 1212 occurs when attempting to create a MERGE table with constituent tables located in different databases. MERGE tables are a special storage engine (MRG_MyISAM) that combines multiple identical MyISAM tables into a single virtual table. This error indicates that all underlying tables must reside in the same database for the MERGE table to function correctly.
MySQL Error 1212 (ER_UNION_TABLES_IN_DIFFERENT_DIR) occurs when you attempt to create a MERGE table that references MyISAM tables from different databases. A MERGE table is a special-purpose storage engine that acts as a wrapper around a collection of identical MyISAM tables, presenting them as a single logical table for queries. The MERGE engine requires all constituent tables to be in the same database because it maintains a namespace hierarchy and simplified table resolution logic. When MySQL encounters a MERGE table definition referencing tables across databases, it rejects the operation with Error 1212. This constraint simplifies the MERGE engine's internal workings and prevents potential issues with cross-database lookups and namespace conflicts. Underlying tables must not only be in the same database, but must also be identical in structure: same column names, data types (in the same order), and index definitions. A single structural difference causes the MERGE table to be invalid or unstable.
Check that every table referenced in the MERGE table's UNION clause exists in the same database.
-- List all tables in database_a:
USE database_a;
SHOW TABLES;
-- List all tables in database_b:
USE database_b;
SHOW TABLES;
-- Check the full names with database prefixes:
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME IN ('table1', 'table2', 'table3');All tables you plan to use in the MERGE table must have the same TABLE_SCHEMA value.
MERGE tables require all constituent tables to be identical in structure. Check columns, data types, and indexes.
-- Use database where tables reside:
USE correct_database;
-- Check table structures:
DESC table1;
DESC table2;
DESC table3;
-- Compare column definitions more precisely:
SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_KEY, EXTRA
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'correct_database' AND TABLE_NAME IN ('table1', 'table2')
ORDER BY TABLE_NAME, ORDINAL_POSITION;
-- Compare indexes:
SHOW INDEX FROM table1;
SHOW INDEX FROM table2;Each table must have identical column names, types, order, and indexes. Even a single difference (e.g., INT vs BIGINT, or different column order) will cause issues.
If your constituent tables are in different databases, move them all to a single database before creating the MERGE table.
-- Option 1: Move tables to the target database using RENAME:
RENAME TABLE database_a.table1 TO target_db.table1;
RENAME TABLE database_b.table2 TO target_db.table2;
-- Option 2: Copy tables and drop originals (if RENAME fails due to cross-filesystem):
CREATE TABLE target_db.table1 LIKE database_a.table1;
INSERT INTO target_db.table1 SELECT * FROM database_a.table1;
DROP TABLE database_a.table1;
-- Verify the move:
USE target_db;
SHOW TABLES;Prefer RENAME over CREATE+INSERT+DROP as it is atomic and preserves all metadata. Verify with SHOW TABLES that all tables are now in target_db.
Once all constituent tables are in the same database and have identical structure, create the MERGE table.
USE target_database;
CREATE TABLE merged_table (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
data VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX(id)
) ENGINE=MERGE UNION=(table1, table2, table3) INSERT_METHOD=LAST;
-- Verify the MERGE table was created:
SHOW CREATE TABLE merged_table\G
DESC merged_table;Omit the database name prefix from UNION=(table1, table2, table3)βjust use table names since they are in the same database. If you accidentally use database_a.table1, MySQL will reject it with Error 1212.
Verify the MERGE table works correctly by querying, inserting, and checking data.
-- Query all rows across merged tables:
SELECT COUNT(*) FROM merged_table;
SELECT * FROM merged_table LIMIT 10;
-- Insert into the MERGE table (INSERT_METHOD=LAST sends to the last table):
INSERT INTO merged_table (data) VALUES ('test entry');
-- Query to verify the insert:
SELECT * FROM table3 WHERE data = 'test entry';
-- Update and delete operations:
UPDATE merged_table SET data = 'updated' WHERE id = 1;
DELETE FROM merged_table WHERE id = 1;
-- If any operation fails, check table structure differences and error log.If operations succeed, the MERGE table is configured correctly.
Ensure all code creating or altering MERGE tables references only tables from one database.
// BAD - references tables from different databases:
const query = `
CREATE TABLE merge_table (
id INT PRIMARY KEY,
data VARCHAR(255)
) ENGINE=MERGE UNION=(db_a.table1, db_b.table2) INSERT_METHOD=LAST;
`;
// GOOD - all tables from same database:
const query = `
CREATE TABLE single_db.merge_table (
id INT PRIMARY KEY,
data VARCHAR(255)
) ENGINE=MERGE UNION=(table1, table2, table3) INSERT_METHOD=LAST;
`;Review migration scripts, ORM configurations, and any dynamically generated DDL statements to ensure they never mix databases in UNION clauses.
MERGE tables are deprecated in modern MySQL versions (MySQL 8.0.20 and later) in favor of partitioned tables and views. If you are using MERGE tables in new code, consider migrating to partitioned InnoDB tables instead, which provide superior performance and flexibility without the database restriction.
For legacy systems using MERGE tables: ensure all maintenance operations (schema changes, index rebuilds) apply to all constituent tables simultaneously. MERGE table operations can be slow on very large table sets; consider using views or application-level query routing if you have more than 10-20 constituent tables.
If you cannot consolidate tables into a single database (e.g., due to multi-tenant architecture), use a UNION query or application-level table routing instead of MERGE. Some developers use database views that UNION results from multiple databases, though this requires care with INSERT/UPDATE/DELETE operations.
Always backup all constituent tables before creating or altering a MERGE table. While MERGE table operations themselves are relatively safe, a misconfigured MERGE table can cause unexpected query results or data loss if tables become out of sync.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions