تصميم الجداول / Designing tables

الترتيب / Order
3 من 4 / 3 of 4
آخر تحديث / Updated
8 أكتوبر 2026 / 8 October 2026

أهداف الدرس

  1. أن تضع كل معلومة في مكان واحد.
  2. أن تربط الجداول بالمفاتيح.

إن كُتب اسم القسم في صف كل موظف، فتغيير الاسم يعني تعديل مئات الصفوف. الأفضل جدول للأقسام، وكل موظف يشير إلى قسمه برقمه.

SQL
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

CREATE TABLE employees (
  id            INTEGER PRIMARY KEY,
  name          VARCHAR(100) NOT NULL,
  department_id INTEGER REFERENCES departments(id)
);
  • لكل جدول مفتاح أساسي.
  • لا معلومة مكرّرة في مكانين.
  • العلاقات بمفاتيح أجنبية.

تمرين

أين تحفظ رقم هاتف القسم؟

الإجابة

في جدول الأقسام، لا في جدول الموظفين.

Lesson goals

  1. Keep each fact in one place.
  2. Link tables with keys.

If the department name is written in every employee's row, renaming it means editing hundreds of rows. Better: a departments table, and each employee points to theirs by number.

SQL
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

CREATE TABLE employees (
  id            INTEGER PRIMARY KEY,
  name          VARCHAR(100) NOT NULL,
  department_id INTEGER REFERENCES departments(id)
);
  • Every table has a primary key.
  • No fact is stored twice.
  • Relationships use foreign keys.

Exercise

Where does a department's phone number go?

Answer

In the departments table, not the employees table.

التعليقات / Comments

لا تعليقات بعد. كن أول من يسأل. / No comments yet. Be the first to ask.

تعليق الطالب يظهر فوراً، وتعليق الزائر بعد موافقة المدرّس. / A student's comment shows at once; a visitor's once the teacher approves it.

في «قواعد البيانات وSQL» / In “Databases and SQL”

  1. 1الاستعلام والربط / Queries and joinsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  2. 2المعاملات / Transactionsفيها كود / has codeالتعليقات مفتوحة / comments open
  3. 3تصميم الجداول / Designing tablesفيها كود / has codeالتعليقات مفتوحة / comments open
  4. 4السرعة والفهارس / Speed and indexesفيها كود / has codeالتعليقات مفتوحة / comments open

↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق↑ ↓ to move · Enter to open · Esc to close