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
Four tables, six guests, eight bookings. Every database lesson in this course uses this same small hotel, so you learn the shape once and spend the rest of your attention on the query. Run it: the box at the bottom is a real query box, and the answers come from a real engine — change the query and press Run.

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 youWhich a file does not
Many writers at once, safelyTwo writes and one of them is lost
All-or-nothing changesA crash mid-write leaves nonsense
Finding rows without reading everythingA full scan, every time
Rules the data itself must obeyHope, and careful callers
One shared copy for every programA 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.

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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What is a primary key?

2. What happens when two clerks write to the same JSON file at the same instant?

3. What does the engine in these lessons do with SQL it does not implement?

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 التوقعات الخاطئة معلَّمة
How do you want to submit? / كيف تريد التسليم؟