Quickly diagnose and resolve the MySQL errors developers hit most often.
Developers most often see: ERROR 1054 Unknown column
, ERROR 1062 Duplicate entry
, ERROR 1452 Cannot add or update child row
, and ERROR 1049 Unknown database
. Each signals a different root cause—typo, uniqueness collision, broken foreign key, or missing schema.
Focus on the error code and object names. For example, ERROR 1054 (42S22): Unknown column 'emaild' in 'field list'
tells you the column 'emaild' is misspelled. Combine the code with SHOW WARNINGS;
to view the full diagnostic stack.
Confirm that the column exists and is spelled correctly. Use DESCRIBE Customers;
to list actual columns, then update the query. If the column truly does not exist, add it with ALTER TABLE Customers ADD COLUMN ...;
.
-- Typo causes ERROR 1054
SELECT emaild FROM Customers;
-- Inspect table
DESCRIBE Customers;
-- Correct query
SELECT email FROM Customers;
This error appears when an INSERT
or UPDATE
violates a unique index, usually on PRIMARY KEY
or UNIQUE
constraints. Check the conflicting value with SELECT * FROM Customers WHERE id = 42;
. Either remove the duplicate, choose a new key, or switch to INSERT ... ON DUPLICATE KEY UPDATE
.
INSERT INTO Customers(id,name,email)
VALUES(42,'Dana','dana@example.com')
ON DUPLICATE KEY UPDATE email=VALUES(email);
The parent row referenced by the foreign key does not exist. Insert or verify the parent first, or temporarily disable checks with SET FOREIGN_KEY_CHECKS=0;
—but re-enable immediately after to keep integrity.
• Use strict SQL modes (STRICT_ALL_TABLES
).
• Validate input in application code.
• Keep schema migrations under version control.
• Run EXPLAIN
before modifying data in bulk.
• Add unit tests for critical queries.
Run SHOW ERRORS;
right after the failing statement or check the MySQL error log file specified by log_error
.
Yes. Use INSERT IGNORE
to skip duplicates or INSERT ... ON DUPLICATE KEY UPDATE
to merge changes.
You might be referencing the database before COMMIT
in a transaction or misspelling it. Verify with SHOW DATABASES;
.