Lesson 8 / الدرس 8
What a database is / ما قاعدة البيانات
Tables, rows, columns and keys — and why a program keeps its data in one rather than in files. From this lesson on you have a real database in the page beside you, and every query you read you can also change and run.
جداول وصفوف وأعمدة ومفاتيح — ولماذا يحفظ برنامج بياناته فيها لا في ملفات. ومن هذا الدرس فصاعدًا لديك قاعدة بيانات حقيقية في الصفحة بجوارك، وكل استعلام تقرؤه تستطيع تغييره وتشغيله.
The guest house you will be working in
guests id name city email phone
rooms id name kind price_per_night capacity
bookings id guest_id room_id check_in check_out nights status
payments id booking_id amount method paid_at
table one kind of thing a spreadsheet page
row one of those things a line on it
column one fact about each of them and it has a type
key what makes a row findable id, and the *_id columns
Why not just files
You could keep all of this in a JSON file — lesson 20 of the JavaScript course showed how. It works until two things happen at once. Two clerks book the last room in the same second and the file ends up with one of the bookings, not both. A crash halfway through writing leaves a half-written file and no data at all. Finding "every confirmed booking in March" means reading the whole file into memory and looping. A database is the piece of software that has already solved all three, and has been solving them since before you were born.
| A database gives you | Which a file does not |
|---|---|
| Many writers at once, safely | Two writes and one of them is lost |
| All-or-nothing changes | A crash mid-write leaves nonsense |
| Finding rows without reading everything | A full scan, every time |
| Rules the data itself must obey | Hope, and careful callers |
| One shared copy for every program | A file each, drifting apart |
The fourth row is the one this section spends most time on. A database can refuse a booking whose guest_id names nobody — the rule lives in the data, so every program that touches it obeys, including the one somebody writes next year in another language.
guest_id فيه لا يسمّي أحدًا — فالقاعدة تسكن في البيانات، فيطيعها كل برنامج يلمسها، ومنه ما يكتبه أحدهم العام القادم بلغة أخرى.
Two words you will see everywhere. A primary key is the column that identifies a row uniquely — id in all four tables here — and no two rows may share one. A foreign key is a column holding another table's primary key, which is how bookings.guest_id means "the guest this booking is for". Those two ideas are the entire mechanism by which separate tables become a connected system, and lesson 10 is about nothing else.
id في الجداول الأربعة هنا — ولا يجوز أن يتشاركه صفان. والمفتاح الأجنبي عمود يحمل مفتاح جدول آخر الأساسي، وهو كيف يعني bookings.guest_id "النزيل الذي هذا الحجز له". وتلك الفكرتان هما كل الآلية التي تصير بها جداول منفصلة نظامًا مترابطًا، والدرس 10 ليس عن سواهما.select وSELECT كلاهما يعمل. والعرف حروف كبيرة للكلمات المفتاحية وصغيرة لأسمائك أنت، وذلك يجعل استعلامًا طويلًا مقروءًا بنظرة: فيبرز شكل الجملة عن الأجزاء التي لك. ولن يرسبك أحد على تجاهله، وسيلاحظ الجميع.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. What is a primary key?
A foreign key is a column holding another table's primary key, which is how bookings.guest_id means "the guest this booking is for".
bookings.guest_id "النزيل الذي هذا الحجز له".2. What happens when two clerks write to the same JSON file at the same instant?
The other two are a crash mid-write leaving nonsense, and having to read everything to find anything.
3. What does the engine in these lessons do with SQL it does not implement?
A wrong answer delivered confidently would be worse than a refusal. Run what matters against the MySQL on your own machine.
Score / النتيجة: 0 / 3
Your task / مهمتك
Explore the four tables until you can describe the hotel without looking. Write and run six queries: every room, every booking, the guests in one city, the rooms under a price you choose, one query that returns no rows at all, and one that misspells a column so you can read the error. For each, write one line above it saying what you expected before you pressed Run, and mark the ones where you were wrong.
استكشف الجداول الأربعة حتى تستطيع وصف الفندق دون النظر. واكتب وشغّل ستة استعلامات: كل الغرف، وكل الحجوزات، والنزلاء في مدينة واحدة، والغرف تحت سعر تختاره، واستعلام لا يعيد أي صف، وواحد يخطئ اسم عمود لتقرأ الخطأ. واكتب فوق كل واحد سطرًا يقول ما توقعته قبل الضغط، وعلّم ما أخطأت فيه.
- Six queries written and run ستة استعلامات مكتوبة ومشغَّلة
- One that returns no rows, and one that errors واحد لا يعيد صفوفًا وواحد يخطئ
- A prediction written above each one توقّع مكتوب فوق كل واحد
- The wrong predictions marked التوقعات الخاطئة معلَّمة