Seven problems, worked end to end
Troubleshooting Lab
Every database developer eventually hits these seven. Each one below links to a live lab where you can reproduce the real error yourself.
ڈیٹا بیس کا کام کرنے والے ہر شخص کو آج نہیں تو کل ان سات عام مسائل کا سامنا کرنا پڑتا ہے۔ نیچے دیا گیا ہر مسئلہ ایک لائیو لیب (Live Lab) سے منسلک ہے جہاں آپ اس حقیقی خرابی کو خود دہرا کر دیکھ سکتے ہیں۔
Problem 1: Duplicate unique value
- SYMPTOM
- INSERT fails with: Duplicate entry 'ADM-2025-001' for key 'uq_students_admission_no'
- WHY IT HAPPENED
- A UNIQUE constraint on
admission_noalready has a row with that exact value. - HOW TO CHECK IT
SELECT admission_no, COUNT(*) FROM students GROUP BY admission_no HAVING COUNT(*) > 1;
- HOW TO FIX IT
- Use a different, genuinely unique value, or update the existing row instead of inserting a new one.
- HOW TO PREVENT IT
- Generate admission numbers from a sequence or check for existence before inserting in application code — though the database constraint remains the real safety net.
Problem 2: Missing parent Foreign Key record
- SYMPTOM
- INSERT fails with: Cannot add or update a child row: a foreign key constraint fails
- WHY IT HAPPENED
- The row being inserted references a parent id (e.g.
student_id = 999999) that does not exist in the parent table. - HOW TO CHECK IT
SELECT e.* FROM student_enrollments e LEFT JOIN students s ON s.student_id=e.student_id WHERE s.student_id IS NULL;
- HOW TO FIX IT
- Insert or verify the parent row first, or correct the id being referenced.
- HOW TO PREVENT IT
- Populate dropdowns from live queries (as this lab does) rather than free-typed ids, so the user can only select ids that already exist.
Problem 3: Wrong required field / NULL
- SYMPTOM
- INSERT fails with: Column 'admission_no' cannot be null
- WHY IT HAPPENED
- A NOT NULL column was left empty in the submitted form or import file.
- HOW TO CHECK IT
DESCRIBE students; -- look for 'NO' in the Null column- HOW TO FIX IT
- Supply a real value for every NOT NULL column.
- HOW TO PREVENT IT
- Mark required fields as
requiredin HTML forms as a first line of defense — the database constraint is still what actually protects the data.
Problem 4: Invalid relationship (business-rule duplicate)
- SYMPTOM
- INSERT fails with: Duplicate entry for key 'uq_class_subject' when assigning a second teacher to a subject a class already has covered.
- WHY IT HAPPENED
- The composite UNIQUE constraint
(class_id, subject_id)encodes the rule "one teacher per subject per class" — a deliberate business decision, not an accident. - HOW TO CHECK IT
SELECT class_id, subject_id, COUNT(*) FROM class_subject_teachers GROUP BY class_id, subject_id HAVING COUNT(*) > 1;
- HOW TO FIX IT
- Update the existing assignment row to point at the new teacher instead of inserting a second one.
- HOW TO PREVENT IT
- Model business rules as composite UNIQUE constraints wherever practical — the database enforces the rule even if application code forgets to check.
Problem 5: Trying to delete referenced data
- SYMPTOM
- DELETE fails with: Cannot delete or update a parent row: a foreign key constraint fails
- WHY IT HAPPENED
- Other tables (enrollments, attendance, results, invoices...) still hold rows with this student's id, and those Foreign Keys use
ON DELETE RESTRICT. - HOW TO CHECK IT
SELECT COUNT(*) FROM student_enrollments WHERE student_id = ?; -- repeat for each related table
- HOW TO FIX IT
- Either remove/reassign the related rows first, or — usually better — set
status = 'Withdrawn'instead of deleting the student at all. - HOW TO PREVENT IT
- Prefer soft-deletes (a status column) over hard DELETE for any entity with historical significance.
Problem 6: Slow / inefficient query
- SYMPTOM
- A query that filters, sorts, or joins on a large table takes noticeably longer than expected.
- WHY IT HAPPENED
- EXPLAIN shows type = ALL — MySQL is scanning every row because no index covers the columns being filtered.
- HOW TO CHECK IT
EXPLAIN SELECT * FROM fee_invoices WHERE status = 'Overdue';
- HOW TO FIX IT
- Add an index on the column(s) actually used in the WHERE/JOIN/ORDER BY clause.
- HOW TO PREVENT IT
- Design indexes around real, observed query patterns rather than guessing — and re-check with EXPLAIN after adding one.
Problem 7: Incorrect JOIN producing unexpected results
- SYMPTOM
- A report is missing rows that clearly exist in the underlying tables (or shows far too many rows).
- WHY IT HAPPENED
- An INNER JOIN silently drops any row that has no match on the other side — e.g. a student who exists but currently has no active enrollment disappears entirely from a report that INNER JOINs to enrollments.
- HOW TO CHECK IT
SELECT s.student_id FROM students s LEFT JOIN student_enrollments e ON e.student_id=s.student_id WHERE e.enrollment_id IS NULL;
- HOW TO FIX IT
- Switch to a LEFT JOIN when the "no match yet" case is legitimate and should still appear in the report.
- HOW TO PREVENT IT
- Always ask "should rows with no match on the other side still appear?" before choosing INNER vs LEFT JOIN. See the JOIN Lab for a direct side-by-side.
Explain this page
- Why focus on error messages?
- MySQL's error messages are precise: they name the exact constraint, table, and column that rejected the operation. Reading the real message — rather than guessing — is the fastest path to a correct fix, and the habit that separates competent database work from trial-and-error guessing.
- Why not just fix the symptom?
- Disabling a constraint or catching an error silently does not remove the underlying data problem — it hides it. Each problem below identifies the root cause so the actual issue, not just the surface error, gets resolved.
- ایرر میسجز (Error Messages) پر توجہ کیوں؟
- MySQL کے ایرر میسجز (Error Messages) انتہائی واضح ہوتے ہیں: یہ اس عین کنسٹرینٹ (Constraint)، ٹیبل (Table)، اور کالم (Column) کا نام بتاتے ہیں جس نے اس آپریشن کو مسترد کیا۔ اصل میسج پڑھنا — اندازے لگانے کی بجائے — درست حل تک پہنچنے کا سب سے تیز راستہ ہے، اور یہی عادت ایک قابل ڈیٹا بیس ڈویلپر کو اندھیرے میں ٹٹولنے سے الگ کرتی ہے۔
- صرف ظاہری علامت ٹھیک کیوں نہیں کی جاتی؟
- کسی کنسٹرینٹ (Constraint) کو بند کرنا یا خاموشی سے ایرر (Error) کو نظر انداز کرنا بنیادی ڈیٹا کا مسئلہ حل نہیں کرتا — یہ اسے چھپا دیتا ہے۔ نیچے ہر مسئلے میں اصل وجہ بیان کی گئی ہے تاکہ صرف ظاہری ایرر نہیں، بلکہ اصل خرابی حل ہو۔