Theoretical Core: Relational Algebra & Views الجوهر النظري: Relational Algebra & Views
Understanding the mathematical engine beneath SQL (Relational Algebra) and mastering Virtual Tables (Views). فهم المحرك الرياضي الكامن خلف SQL (Relational Algebra) وإتقان الجداول الافتراضية (Views).
SQL Views SQL Views
A View is a "Virtual Table". It does not store data physically; instead, it stores a query that generates the data dynamically when accessed. الـ View عبارة عن "جدول افتراضي" (Virtual Table). لا يخزن البيانات فعلياً؛ بدلاً من ذلك، يخزن استعلاماً يولد البيانات ديناميكياً عند الوصول إليه.
- Simplification:Simplification (التبسيط): Hides complex JOINs from users. يخفي الـ JOINs المعقدة عن المستخدمين.
- Security:Security (الأمان): Hides sensitive columns (e.g., Salary) by not including them in the view. يخفي الأعمدة الحساسة (مثل الراتب) بعدم تضمينها في الـ View.
- Consistency:Consistency (الاتساق): Renaming columns for clarity. إعادة تسمية الأعمدة للوضوح.
- Updates:Updates (التحديثات): Views involving multiple tables (JOINs) or Aggregates (SUM, COUNT) are generally NOT updatable. الـ Views التي تتضمن جداول متعددة (JOINs) أو دوال تجميع (Aggregates) عادة ما تكون غير قابلة للتحديث.
- Performance:Performance (الأداء): The query runs every time the view is accessed. يتم تشغيل الاستعلام في كل مرة يتم فيها الوصول إلى الـ View.
Schema Evolution تطور المخطط (Schema Evolution)
DROP Command Behavior سلوك أمر DROP
Deleting tables or views requires specifying behavior for dependent objects. حذف الجداول أو الـ Views يتطلب تحديد السلوك للكائنات المعتمدة عليها.
Relational Algebra: Unary Relational Algebra: أحادية (Unary)
SELECT ($\sigma$) SELECT ($\sigma$)
Filters rows based on a condition.
SQL Equivalent: WHERE
يصفي الصفوف (rows) بناءً على شرط.
مكافئ SQL: WHERE
PROJECT ($\pi$) PROJECT ($\pi$)
Filters columns. Removes duplicates by definition (Set theory).
SQL Equivalent: SELECT DISTINCT
يصفي الأعمدة (columns). يزيل التكرارات بحكم التعريف (نظرية المجموعات).
مكافئ SQL: SELECT DISTINCT
Set Operations عمليات المجموعات (Set Operations)
These require Union Compatibility: Relations must have the same degree (number of columns) and compatible data types. تتطلب هذه العمليات Union Compatibility: يجب أن يكون للعلاقات نفس الدرجة (عدد الأعمدة) وأنواع بيانات متوافقة.
Union ($\cup$) Union ($\cup$)
Tuples in R OR S. الصفوف في R أو S.
Intersection ($\cap$) Intersection ($\cap$)
Tuples in R AND S. الصفوف في R و S.
Difference ($-$) Difference ($-$)
Tuples in R but NOT in S. الصفوف في R ولكن ليست في S.
Relational Operations العمليات العلائقية (Relational Operations)
Cartesian Product ($R \times S$) Cartesian Product ($R \times S$)
Combines every tuple in R with every tuple in S.
Size: If R has $n$ tuples and S has $m$ tuples, result has $n \times m$ tuples.
يدمج كل صف في R مع كل صف في S.
الحجم: إذا كان R يحتوي على $n$ صفوف و S يحتوي على $m$ صفوف، النتيجة تكون $n \times m$ صفوف.
JOIN ($\bowtie$) JOIN ($\bowtie$)
Equivalent to a Cartesian Product followed by a Selection ($\sigma$). يعادل Cartesian Product متبوعاً بـ Selection ($\sigma$).
DIVISION ($R \div S$) DIVISION ($R \div S$)
Finds tuples in R that match ALL tuples in S.
Example: "Find employees who work on ALL projects."
If $S$ contains projects {P1, P2}, division returns employees who work on P1 AND P2.
يجد الصفوف في R التي تطابق جميع (ALL) الصفوف في S.
مثال: "أوجد الموظفين الذين يعملون في جميع المشاريع."
إذا كان $S$ يحتوي على المشاريع {P1, P2}، القسمة ترجع الموظفين الذين يعملون في P1 و P2 معاً.
The Exam Vault خزنة الاختبار
Professor's Secrets & Trap Avoidance أسرار البروفيسور وتجنب الفخاخ
TRAP: Relational vs SQL فخ: Relational vs SQL
Algebra: $\pi$ (Project) creates a Set (removes duplicates).
SQL: `SELECT` creates a Bag (keeps duplicates).
To make SQL behave like $\pi$, you MUST use `SELECT DISTINCT`.
Algebra: $\pi$ (Project) تنشئ Set (تزيل التكرار).
SQL: `SELECT` تنشئ Bag (تحتفظ بالتكرار).
لجعل SQL تتصرف مثل $\pi$، يجب استخدام `SELECT DISTINCT`.
TRAP: Union Compatibility فخ: Union Compatibility
You cannot `UNION` just any two tables. They must have the same number of columns and compatible data types in the same order. (Student $\cup$ Instructor is likely okay; Student $\cup$ Course is NOT). لا يمكنك عمل `UNION` لأي جدولين. يجب أن يكون لهما نفس عدد الأعمدة و أنواع بيانات متوافقة بنفس الترتيب. (Student $\cup$ Instructor غالباً صحيح؛ Student $\cup$ Course غير صحيح).
SECRET: The Division Keyword سر: كلمة Division
If an exam question uses the word "ALL" or "EVERY" (e.g., "Find students who took EVERY course"), it is a DIVISION operation ($\div$). In SQL, this is often solved with double `NOT EXISTS`. إذا استخدم سؤال الاختبار كلمة "ALL" أو "EVERY" (مثل "أوجد الطلاب الذين أخذوا كل المواد")، فهي عملية DIVISION ($\div$). في SQL، غالباً ما تحل باستخدام `NOT EXISTS` مزدوجة.
KEY CONCEPT: Order of Ops مفهوم أساسي: ترتيب العمليات
In Relational Algebra expression trees, we push Selections ($\sigma$) as far down as possible (perform them first). This reduces the size of intermediate results before doing expensive Joins ($\bowtie$). في أشجار تعبيرات Relational Algebra، ندفع Selections ($\sigma$) للأسفل قدر الإمكان (لتنفيذها أولاً). هذا يقلل حجم النتائج الوسيطة قبل القيام بعمليات Joins ($\bowtie$) المكلفة.