ERROR 1005 occurs when MySQL cannot create a table, usually due to foreign key constraint issues. Most commonly, this happens when data types don't match, storage engines are incompatible, or referenced tables don't exist. Fix by ensuring matching column types, using InnoDB for both tables, and verifying all referenced objects exist.
MySQL ERROR 1005 (SQLSTATE HY000) is a general "Can't create table" error that prevents table creation in most cases. While the error code is 1005, the underlying cause is often referenced in an error number in parentheses, such as errno: 150 (foreign key constraint issue) or errno: 121 (duplicate constraint name). This error typically indicates that InnoDB cannot fulfill a table creation request, most often because of a malformed foreign key constraint or incompatibility between the table being created and the tables it references. The error acts as a catch-all for various table creation failures, making proper error investigation critical.
The most common cause of ERROR 1005 is a data type mismatch between the foreign key column and the referenced primary key column. Both must be identical, including the UNSIGNED modifier.
-- WRONG - INT UNSIGNED vs INT mismatch
CREATE TABLE parent_table (
id INT UNSIGNED PRIMARY KEY
);
CREATE TABLE child_table (
id INT PRIMARY KEY,
parent_id INT, -- WRONG: should be INT UNSIGNED
FOREIGN KEY (parent_id) REFERENCES parent_table(id)
);
-- CORRECT - Types match exactly
CREATE TABLE parent_table (
id INT UNSIGNED PRIMARY KEY
);
CREATE TABLE child_table (
id INT PRIMARY KEY,
parent_id INT UNSIGNED, -- Matches parent_table.id type
FOREIGN KEY (parent_id) REFERENCES parent_table(id)
);Common matching pairs:
- INT UNSIGNED ↔ INT UNSIGNED
- INT ↔ INT
- BIGINT ↔ BIGINT
- VARCHAR(255) ↔ VARCHAR(255) (exact length and collation)
Use DESCRIBE table_name; to verify column types.
MySQL ERROR 1005 occurs when parent and child tables use different storage engines or when the storage engine doesn't support foreign keys. MyISAM does not support foreign key constraints; you must use InnoDB.
-- Check current storage engine
SHOW CREATE TABLE your_table;
-- Create or alter tables to use InnoDB
CREATE TABLE parent_table (
id INT UNSIGNED PRIMARY KEY
) ENGINE=InnoDB;
CREATE TABLE child_table (
id INT PRIMARY KEY,
parent_id INT UNSIGNED,
FOREIGN KEY (parent_id) REFERENCES parent_table(id)
) ENGINE=InnoDB;
-- If converting existing table
ALTER TABLE your_table ENGINE=InnoDB;Verify InnoDB is enabled on your MySQL server:
SHOW VARIABLES LIKE 'have_innodb';
-- Should return 'YES'Before creating a foreign key constraint, confirm that the parent table and the column you're referencing actually exist in the database.
-- List all tables in current database
SHOW TABLES;
-- Verify the referenced column exists
SHOW COLUMNS FROM parent_table;
-- Check for correct spelling and case sensitivity
-- Table and column names may be case-sensitive depending on OS
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_database'
AND TABLE_NAME = 'parent_table'
AND COLUMN_NAME = 'id';If the parent table doesn't exist, create it first before creating the child table with foreign keys.
If you're adding a foreign key constraint with ALTER TABLE or receiving errno: 121, a constraint with that name may already exist. This prevents new constraints to the same parent table.
-- View all constraints for a table
SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'child_table';
-- If adding a constraint, use a unique name
ALTER TABLE child_table
ADD CONSTRAINT fk_child_parent_unique_name FOREIGN KEY (parent_id)
REFERENCES parent_table(id);
-- Drop an existing constraint if needed
ALTER TABLE child_table DROP FOREIGN KEY old_constraint_name;When using VARCHAR or CHAR columns as foreign keys, the character set and collation must match exactly between parent and child tables.
-- View current character set and collation
SHOW CREATE TABLE your_table;
-- Create tables with matching collation
CREATE TABLE parent_table (
code VARCHAR(50) COLLATE utf8mb4_unicode_ci PRIMARY KEY
) ENGINE=InnoDB;
CREATE TABLE child_table (
id INT PRIMARY KEY,
code_ref VARCHAR(50) COLLATE utf8mb4_unicode_ci,
FOREIGN KEY (code_ref) REFERENCES parent_table(code)
) ENGINE=InnoDB;
-- Or convert existing table
ALTER TABLE child_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;When ERROR 1005 persists, get detailed diagnostic information from InnoDB's internal status report.
-- Run this immediately after the failed CREATE/ALTER TABLE command
SHOW ENGINE INNODB STATUS;
-- Look for the "LATEST FOREIGN KEY ERROR" section
-- It will show:
-- - Which constraint failed
-- - Why it failed (type mismatch, missing index, etc.)
-- - Which tables and columns are involved
-- Example output might show:
-- 150318 16:58:05 Error in foreign key constraint of table db/child_table:
-- Could not find a matching row in the referenced table parent_tableThis output provides specific details that generic error messages don't include, making debugging much faster.
When importing database dumps or restoring from backups, you can temporarily disable foreign key checks to work around constraint issues. Be cautious: this can create data inconsistencies if not done carefully.
-- Disable foreign key checks (careful - can cause inconsistent data)
SET FOREIGN_KEY_CHECKS = 0;
-- Now create your tables or import your dump
CREATE TABLE child_table (
id INT PRIMARY KEY,
parent_id INT UNSIGNED,
FOREIGN KEY (parent_id) REFERENCES parent_table(id)
) ENGINE=InnoDB;
-- Re-enable checks
SET FOREIGN_KEY_CHECKS = 1;
-- Verify data integrity
-- Make sure all parent_id values actually exist in parent_table
SELECT COUNT(*) FROM child_table
WHERE parent_id NOT IN (SELECT id FROM parent_table);Only use this approach for imports or migrations. Always re-enable checks and verify data consistency afterward.
Errno Reference: ERROR 1005 can manifest with several errno codes that indicate specific root causes:
- errno: 150 - Foreign key constraint is incorrectly formed (data type mismatch, missing index, storage engine incompatibility)
- errno: 121 - Duplicate foreign key constraint name already exists
- errno: 140 - Wrong CREATE TABLE options (commonly related to storage engine or compression settings)
- errno: -1 - Internal InnoDB error, often caused by column names conflicting with reserved InnoDB table names
Case Sensitivity: Table and column names may be case-sensitive depending on your operating system. Linux is case-sensitive; Windows and macOS are not. Always verify spelling and case when referencing tables.
Circular References: MySQL does not allow circular foreign key relationships (Table A → Table B → Table A). If you need bidirectional relationships, redesign your schema using a junction table instead.
Orphaned Intermediate Tables: When MySQL encounters issues during ALTER TABLE operations, it may leave behind temporary tables named #sql-.... These can prevent future operations. Use SHOW TABLES; to check for them and delete them manually or restart MySQL.
Storage Engine Constraints: Only InnoDB fully supports foreign key constraints in modern MySQL. MyISAM, MEMORY, and CSV engines do not. If you must use non-InnoDB engines, remove foreign key constraints or migrate to InnoDB.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL