أهداف الدرس
- أن تضع كل معلومة في مكان واحد.
- أن تربط الجداول بالمفاتيح.
إن كُتب اسم القسم في صف كل موظف، فتغيير الاسم يعني تعديل مئات الصفوف. الأفضل جدول للأقسام، وكل موظف يشير إلى قسمه برقمه.
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
- Keep each fact in one place.
- 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.
التعليقات (1) / Comments (1)
Ashrafزائر / Visitor
How to do this?