Break it on purpose, then understand why
Keys & Constraints Lab
Constraints exist to reject bad data. The fastest way to understand one is to trigger it and read MySQL's real error message.
کنسٹرینٹس (Constraints) غلط اور غیر معیاری ڈیٹا کو مسترد کرنے کے لیے لگائے جاتے ہیں۔ انہیں سمجھنے کا سب سے تیز طریقہ یہ ہے کہ جان بوجھ کر غلط ڈیٹا ڈال کر MySQL کے حقیقی ایرر میسج (Error Message) کو پڑھا جائے۔
Delete Behavior Lab
Every Foreign Key in this schema was given a deliberate ON DELETE rule.
Nothing defaults to CASCADE everywhere.
| Foreign Key | Rule | Why |
|---|---|---|
| student_parents.student_id | CASCADE | A pure link row — if the student record is removed, the link is meaningless and safe to remove with it. |
| student_parents.parent_id | RESTRICT | Prevents accidentally deleting a parent who is still linked to a student. |
| student_enrollments.student_id / class_id | RESTRICT | Enrollment is academic history — never silently destroyed. |
| attendance.student_id / class_id | RESTRICT | Attendance is a legal/administrative record. |
| exam_results.exam_id / student_id / subject_id | RESTRICT | Academic results must be preserved even if a student later leaves. |
| fee_invoices.student_id | RESTRICT | Financial history must never disappear silently. |
| fee_payments.invoice_id | RESTRICT | A payment record is proof of money received — never deletable via a side effect. |
| class_subject_teachers.* | RESTRICT | An assignment references three important entities; removing any of them should be a deliberate decision, not a silent cascade. |
Why not just CASCADE everywhere? CASCADE looks convenient, but on
historical or financial tables it means one accidental DELETE on a parent row can silently
wipe out years of records with no trace. RESTRICT forces a human (or explicit code) to
decide what to do with the related rows first — see the CRUD Lab
for what a blocked delete actually looks like.
Explain this page
- Is a "failed" INSERT here actually a bug?
- No — a rejected INSERT on this page is a successful test. The constraint did its job.
- What could go wrong without these constraints?
- Duplicate admission numbers, enrollments pointing at students that don't exist, blank required fields, and cascading deletes that erase financial history with no way back.
- کیا یہاں "ناکام" INSERT دراصل کوئی خرابی ہے؟
- نہیں — اس صفحے پر مسترد شدہ INSERT دراصل ایک کامیاب ٹیسٹ ہے۔ کنسٹرینٹ (Constraint) نے اپنا کام درست طریقے سے انجام دیا۔
- ان کنسٹرینٹس کے بغیر کیا غلط ہو سکتا ہے؟
- ایک جیسے داخلہ نمبر (Admission Numbers)، ایسے داخلوں (Enrollments) کے ریکارڈز جو غیر موجود طلبہ کی طرف اشارہ کریں، ضروری فیلڈز (Fields) خالی چھوڑنا، اور کیسکیڈنگ ڈیلیٹس (Cascading Deletes) جو مالی تاریخ ہمیشہ کے لیے مٹا دیں۔