Tables, keys and relationships · الجداول والمفاتيح والعلاقات
More than one table
Real databases split data across several tables. This lesson uses a small shop with two of them:
customer— one row per customer (id,name,city)orders— one row per order (id,customer_id,order_date,total)
Storing the customer's name on every order would repeat it again and again. Keeping customers in their own table stores each one once.
أكثر من جدول
قواعد البيانات الحقيقية تقسم البيانات عبر عدة جداول. تستخدم هذه الدرس متجرًا صغيرًا يحتوي على اثنين منهما:
customer— صف واحد لكل عميل (id،name،city)orders— صف واحد لكل طلب (id،customer_id،order_date،total)
تخزين اسم العميل في كل طلب سيكرر الاسم مرارًا وتكرارًا. الاحتفاظ بالعملاء في جدولهم الخاص يخزن كل واحد مرة واحدة.
Primary keys
A primary key is the column that uniquely identifies each row. It is never blank and never repeated.
customer.idis the primary key ofcustomer.orders.idis the primary key oforders.
Given a primary key value, you can always find exactly one row.
المفاتيح الأساسية
المفتاح الأساسي هو العمود الذي يحدد فريدًا كل صف. لا يكون أبدًا فارغًا ولا مكررًا.
customer.idهو المفتاح الأساسي لـcustomer.orders.idهو المفتاح الأساسي لـorders.
بمعطى قيمة مفتاح أساسي، يمكنك دائمًا العثور على صف واحد بالضبط.
Foreign keys
A foreign key is a column that holds the primary key of another table, linking the two.
orders.customer_id holds a value from customer.id — it says which customer each order belongs to.
Referential integrity means every foreign-key value must match a real primary key: an order can't belong to a customer who doesn't exist.
المفاتيح الأجنبية
المفتاح الأجنبي هو عمود يحمل المفتاح الأساسي لجدول آخر، ويربط الجداول ببعضهما.
orders.customer_id يحمل قيمة من customer.id — إنه يشير إلى أي عميل ينتمي كل طلب إليه.
سلامة المرجعية تعني أن كل قيمة مفتاح أجنبي يجب أن تطابق مفتاحًا أساسيًا حقيقيًا: لا يمكن أن ينتمي طلب لعميل غير موجود.
Common mistakes
- A PRIMARY KEY is unique and cannot be NULL.
- A foreign key points to another table's primary key.
أخطاء شائعة
- المفتاح الأساسي (PRIMARY KEY) فريد ولا يمكن أن يكون NULL.
- المفتاح الأجنبي يشير إلى المفتاح الأساسي لجدول آخر.
Keys link two tables · المفاتيح تربط بين جدولين
A shared key lets a row in one table point to a row in another. · المفتاح المشترك يسمح لصف في أحد الجداول بالإشارة إلى صف في جدول آخر.
Show the whole customer table so you can see the rows and their primary key id. · اعرض جدول customer بالكامل لترى الصفوف ومفتاحها الرئيسي id.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Show each order's id, its customer_id (the foreign key), and its total, ordered by id. · أظهر id لكل طلب، و customer_id الخاص به (المفتاح الأجنبي)، و total الخاص به، مرتباً حسب id.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.