مقدمة في قواعد البيانات: تصميم قواعد البيانات 1
تغطي هذه الوحدة إرشادات التصميم غير الرسمية لمخططات العلاقات، الاعتمادات الوظيفية، والنماذج العادية (الأول، الثاني، والثالث) بناءً على المفاتيح الأساسية.
Introduction to Database: Database Design-1
This module covers informal design guidelines for relation schemas, functional dependencies, and normal forms (1NF, 2NF, 3NF) based on primary keys.
أهداف التعلم
- وصف الاعتمادات الوظيفية للسمات في العلاقات.
- تطبيق النماذج العادية الأساسية في العلاقات.
- تحديد وتجنب شذوذ التحديث والإدراج والحذف في قواعد البيانات.
- Describe functional dependencies of attributes in relations.
- Apply basic normal forms in relations.
- Identify and avoid update, insert, and delete anomalies in databases.
1 إرشادات التصميم غير الرسمية
1 Informal Design Guidelines
قواعد بسيطة لضمان تصميم قاعدة بيانات خالية من التكرار والمشاكل، مثل التأكد من أن كل جدول يمثل كياناً واحداً.
Simple rules to ensure a database design is free of redundancy and anomalies, like ensuring each table represents one entity.
تتضمن إرشادات التصميم غير الرسمية أربعة مبادئ رئيسية:
- دلالات السمات: يجب أن يمثل كل صف (Tuple) كياناً أو علاقة واحدة.
- تقليل المعلومات المتكررة لتجنب شذوذ التحديث (Update Anomalies) والذي يشمل شذوذ الإدراج والحذف والتعديل.
- القيم الفارغة (Null Values): يجب تصميم العلاقات بحيث تحتوي على أقل عدد ممكن من القيم الفارغة.
- السجلات الزائفة (Spurious Tuples): يجب تصميم العلاقات لتلبية شرط الربط غير المفقود (Lossless Join) لتجنب توليد سجلات خاطئة عند دمج الجداول.
Informal design guidelines include four main principles:
- Semantics of attributes: each tuple should represent one entity or relationship.
- Reducing redundant information to avoid Update Anomalies (Insert, Delete, Modification).
- Null Values: relations should be designed to have as few NULL values as possible.
- Spurious Tuples: relations must satisfy the lossless join condition to avoid generating erroneous tuples during natural joins.
لماذا هذه الإرشادات مهمة؟ لأن التصميم السيئ يؤدي إلى إهدار مساحة التخزين وصعوبة صيانة البيانات.
على سبيل المثال، إذا تم دمج بيانات الموظف والقسم في جدول واحد، فإن تغيير اسم القسم يتطلب تحديث مئات السجلات، مما يزيد من احتمالية عدم اتساق البيانات (Modification Anomaly).
Why are these guidelines critical? Poor design leads to wasted storage and data maintenance nightmares.
For instance, mixing Employee and Department data in one table means changing a department's name requires updating hundreds of rows, increasing the risk of data inconsistency (Modification Anomaly).
كيف يمكن أن يؤدي وجود قيم فارغة (NULL) بكثرة إلى مشاكل في قاعدة البيانات؟ How can having too many NULL values lead to problems in a database?
القيم الفارغة تستهلك مساحة، وتجعل عمليات البحث والربط (JOIN) أكثر تعقيداً، وقد تؤدي إلى نتائج غير متوقعة في الدوال التجميعية (Aggregate functions).
NULL values waste space, complicate search and JOIN operations, and can lead to unpredictable results in aggregate functions.
2 الاعتمادات الوظيفية (FDs)
2 Functional Dependencies (FDs)
قيد يحدد أن قيمة سمة معينة (X) تحدد بشكل فريد قيمة سمة أخرى (Y).
A constraint specifying that the value of one attribute (X) uniquely determines the value of another attribute (Y).
الاعتمادات الوظيفية (FDs) هي مقاييس رسمية لـ "جودة" التصميم العلائقي.
يُقال إن مجموعة السمات X تحدد وظيفياً مجموعة السمات Y (تُكتب X -> Y) إذا كانت قيمة X تحدد قيمة فريدة لـ Y. بعبارة أخرى، لأي صفين t1 و t2، إذا كان t1[X] = t2[X]، فيجب أن يكون t1[Y] = t2[Y].
هذه الاعتمادات مستمدة من القيود الواقعية للبيانات.
Functional dependencies (FDs) are formal measures of the "goodness" of relational designs.
A set of attributes X functionally determines a set of attributes Y (written X -> Y) if the value of X determines a unique value for Y. In other words, for any two tuples t1 and t2, if t1[X] = t2[X], then t1[Y] = t2[Y].
These are derived from real-world constraints on the attributes.
الاعتمادات الوظيفية هي الأساس الرياضي لعملية التطبيع (Normalization).
إذا كان K مفتاحاً للعلاقة R، فإن K يحدد وظيفياً جميع السمات في R.
فهم هذه الاعتمادات يسمح لنا بتفكيك الجداول الكبيرة إلى جداول أصغر دون فقدان المعلومات (Lossless Decomposition).
FDs are the mathematical foundation of Normalization.
If K is a key of relation R, then K functionally determines all attributes in R.
Understanding FDs allows us to decompose large tables into smaller ones without losing information (Lossless Decomposition).
إذا كان رقم الضمان الاجتماعي (SSN) يحدد اسم الموظف (ENAME)، فهل العكس صحيح بالضرورة؟ If SSN -> ENAME, is the reverse necessarily true?
لا، لأن أكثر من موظف قد يشتركون في نفس الاسم، وبالتالي الاسم لا يحدد رقم الضمان الاجتماعي بشكل فريد.
No, because multiple employees can have the same name, so ENAME does not uniquely determine SSN.
3 قواعد الاستدلال للاعتمادات الوظيفية
3 Inference Rules for FDs
قواعد رياضية (بديهيات أرمسترونغ) لاستنتاج اعتمادات وظيفية جديدة من مجموعة اعتمادات موجودة.
Mathematical rules (Armstrong's axioms) to infer new functional dependencies from a given set.
بالنظر إلى مجموعة من الاعتمادات الوظيفية F، يمكننا استنتاج اعتمادات إضافية باستخدام قواعد أرمسترونغ:
- الانعكاس (Reflexive): إذا كانت Y مجموعة فرعية من X، فإن X -> Y.
- الزيادة (Augmentation): إذا كان X -> Y، فإن XZ -> YZ.
- التعدي (Transitive): إذا كان X -> Y و Y -> Z، فإن X -> Z.
هذه القواعد تعتبر سليمة (Sound) وكاملة (Complete). هناك قواعد إضافية مشتقة مثل التفكيك (Decomposition) والاتحاد (Union).
Given a set of FDs F, we can infer additional FDs using Armstrong's inference rules:
- Reflexive: If Y is a subset of X, then X -> Y.
- Augmentation: If X -> Y, then XZ -> YZ.
- Transitive: If X -> Y and Y -> Z, then X -> Z.
These rules are sound and complete. Additional useful rules derived from these include Decomposition, Union, and Pseudotransitivity.
مفهوم الإغلاق (Closure) لمجموعة F (يُرمز له بـ F+) هو مجموعة كل الاعتمادات التي يمكن استنتاجها.
وبالمثل، إغلاق مجموعة من السمات X (يُرمز له بـ X+) هو كل السمات التي يمكن تحديدها وظيفياً بواسطة X.
حساب X+ ضروري لاكتشاف المفاتيح المرشحة (Candidate Keys) في قاعدة البيانات.
The Closure of a set F (denoted F+) is the set of all FDs that can be inferred.
Similarly, the closure of a set of attributes X (denoted X+) is all attributes functionally determined by X.
Computing X+ is essential for discovering Candidate Keys in a database schema.
لماذا تعتبر بديهيات أرمسترونغ "سليمة" و "كاملة"؟ Why are Armstrong's axioms considered "sound" and "complete"?
سليمة تعني أنها لا تستنتج أي اعتمادات خاطئة. كاملة تعني أنها قادرة على استنتاج جميع الاعتمادات الصحيحة الممكنة.
Sound means they do not generate any incorrect dependencies. Complete means they can generate all possible correct dependencies.
4 المفاتيح والسمات
4 Keys and Attributes
المفتاح هو سمة أو مجموعة سمات تحدد الصف بشكل فريد، والسمة الأساسية هي جزء من أي مفتاح مرشح.
A key uniquely identifies a tuple, and a prime attribute is part of any candidate key.
المفتاح الفائق (Superkey) هو مجموعة من السمات التي تحدد الصف بشكل فريد.
المفتاح (Key) هو مفتاح فائق أصغري (Minimal)، أي أن إزالة أي سمة منه تفقده خاصية التفرد.
إذا كان هناك أكثر من مفتاح، تُسمى مفاتيح مرشحة (Candidate Keys)، ويُختار أحدها ليكون المفتاح الأساسي (Primary Key).
السمة الأساسية (Prime Attribute) هي أي سمة تنتمي لأي مفتاح مرشح، والسمة غير الأساسية (Nonprime Attribute) هي التي لا تنتمي لأي مفتاح مرشح.
A Superkey is a set of attributes that uniquely identifies a tuple.
A Key is a minimal superkey (removing any attribute means it's no longer a superkey).
If a relation has multiple keys, they are Candidate Keys; one is chosen as the Primary Key.
A Prime Attribute is a member of SOME candidate key, while a Nonprime Attribute is not a member of ANY candidate key.
التمييز بين السمة الأساسية وغير الأساسية حاسم في تعريف النماذج العادية (خاصة 2NF و 3NF).
الاعتمادات الجزئية (Partial Dependencies) تحدث عندما تعتمد سمة غير أساسية على جزء فقط من المفتاح المرشح، وهذا ما يعالجه النموذج العادي الثاني.
The distinction between prime and nonprime attributes is crucial for defining normal forms (especially 2NF and 3NF).
Partial dependencies occur when a nonprime attribute depends on only a part of a candidate key, which is exactly what 2NF eliminates.
هل كل مفتاح (Key) هو مفتاح فائق (Superkey)؟ وهل العكس صحيح؟ Is every Key a Superkey? Is the reverse true?
نعم، كل مفتاح هو مفتاح فائق. لكن ليس كل مفتاح فائق هو مفتاح، لأن المفتاح الفائق قد يحتوي على سمات إضافية غير ضرورية للتفرد.
Yes, every Key is a Superkey. But not every Superkey is a Key, because a Superkey might contain extra, unnecessary attributes for uniqueness.
5 النموذج العادي الأول (1NF)
5 First Normal Form (1NF)
يجب أن تكون جميع القيم في الجدول ذرية (غير قابلة للتجزئة) ولا يُسمح بالسمات متعددة القيم أو الجداول المتداخلة.
All values in a table must be atomic (indivisible); no multi-valued attributes or nested relations allowed.
النموذج العادي الأول (1NF) هو أساس قواعد البيانات العلائقية.
يمنع هذا النموذج السمات المركبة (Composite attributes)، والسمات متعددة القيم (Multivalued attributes)، والعلاقات المتداخلة (Nested relations).
يجب أن تكون قيمة السمة لأي صف مفردة وذرية (Atomic).
يُعتبر هذا النموذج جزءاً من التعريف الأساسي للعلاقة (Relation).
First Normal Form (1NF) is the foundation of relational databases.
It disallows composite attributes, multivalued attributes, and nested relations.
The value of an attribute for an individual tuple must be atomic.
It is considered to be part of the formal definition of a relation.
لتحويل جدول غير طبيعي إلى 1NF، نقوم عادة بإنشاء صفوف جديدة لكل قيمة من القيم المتعددة (مما قد يسبب تكراراً في البيانات الأخرى)، أو نقوم بفصل السمة متعددة القيم إلى جدول جديد مع المفتاح الأساسي للجدول الأصلي.
To normalize into 1NF, we typically either form new rows for each value of a multivalued attribute (which introduces redundancy in other columns) or extract the multivalued attribute into a new table along with the primary key of the original table.
لماذا تُعتبر السمات متعددة القيم مشكلة في قواعد البيانات العلائقية؟ Why are multivalued attributes a problem in relational databases?
لأنها تكسر البنية المجدولة الثابتة وتجعل الاستعلامات (مثل البحث عن قيمة محددة داخل القائمة) معقدة وبطيئة.
Because they break the flat tabular structure and make querying (like searching for a specific value inside the list) complex and slow.
6 النموذج العادي الثاني (2NF)
6 Second Normal Form (2NF)
يجب أن يكون الجدول في 1NF، وكل سمة غير أساسية يجب أن تعتمد على المفتاح الأساسي بالكامل (لا توجد اعتمادات جزئية).
Table must be in 1NF, and every non-prime attribute must depend on the WHOLE primary key (no partial dependencies).
النموذج العادي الثاني (2NF) يعتمد على مفهوم الاعتماد الوظيفي الكامل (Full Functional Dependency).
يكون المخطط R في 2NF إذا كانت كل سمة غير أساسية (Non-prime attribute) تعتمد وظيفياً بالكامل على المفتاح الأساسي.
الاعتماد الكامل يعني أنه إذا أزلنا أي جزء من المفتاح، فإن الاعتماد لن يعود قائماً.
إذا كان المفتاح الأساسي يتكون من سمة واحدة فقط، فإن الجدول يكون تلقائياً في 2NF (بافتراض أنه في 1NF).
Second Normal Form (2NF) is based on the concept of Full Functional Dependency.
A relation schema R is in 2NF if every non-prime attribute in R is fully functionally dependent on the primary key.
Full dependency means that removing any attribute from the key breaks the dependency.
If the primary key consists of only a single attribute, the relation is automatically in 2NF (assuming it's in 1NF).
التعريف العام لـ 2NF (في حال وجود مفاتيح مرشحة متعددة) ينص على أن كل سمة غير أساسية يجب أن تعتمد بالكامل على *كل* مفتاح من مفاتيح R.
يتم الوصول إلى 2NF عن طريق تفكيك الجدول ونقل السمات التي تعتمد جزئياً على المفتاح إلى جداول جديدة مع ذلك الجزء من المفتاح.
The general definition of 2NF (accounting for multiple candidate keys) states that every non-prime attribute must be fully functionally dependent on *every* key of R.
Normalization to 2NF involves decomposing the relation and moving partially dependent attributes to new relations along with the part of the key they depend on.
إذا كان الجدول يحتوي على مفتاح أساسي مكون من عمود واحد، فهل يمكن أن ينتهك 2NF؟ If a table has a single-column primary key, can it violate 2NF?
لا، لأن الاعتماد الجزئي يتطلب مفتاحاً مركباً (أكثر من عمود). لذا فهو تلقائياً في 2NF.
No, because partial dependency requires a composite key (more than one column). Thus, it is automatically in 2NF.
7 النموذج العادي الثالث (3NF)
7 Third Normal Form (3NF)
يجب أن يكون الجدول في 2NF، ولا يجوز أن تعتمد أي سمة غير أساسية على سمة غير أساسية أخرى (لا توجد اعتمادات متعدية).
Table must be in 2NF, and no non-prime attribute can depend on another non-prime attribute (no transitive dependencies).
النموذج العادي الثالث (3NF) يمنع الاعتمادات المتعدية (Transitive Dependencies).
يكون المخطط في 3NF إذا كان في 2NF ولا توجد أي سمة غير أساسية تعتمد بشكل متعدٍ على المفتاح الأساسي.
الاعتماد المتعدي يحدث عندما يكون X -> Y و Y -> Z، مما يؤدي إلى X -> Z (حيث Y ليس مفتاحاً مرشحاً).
بعبارة أبسط: كل سمة يجب أن تعتمد على المفتاح، المفتاح كله، ولا شيء سوى المفتاح.
Third Normal Form (3NF) eliminates Transitive Dependencies.
A schema is in 3NF if it is in 2NF and no non-prime attribute is transitively dependent on the primary key.
A transitive dependency occurs when X -> Y and Y -> Z, implying X -> Z (where Y is not a candidate key).
Simply put: every attribute must depend on the key, the whole key, and nothing but the key.
التعريف العام لـ 3NF ينص على أنه لأي اعتماد وظيفي X -> A، يجب أن يتحقق أحد الشرطين: إما أن يكون X مفتاحاً فائقاً (Superkey)، أو أن تكون A سمة أساسية (Prime attribute).
إذا لم يتحقق الشرط الثاني، ننتقل إلى نموذج بويس-كود (BCNF) الأكثر صرامة.
The general definition of 3NF states that for any FD X -> A, either: (a) X is a superkey, or (b) A is a prime attribute.
If we disallow condition (b), we get the stricter Boyce-Codd Normal Form (BCNF).
| 1NF | 2NF | 3NF | |
|---|---|---|---|
| القاعدة الأساسية Basic Rule | السمات تعتمد على المفتاح (قيم ذرية) Attributes depend on the key (Atomic values) | السمات تعتمد على المفتاح بالكامل Attributes depend on the WHOLE key | السمات تعتمد على لا شيء سوى المفتاح Attributes depend on NOTHING BUT the key |
| المشكلة التي يحلها Problem Solved | السمات المتعددة والمركبة Multivalued & composite attributes | الاعتمادات الجزئية Partial dependencies | الاعتمادات المتعدية Transitive dependencies |
لماذا يُسمح في 3NF بأن تعتمد سمة أساسية على سمة غير أساسية؟ Why does 3NF allow a prime attribute to depend on a non-prime attribute?
لأن 3NF يركز فقط على السمات غير الأساسية. هذا الاستثناء (الشرط b) هو ما يميز 3NF عن BCNF، حيث يمنع BCNF هذا النوع من الاعتماد تماماً.
Because 3NF only restricts non-prime attributes. This exception (condition b) is exactly what differentiates 3NF from BCNF, which strictly forbids this.