Core: Relational Model & Constraints الجوهر: نموذج البيانات العلائقي والقيود

The mathematical foundation of modern databases. Relations, Tuples, Keys, and Integrity Constraints. الأساس الرياضي لقواعد البيانات الحديثة. العلاقات (Relations)، الصفوف (Tuples)، المفاتيح (Keys)، وقيود التكامل (Integrity Constraints).

Relation Schema Relation Schema Tuples & Domains Tuples & Domains Superkey vs Candidate Key Superkey vs Candidate Key Referential Integrity Referential Integrity Entity Integrity Entity Integrity

Relational Terminology مصطلحات النموذج العلائقي

Formal Term المصطلح الرسمي Informal (Common) Term المصطلح الشائع Definition التعريف
Relation Relation Table Table (جدول) A set of tuples. (The whole table). مجموعة من Tuples (الجدول بأكمله).
Tuple Tuple Row / Record Row / Record (صف / سجل) An ordered set of values: $< v_1, v_2, ..., v_n >$. مجموعة مرتبة من القيم: $< v_1, v_2, ..., v_n >$.
Attribute Attribute Column Header / Field Column / Field (عمود / حقل) Role played by a domain in a relation. الدور الذي يلعبه الـ Domain في العلاقة.
Domain Domain Data Type / Set of Values Data Type (نوع البيانات / نطاق القيم) Set of atomic values (e.g., Integer, Date). مجموعة من القيم الذرية (مثل: عدد صحيح، تاريخ).
Relation State Relation State Populated Table Populated Table (جدول معبأ بالبيانات) The set of tuples at a given moment in time. مجموعة الـ Tuples في لحظة زمنية معينة.

Formal Definitions تعريفات رسمية

Relation State $r(R)$ Relation State $r(R)$

Formally, a relation state $r(R)$ is a subset of the Cartesian Product of the domains of its attributes. رسمياً، حالة العلاقة $r(R)$ هي مجموعة جزئية من Cartesian Product (الضرب الديكارتي) لمجالات سماتها (Attributes Domains).

$r(R) \subseteq dom(A_1) \times dom(A_2) \times ... \times dom(A_n)$

Example: If $dom(A1)=\{0,1\}$ and $dom(A2)=\{a,b\}$, Cartesian Product has 4 tuples. A state might have only $\{<0,a>, <1,b>\}$. مثال: إذا كان $dom(A1)=\{0,1\}$ و $dom(A2)=\{a,b\}$، فإن الضرب الديكارتي يحتوي على 4 صفوف. قد تحتوي الحالة فقط على $\{<0,a>, <1,b>\}$.

Characteristics الخصائص

  • Ordering: Ordering (الترتيب): Tuples are unordered (it's a Set). Attributes are ordered in the schema. الصفوف (Tuples) غير مرتبة (لأنها مجموعة Set). السمات مرتبة في المخطط.
  • Atomicity: Atomicity (الذرية): Values are atomic (indivisible). No composite or multivalued attributes in a cell (1NF). القيم ذرية (غير قابلة للتقسيم). لا توجد سمات مركبة أو متعددة القيم في الخلية الواحدة (1NF).
  • Nulls: Nulls: Represents unknown or inapplicable values. تمثل قيماً مجهولة أو غير قابلة للتطبيق.

Integrity Constraints قيود التكامل (Integrity Constraints)

1. Key Constraints 1. Key Constraints (قيود المفاتيح)

Superkey (SK) Superkey (SK) Any set of attributes that uniquely identifies a tuple. No duplicates allowed. أي مجموعة من السمات التي تحدد الـ Tuple بشكل فريد. التكرار غير مسموح.
Candidate Key (K) Candidate Key (K) A Minimal Superkey. If you remove any attribute, it loses uniqueness. هو Minimal Superkey (مفتاح فائق أصغري). إذا قمت بإزالة أي سمة منه، يفقد صفة التفرد.
Primary Key (PK): Primary Key (PK): One of the candidate keys chosen to identify tuples. Underlined in schema. واحد من الـ Candidate Keys يتم اختياره لتعريف الصفوف. يوضع تحته خط في المخطط.

2. Entity Integrity Constraint 2. Entity Integrity Constraint

The Primary Key attributes of a relation CANNOT have Null values.
Why? Because PK is used to identify tuples. You cannot identify something with "Unknown".
سمات الـ Primary Key للعلاقة لا يمكن أن تكون Null.
لماذا؟ لأن الـ PK يُستخدم لتعريف الصفوف. لا يمكنك تعريف شيء بـ "مجهول".

3. Referential Integrity Constraint 3. Referential Integrity Constraint

Specifies a relationship between two relations. A tuple in referencing relation $R_1$ referencing $R_2$ must refer to an existing tuple in $R_2$. يحدد علاقة بين جدولين. أي صف في العلاقة $R_1$ يشير إلى $R_2$ يجب أن يشير إلى صف موجود فعلياً في $R_2$.

Foreign Key (FK) Rule: قاعدة Foreign Key (FK): Value of FK must be either: قيمة الـ FK يجب أن تكون إما:
  • An existing Primary Key value in the referenced relation. قيمة Primary Key موجودة في العلاقة المشار إليها.
  • Null (if allowed). Null (إذا كان مسموحاً).

Operations & Violations العمليات والانتهاكات

Basic operations: INSERT, DELETE, UPDATE. These can violate constraints. العمليات الأساسية: INSERT, DELETE, UPDATE. هذه العمليات قد تنتهك القيود.

INSERT Can violate:
- Domain (wrong type)
- Key (duplicate PK)
- Entity Integrity (Null PK)
- Referential Integrity (FK not found)
قد ينتهك:
- Domain (نوع خطأ)
- Key (تكرار PK)
- Entity Integrity (PK فارغ)
- Referential Integrity (FK غير موجود)
DELETE Can violate:
- Referential Integrity only.
(If you delete a parent referenced by children).
قد ينتهك:
- Referential Integrity فقط.
(إذا حذفت أباً يُشار إليه من قبل أبناء).
UPDATE Can violate:
- All constraints depending on what is updated (PK or FK or non-key).
قد ينتهك:
- جميع القيود اعتماداً على ما يتم تحديثه (PK أو FK أو حقل عادي).
Handling Violations (Options): معالجة الانتهاكات (الخيارات):
  • RESTRICT / REJECT:RESTRICT / REJECT: Stop the operation. إيقاف العملية.
  • CASCADE:CASCADE: Propagate the update/delete (e.g., delete parent $\to$ delete children). نشر التحديث/الحذف (مثلاً: حذف الأب $\to$ حذف الأبناء).
  • SET NULL:SET NULL: Set referencing FK to Null. تعيين الـ FK المشير إلى Null.

The Exam Vault خزنة الاختبار

Professor's Secrets & Trap Avoidance أسرار البروفيسور وتجنب الفخاخ

TRAP: Superkey vs Key فخ: Superkey vs Key

Is every Key a Superkey? YES.
Is every Superkey a Key? NO. Only minimal superkeys are Keys. If a Superkey has redundant attributes (e.g., {SSN, Name}), it is NOT a Candidate Key.
هل كل Key يعتبر Superkey؟ نعم.
هل كل Superkey يعتبر Key؟ لا. فقط الـ Superkeys الأصغرية (Minimal) هي Keys. إذا كان الـ Superkey يحتوي على سمات زائدة (مثل {SSN, Name})، فهو ليس Candidate Key.

TRAP: Foreign Key Nulls فخ: Foreign Key Nulls

"Can a Foreign Key be NULL?"
YES (usually), unless it is part of the Primary Key of the referencing table.
"Can a Primary Key be NULL?"
NEVER (Entity Integrity).
"هل يمكن أن يكون Foreign Key قيمة NULL؟"
نعم (عادة)، إلا إذا كان جزءاً من الـ Primary Key للجدول الذي يشير.
"هل يمكن أن يكون Primary Key قيمة NULL؟"
أبداً (Entity Integrity).

SECRET: Self-Referencing سر: Self-Referencing

A Foreign Key can refer to the same table. This is called a recursive relationship (e.g., Employee table with `Supervisor_SSN` referencing `SSN` in the same table). يمكن لـ Foreign Key أن يشير إلى نفس الجدول. يسمى هذا علاقة تكرارية (Recursive) (مثل جدول الموظفين وحقل `Supervisor_SSN` يشير إلى `SSN` في نفس الجدول).

KEY CONCEPT: Delete Rules مفهوم أساسي: Delete Rules

If you delete a parent record (referenced by others), the DBMS blocks it by default (RESTRICT). If you want it to automatically delete children, you must define ON DELETE CASCADE in the schema. إذا قمت بحذف سجل أب (مشار إليه من قبل آخرين)، يمنع DBMS ذلك افتراضياً (RESTRICT). إذا كنت تريد حذف الأبناء تلقائياً، يجب عليك تعريف ON DELETE CASCADE في المخطط.