النموذج العلائقي
مقدمة في نموذج البيانات العلائقي، خصائص العلاقات، قيود قاعدة البيانات العلائقية، والعمليات الأساسية لتعديل العلاقات.
Relational Model
Introduction to the Relational Data Model, characteristics of relations, relational database constraints, and basic operations to modify relations.
أهداف التعلم
- إنشاء نموذج علائقي لقاعدة بيانات.
- فهم المصطلحات الرسمية وغير الرسمية للنموذج العلائقي.
- تحديد وتطبيق قيود قاعدة البيانات العلائقية (المجال، المفتاح، الكيان، والمرجعي).
- إدارة عمليات التحديث (الإدراج، الحذف، التعديل) والتعامل مع انتهاكات القيود.
- Create a Relational model of a Database.
- Understand the formal and informal terms of the relational model.
- Identify and apply relational database constraints (Domain, Key, Entity, and Referential).
- Manage update operations (INSERT, DELETE, UPDATE) and handle constraint violations.
1 التعريفات الأساسية للنموذج العلائقي
1 Basic Definitions of the Relational Model
النموذج العلائقي يمثل البيانات كجداول (علاقات) تتكون من صفوف (مجموعات قيم) وأعمدة (صفات).
The relational model represents data as tables (relations) consisting of rows (tuples) and columns (attributes).
يعتمد النموذج العلائقي الرسمي على نظرية المجموعات ومنطق الفئة الأولى. عملياً، يتم تمثيله باستخدام لغة SQL.
يتكون من: العلاقة (Relation) وهي الجدول، الصف (Tuple) وهو مجموعة مرتبة من القيم تمثل كياناً أو علاقة في العالم الحقيقي، الصفة (Attribute) وهي عنوان العمود الذي يعطي دلالة لمعنى البيانات، والمجال (Domain) وهو مجموعة القيم الصالحة التي يمكن أن تأخذها الصفة.
The formal relational model is based on set theory and first-order predicate logic. In practice, it is represented using SQL.
It consists of: a Relation (table), a Tuple (row) which is an ordered set of values representing a real-world entity or relationship, an Attribute (column header) indicating the meaning of the data, and a Domain (set of valid values for an attribute).
تم اقتراح هذا النموذج لأول مرة بواسطة د. إدغار كود (E.F. Codd) في عام 1970، مما أحدث ثورة في إدارة قواعد البيانات.
يحدد المجال ليس فقط نوع البيانات (مثل الأرقام أو التواريخ) بل أيضاً الدور المنطقي الذي تلعبه هذه البيانات في العلاقة.
The model was first proposed by Dr. E.F. Codd in 1970, causing a major revolution in database management.
A domain defines not just the data type (like numbers or dates) but also the logical role the data plays in the relation.
| المصطلح غير الرسميInformal Term | المصطلح الرسميFormal Term | |
|---|---|---|
| الجدولTable | TableTable | RelationRelation |
| عنوان العمودColumn Header | Column HeaderColumn Header | AttributeAttribute |
| القيم الممكنة / نوع البياناتAll possible Column Values / Data Type | Data TypeData Type | DomainDomain |
| الصفRow | RowRow | TupleTuple |
| تعريف الجدولTable Definition | Table DefinitionTable Definition | Schema of a RelationSchema of a Relation |
| الجدول المعبأ بالبياناتPopulated Table | Populated TablePopulated Table | State of the RelationState of the Relation |
لماذا نستخدم مصطلحات رسمية (مثل Tuple و Relation) بدلاً من المصطلحات الشائعة (مثل Row و Table) في الجانب النظري؟ Why do we use formal terms (like Tuple and Relation) instead of common terms (like Row and Table) in theory?
لأن النموذج العلائقي مبني على أساس رياضي صارم (نظرية المجموعات)، والمصطلحات الرسمية تعكس هذا الأساس الدقيق الذي يمنع الغموض.
Because the relational model is built on a strict mathematical foundation (set theory), and formal terms reflect this precise foundation that prevents ambiguity.
2 مخطط العلاقة مقابل حالة العلاقة
2 Relation Schema vs. Relation State
المخطط هو هيكل الجدول (أسماؤه وأعمدته)، بينما الحالة هي البيانات الفعلية الموجودة فيه في لحظة معينة.
The schema is the structure of the table (name and columns), while the state is the actual data inside it at a specific moment.
مخطط العلاقة (Relation Schema): يُرمز له بـ R(A1, A2, ..., An)، حيث R هو اسم العلاقة و A هي الصفات. يمثل وصفاً للعلاقة.
حالة العلاقة (Relation State): يُرمز لها بـ r(R)، وهي مجموعة محددة من الصفوف (Tuples) في العلاقة في لحظة معينة. رياضياً، هي مجموعة فرعية من الجداء الديكارتي (Cartesian product) لمجالات صفاتها.
Relation Schema: Denoted by R(A1, A2, ..., An), where R is the relation name and A are the attributes. It represents the description of the relation.
Relation State: Denoted by r(R), it is a specific set of tuples (rows) in the relation at a particular moment in time. Formally, it is a subset of the Cartesian product of the domains of its attributes.
الجداء الديكارتي يحتوي على جميع المجموعات الممكنة من القيم. حالة العلاقة r(R) تقتصر فقط على الصفوف التي تمثل معلومات صحيحة (Valid information) في العالم المصغر (Mini-world) في وقت معين.
تتغير الحالة مع كل عملية إدراج أو حذف أو تعديل، بينما يبقى المخطط ثابتاً نسبياً.
The Cartesian product contains all possible combinations of values. The relation state r(R) is restricted only to the tuples that represent valid information in the mini-world at a particular time.
The state changes with every insert, delete, or update, while the schema remains relatively static.
هل يمكن أن تتغير حالة العلاقة دون أن يتغير مخططها؟ Can the relation state change without the schema changing?
نعم، هذا هو الوضع الطبيعي. تتغير الحالة باستمرار عند إضافة أو حذف بيانات، بينما المخطط (الهيكل) يبقى ثابتاً.
Yes, this is the normal behavior. The state changes constantly when data is added or removed, while the schema (structure) remains static.
3 خصائص العلاقات
3 Characteristics of Relations
الصفوف في العلاقة غير مرتبة، والقيم داخلها يجب أن تكون ذرية (لا يمكن تجزئتها).
Tuples in a relation are unordered, and values within them must be atomic (indivisible).
تتميز العلاقات بعدة خصائص:
- ترتيب الصفوف: الصفوف غير مرتبة لأن العلاقة رياضياً هي مجموعة (Set) من الصفوف.
- ترتيب الصفات: في التعريف القياسي، الصفات والقيم مرتبة، ولكن في التعريف الأعم يمكن اعتبار الصف كمجموعة غير مرتبة من أزواج (اسم الصفة، القيمة).
- القيم الذرية: كل قيمة في الصف يجب أن تكون ذرية (Atomic) ومستمدة من مجال الصفة.
- القيم الفارغة (NULL): تُستخدم لتمثيل القيم غير المعروفة أو غير القابلة للتطبيق.
Relations have several characteristics:
- Ordering of tuples: Tuples are not considered to be ordered because a relation is a set of tuples.
- Ordering of attributes: In the standard definition, attributes and values are ordered, but a more general definition treats a tuple as an unordered set of (attribute, value) pairs.
- Atomic values: All values are considered atomic (indivisible) and derived from the attribute's domain.
- NULL values: Used to represent values that are unknown or inapplicable.
عدم ترتيب الصفوف يعني أن الاستعلامات لا يجب أن تعتمد على الترتيب الفعلي للبيانات في القرص. إذا كان الترتيب مطلوباً، يجب تحديده صراحة في الاستعلام (مثل ORDER BY في SQL).
القيم الذرية تمنع وجود قوائم أو مصفوفات داخل الخلية الواحدة، وهو أساس الشكل الطبيعي الأول (1NF).
The lack of tuple ordering means queries should not rely on the physical order of data on disk. If ordering is needed, it must be explicitly requested (e.g., ORDER BY in SQL).
Atomic values prevent lists or arrays inside a single cell, which is the foundation of the First Normal Form (1NF).
لماذا يُعتبر السماح بقيم متعددة (قوائم) داخل حقل واحد انتهاكاً لخصائص النموذج العلائقي؟ Why is allowing multiple values (lists) inside a single field a violation of the relational model characteristics?
لأن النموذج العلائقي يشترط أن تكون جميع القيم ذرية (Atomic) لضمان بساطة الاستعلامات وتجنب تعقيد معالجة البيانات المتداخلة.
Because the relational model requires all values to be atomic to ensure query simplicity and avoid the complexity of processing nested data.
4 قيود المفاتيح
4 Key Constraints
المفتاح هو صفة أو مجموعة صفات تميز كل صف في الجدول بشكل فريد.
A key is an attribute or set of attributes that uniquely identifies each row in a table.
المفتاح الفائق (Superkey): مجموعة من الصفات التي تضمن عدم وجود صفين متطابقين في هذه الصفات.
المفتاح (Key / Candidate Key): هو مفتاح فائق أصغري (Minimal)، أي إذا أزلنا أي صفة منه، يفقد خاصية التفرد.
المفتاح الأساسي (Primary Key): هو أحد المفاتيح المرشحة يتم اختياره لتعريف الصفوف بشكل فريد، وعادة ما يُوضع تحته خط في المخطط.
المفاتيح الثانوية (Secondary/Unique Keys): هي المفاتيح المرشحة الأخرى التي لم يتم اختيارها كمفتاح أساسي.
Superkey: A set of attributes ensuring no two tuples have the same values for these attributes.
Key (Candidate Key): A minimal superkey; removing any attribute from it destroys its uniqueness property.
Primary Key: One of the candidate keys chosen to uniquely identify tuples, typically underlined in the schema.
Secondary/Unique Keys: The other candidate keys not chosen as the primary key.
كل مفتاح هو مفتاح فائق، ولكن ليس كل مفتاح فائق هو مفتاح.
القاعدة العامة لاختيار المفتاح الأساسي هي اختيار المفتاح المرشح ذي الحجم الأصغر (بالبايتات) لتسريع عمليات البحث والفهرسة. إذا لم تكن هناك صفة طبيعية فريدة، يمكن إضافة مفتاح اصطناعي (Surrogate Key).
Every key is a superkey, but not vice versa.
The general rule for choosing a primary key is to select the smallest-sized candidate key (in bytes) to speed up indexing and lookups. If no natural unique attribute exists, an artificial (surrogate) key can be added.
إذا كان لدينا جدول للسيارات يحتوي على (رقم اللوحة، رقم الهيكل). كلاهما فريد. أيهما تختار كمفتاح أساسي ولماذا؟ If we have a CAR table with (License_Plate, Chassis_Number). Both are unique. Which do you choose as Primary Key and why?
كلاهما مفاتيح مرشحة. عادة نختار الأقصر حجماً أو الأكثر استخداماً في الاستعلامات كمفتاح أساسي (مثلاً رقم اللوحة إذا كان أقصر وأسهل في التداول).
Both are candidate keys. We usually choose the one with the smaller size or the one more frequently used in queries as the primary key.
5 تكامل الكيان والتكامل المرجعي
5 Entity and Referential Integrity
تكامل الكيان يمنع المفتاح الأساسي من أن يكون فارغاً، والتكامل المرجعي يضمن أن المفتاح الأجنبي يشير إلى سجل موجود فعلاً.
Entity integrity prevents the primary key from being NULL, and referential integrity ensures a foreign key points to an existing record.
تكامل الكيان (Entity Integrity): ينص على أن صفات المفتاح الأساسي (PK) لا يمكن أن تحتوي على قيم فارغة (NULL) في أي صف، لأن المفتاح الأساسي يُستخدم لتعريف الصفوف.
التكامل المرجعي (Referential Integrity): قيد يتضمن علاقتين. يضمن أن قيمة المفتاح الأجنبي (FK) في العلاقة المرجعية (Referencing) يجب أن تتطابق مع قيمة مفتاح أساسي (PK) موجودة في العلاقة المرجوع إليها (Referenced)، أو أن تكون القيمة فارغة (NULL) إذا لم يكن جزءاً من مفتاح أساسي.
Entity Integrity: States that primary key (PK) attributes cannot have NULL values in any tuple, because PK values are used to identify individual tuples.
Referential Integrity: A constraint involving two relations. It ensures that the value of a foreign key (FK) in the referencing relation must either match an existing PK value in the referenced relation, or be NULL (if the FK is not part of its own PK).
التكامل المرجعي هو الأساس لربط الجداول ببعضها البعض. يتم تمثيله في المخططات بسهم يتجه من المفتاح الأجنبي إلى المفتاح الأساسي.
إذا كان المفتاح الأجنبي جزءاً من المفتاح الأساسي لجدوله، فلا يمكن أن يكون NULL بسبب قيد تكامل الكيان.
Referential integrity is the foundation for linking tables together. It is displayed in schemas as a directed arc from the FK to the PK.
If an FK is part of its own table's PK, it cannot be NULL due to the Entity Integrity constraint.
متى يُسمح للمفتاح الأجنبي بأن يكون NULL؟ When is a Foreign Key allowed to be NULL?
يُسمح له بأن يكون NULL طالما أنه ليس جزءاً من المفتاح الأساسي للجدول الذي يتواجد فيه، ولم يتم تطبيق قيد NOT NULL عليه صراحةً.
It is allowed to be NULL as long as it is not part of the primary key of its own table, and no explicit NOT NULL constraint is applied to it.
6 عمليات التحديث وانتهاكات القيود
6 Update Operations and Constraint Violations
عند إدراج أو حذف أو تعديل البيانات، يجب التأكد من عدم خرق أي من قيود قاعدة البيانات.
When inserting, deleting, or updating data, we must ensure no database constraints are violated.
العمليات الأساسية لتعديل قاعدة البيانات هي: INSERT (إدراج)، DELETE (حذف)، و UPDATE (تعديل).
في حالة حدوث انتهاك للقيود، يمكن اتخاذ عدة إجراءات:
- RESTRICT / REJECT: إلغاء العملية التي تسبب الانتهاك.
- CASCADE: نشر التحديث أو الحذف تلقائياً إلى السجلات المرتبطة.
- SET NULL: تعيين المفاتيح الأجنبية المرتبطة إلى قيمة فارغة (إذا كان مسموحاً).
The basic operations for changing the database are: INSERT, DELETE, and UPDATE.
In case of an integrity violation, several actions can be taken:
- RESTRICT / REJECT: Cancel the operation causing the violation.
- CASCADE: Propagate the update or deletion automatically to referencing tuples.
- SET NULL: Set the referencing foreign keys to NULL (if allowed).
عملية الإدراج (INSERT) يمكن أن تنتهك جميع القيود (المجال، المفتاح، الكيان، المرجعي).
عملية الحذف (DELETE) تنتهك عادة التكامل المرجعي فقط (إذا تم حذف سجل يُشار إليه من جدول آخر).
عملية التعديل (UPDATE) تشبه حذفاً يليه إدراج، ويمكن أن تنتهك القيود بناءً على ما إذا كان التعديل يمس مفتاحاً أساسياً أو أجنبياً أو صفة عادية.
An INSERT operation can violate all constraints (Domain, Key, Entity, Referential).
A DELETE operation typically only violates referential integrity (if a referenced record is deleted).
An UPDATE operation is similar to a DELETE followed by an INSERT, and can violate constraints depending on whether a PK, FK, or ordinary attribute is modified.
إذا قمت بحذف قسم من جدول الأقسام، وكان هناك موظفون يعملون في هذا القسم. ماذا يحدث إذا كان خيار الحذف هو CASCADE؟ If you delete a department from the DEPARTMENT table, and there are employees working in it. What happens if the delete option is CASCADE?
سيتم حذف جميع الموظفين الذين يعملون في هذا القسم تلقائياً من جدول الموظفين.
All employees working in that department will be automatically deleted from the EMPLOYEE table.