مقدمة في قواعد البيانات: المزيد عن SQL والجبر العلائقي
تغطي هذه الوحدة الجداول الافتراضية (Views) في SQL، وتعديل المخططات (Schema Modification)، والعمليات الأساسية والمتقدمة في الجبر العلائقي (Relational Algebra).
Introduction to Database: More SQL-2 and Relational Algebra
This module covers Virtual Tables (Views) in SQL, Schema Modification statements, and fundamental/advanced operations in Relational Algebra.
أهداف التعلم
- شرح مفهوم ومواصفات الجداول الافتراضية (Views) في SQL.
- القدرة على تغيير وتعديل المخطط (Schema) في SQL.
- فهم وتطبيق عمليات الجبر العلائقي (Relational Algebra) الأساسية والمتقدمة.
- Explain the concept and specification of a View in SQL.
- Ability to change a schema in SQL.
- Understand and apply fundamental and advanced Relational Algebra operations.
1 الجداول الافتراضية (Views) في SQL
1 Views (Virtual Tables) in SQL
الجدول الافتراضي (View) هو جدول غير مخزن فعلياً، يُشتق من جداول أخرى لتسهيل الاستعلامات المعقدة وتوفير آلية أمان.
A view is a 'virtual' table derived from other tables to simplify complex queries and act as a security mechanism.
يتم إنشاء الجداول الافتراضية باستخدام الأمر CREATE VIEW. لا يتم تخزين بيانات الجدول الافتراضي فعلياً في معظم الحالات، بل يتم اشتقاقها ديناميكياً.
يسمح الجدول الافتراضي بعمليات استعلام كاملة، ولكنه يقيد عمليات التحديث.
هناك استراتيجيتان لتنفيذ الجداول الافتراضية:
- تعديل الاستعلام (Query Modification) حيث يقوم نظام إدارة قواعد البيانات بتعديل الاستعلام تلقائياً ليتم تنفيذه على الجداول الأساسية، و
- تجسيد الجدول الافتراضي (View Materialization) حيث يتم إنشاء جدول مؤقت فعلي يحتفظ بنتيجة الاستعلام.
Views are created using the CREATE VIEW command. They allow full query operations but limited update operations since they may not be physically stored.
There are two main implementation techniques:
- Query modification (DBMS automatically modifies the view query into a query on the underlying base tables) and
- View materialization (physically creating and keeping a temporary table that holds the view query result).
تحديث الجداول الافتراضية يمثل تحدياً. إذا كان الجدول الافتراضي يحتوي على عمليات تجميع (Aggregate functions) أو ربط (Joins)، فإنه غالباً لا يمكن تحديثه.
لضمان توافق التحديثات مع شروط الجدول الافتراضي، يُستخدم الشرط WITH CHECK OPTION في نهاية جملة الإنشاء، مما يولد خطأ إذا حاول المستخدم إدخال بيانات لا تتوافق مع شرط الجدول الافتراضي.
Updating views is complex. Views involving joins and aggregate functions are generally not updatable unless mapped to unique updates on base tables.
To enforce updatability constraints, the WITH CHECK OPTION clause is added at the end of the CREATE VIEW statement. This allows the system to check for updatability and generate an error if the view is not updatable.
CREATE VIEW DEPT_INFO(DNO, NO_EMPS, TOTAL_SAL) AS
SELECT DNO, COUNT(*), SUM(SALARY)
FROM EMPLOYEE
GROUP BY DNO;
| Query Modification | View Materialization | |
|---|---|---|
| آلية العمل Mechanism | تعديل الاستعلام تلقائياً ليُنفذ على الجداول الأساسية Automatically modifies view query into base table query | إنشاء جدول مؤقت فعلي يحتفظ بالنتيجة Physically creates a temporary table for the result |
| الكفاءة مع الاستعلامات المعقدة المتكررة Efficiency with frequent complex queries | غير فعال (يتم إعادة الحساب في كل مرة) Inefficient (re-evaluates every time) | فعال (البيانات جاهزة في الجدول المؤقت) Efficient (data is ready in temp table) |
| تحديث البيانات Data Updating | يعكس البيانات الحية دائماً Always reflects live data | يتطلب استراتيجية تحديث تدريجي (Incremental update) للحفاظ على التزامن Requires incremental update strategy to maintain correspondence |
لماذا تعتبر استراتيجية 'تعديل الاستعلام' غير فعالة للجداول الافتراضية المعقدة؟ Why is the 'Query modification' strategy inefficient for complex views?
لأنه إذا تم تطبيق العديد من الاستعلامات على الجدول الافتراضي في فترة زمنية قصيرة، سيضطر النظام لإعادة حساب الاستعلام المعقد من الجداول الأساسية في كل مرة، مما يستهلك موارد النظام.
Because if many queries are applied to the view within a short time period, the DBMS must re-evaluate the complex query on the base tables every single time, consuming significant processing power.
2 تعديل المخطط في SQL
2 Schema Modification in SQL
تُستخدم أوامر مثل DROP و ALTER لتعديل هيكل قاعدة البيانات بعد إنشائها.
Commands like DROP and ALTER are used to modify the database structure after it has been created.
يُستخدم الأمر DROP TABLE لإزالة جدول أساسي وتعريفه بالكامل. إذا كان الجدول مرتبطاً بجداول أخرى (مُشار إليه)، فلا يمكن حذفه إلا بإضافة الكلمة CASCADE التي تزيل جميع الإشارات التلقائية.
يُستخدم الأمر ALTER TABLE لإضافة أو حذف أعمدة (Attributes) من جدول موجود. عند إضافة عمود جديد، سيحتوي على قيم فارغة (NULL) لجميع السجلات الحالية، لذا لا يُسمح باستخدام قيد NOT NULL للعمود الجديد مباشرة.
The DROP TABLE statement removes a base table and its definition. If referenced by other tables, it cannot be dropped unless CASCADE is added, which automatically removes all references.
The ALTER TABLE statement is used to add or drop attributes. When adding a new attribute, it will have NULLs in all existing tuples; hence, NOT NULL is not allowed for the new attribute initially.
تعديل المخطط يعكس الطبيعة الديناميكية لقواعد البيانات العلائقية.
استخدام CASCADE يجب أن يتم بحذر شديد لأنه قد يؤدي إلى فقدان بيانات أو قيود هامة في جداول أخرى تعتمد على الجدول المحذوف.
Schema modification reflects the dynamic nature of relational databases.
Using CASCADE must be done with extreme caution as it can lead to the unintended loss of constraints or dependent data across the database schema.
DROP TABLE DEPENDENT CASCADE;
ALTER TABLE EMPLOYEE DROP BDATE;
ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);
ماذا يحدث للبيانات الموجودة عند إضافة عمود جديد باستخدام ALTER TABLE؟ What happens to existing data when a new attribute is added using ALTER TABLE?
يتم تعبئة العمود الجديد بقيم فارغة (NULL) أو بقيمة افتراضية (Default) لجميع السجلات الموجودة مسبقاً.
The new attribute will have NULLs (or some default value) in all the existing tuples.
3 الجبر العلائقي: العمليات الأحادية
3 Relational Algebra: Unary Operations
العمليات الأحادية تُطبق على جدول واحد وتشمل: الاختيار (SELECT)، الإسقاط (PROJECT)، وإعادة التسمية (RENAME).
Unary operations apply to a single table and include: SELECT, PROJECT, and RENAME.
- الاختيار (SELECT - $\sigma$): يقوم بتصفية السجلات (الصفوف) بناءً على شرط معين. إنها عملية تبادلية (Commutative).
- الإسقاط (PROJECT - $\pi$): يقوم باختيار أعمدة محددة وتجاهل الباقي، مما يخلق تقسيماً عمودياً. هذه العملية تزيل التكرارات تلقائياً وليست تبادلية.
- إعادة التسمية (RENAME - $\rho$): تُستخدم لتغيير اسم الجدول أو أسماء الأعمدة أو كليهما، وهي مفيدة جداً عند كتابة استعلامات معقدة تتطلب دمج نفس الجدول.
- SELECT ($\sigma$): Selects a subset of tuples based on a selection condition (acts as a filter). It is commutative.
- PROJECT ($\pi$): Keeps certain columns and discards others, creating a vertical partitioning. It removes duplicate tuples and is NOT commutative.
- RENAME ($\rho$): Used to rename attributes, the relation name, or both. Essential for complex queries, especially self-joins.
الجبر العلائقي 'مغلق' (Closed)، مما يعني أن نتيجة أي عملية هي دائماً علاقة (جدول) جديدة. هذا يسمح بتداخل العمليات (Nesting).
من المهم التفريق بين عملية SELECT في الجبر العلائقي (التي تعادل جملة WHERE في SQL) وعملية PROJECT (التي تعادل جملة SELECT في SQL).
Relational algebra is 'closed', meaning the result of any operation is a new relation. This allows for nesting operations into relational algebra expressions.
It is crucial to distinguish between RA SELECT (which maps to SQL's WHERE clause) and RA PROJECT (which maps to SQL's SELECT clause).
لماذا تقوم عملية الإسقاط (PROJECT) بإزالة التكرارات؟ Why does the PROJECT operation remove duplicate tuples?
لأن نتيجة عملية الإسقاط يجب أن تكون مجموعة رياضية (Mathematical Set)، والمجموعات الرياضية لا تسمح بوجود عناصر مكررة.
Because the result of the project operation must be a set of tuples, and mathematical sets do not allow duplicate elements.
4 الجبر العلائقي: عمليات المجموعات
4 Relational Algebra: Set Operations
عمليات المجموعات (الاتحاد، التقاطع، الفرق) تتطلب أن يكون الجدولان متوافقين نوعياً (Type Compatible).
Set operations (UNION, INTERSECTION, SET DIFFERENCE) require the two operand relations to be 'Type Compatible'.
تشمل عمليات المجموعات الثنائية:
- الاتحاد (UNION - $\cup$): يدمج السجلات من كلا الجدولين مع إزالة التكرارات.
- التقاطع (INTERSECTION - $\cap$): يُرجع السجلات الموجودة في كلا الجدولين معاً.
- فرق المجموعات (SET DIFFERENCE - $-$): يُرجع السجلات الموجودة في الجدول الأول وغير الموجودة في الثاني.
تتطلب هذه العمليات التوافق النوعي (Type Compatibility): يجب أن يحتوي الجدولان على نفس عدد الأعمدة، ويجب أن تكون مجالات (Domains) الأعمدة المتقابلة متوافقة.
Binary set operations include:
- UNION ($\cup$): Includes all tuples in either R or S, eliminating duplicates.
- INTERSECTION ($\cap$): Includes tuples present in both R and S.
- SET DIFFERENCE ($-$): Includes tuples in R but not in S.
These require Type Compatibility: operands must have the same number of attributes, and corresponding attributes must have compatible domains.
عمليتا الاتحاد والتقاطع هما عمليات تبادلية (Commutative) وتجميعية (Associative)، بينما عملية الفرق ليست تبادلية ($R - S \neq S - R$).
في SQL، تقابل هذه العمليات UNION و INTERSECT و EXCEPT، وتزيل التكرارات افتراضياً، ما لم يُستخدم الكلمة ALL (مثل UNION ALL) للسماح بالتكرارات (Multisets).
UNION and INTERSECTION are commutative and associative, whereas SET DIFFERENCE is not ($R - S \neq S - R$).
In SQL, these map to UNION, INTERSECT, and EXCEPT, which eliminate duplicates by default unless the 'ALL' keyword is appended (e.g., UNION ALL) to treat them as multisets.
في عملية الاتحاد $R \cup S$، ما هي أسماء الأعمدة في الجدول الناتج؟ In the UNION operation $R \cup S$, what are the attribute names of the resulting relation?
يأخذ الجدول الناتج أسماء الأعمدة من الجدول الأول (R) كعرف متبع.
The resulting relation has the same attribute names as the first operand relation R (by convention).
5 الجبر العلائقي: الجداء الديكارتي وعمليات الربط
5 Relational Algebra: Cartesian Product and Joins
الجداء الديكارتي يدمج كل سجل من الجدول الأول مع كل سجل من الجدول الثاني، بينما الربط (JOIN) يدمج السجلات المرتبطة فقط بناءً على شرط معين.
Cartesian Product combines every tuple from R with every tuple from S, while JOIN combines only related tuples based on a condition.
- الجداء الديكارتي (CARTESIAN PRODUCT - $\times$): ينتج عنه جدول يحتوي على جميع التوليفات الممكنة للسجلات. إذا كان R يحتوي على $n$ سجل و S يحتوي على $m$ سجل، فالنتيجة تحتوي على $n \times m$ سجل. نادراً ما يكون مفيداً بمفرده.
- الربط (JOIN - $\bowtie$): يعادل جداء ديكارتي متبوعاً بعملية اختيار (SELECT). يدمج السجلات التي تحقق شرط الربط فقط.
- الربط المتساوي (EQUIJOIN): ربط يستخدم شرط المساواة فقط.
- الربط الطبيعي (NATURAL JOIN - $*$): نوع من الربط المتساوي يزيل العمود المكرر تلقائياً، ويتطلب أن تكون أعمدة الربط بنفس الاسم في كلا الجدولين.
- CARTESIAN PRODUCT ($\times$): Combines tuples in a combinatorial fashion. If R has $n$ tuples and S has $m$ tuples, the result has $n \times m$ tuples. Rarely meaningful alone.
- JOIN ($\bowtie$): Equivalent to a Cartesian Product followed by a SELECT. Combines related tuples satisfying a join condition.
- EQUIJOIN: A join involving only equality comparisons.
- NATURAL JOIN ($*$): An equijoin that gets rid of the superfluous (duplicate) attribute. It requires the join attributes to have the same name in both relations.
عمليات الربط المشروحة هي 'ربط داخلي' (INNER JOIN)، حيث أن السجل الذي لا يجد تطابقاً في الجدول الآخر يتم استبعاده من النتيجة.
إذا أردنا الاحتفاظ بالسجلات غير المتطابقة، نستخدم 'الربط الخارجي' (OUTER JOIN). الجداء الديكارتي لا يتطلب توافقاً نوعياً.
The joins discussed so far are INNER JOINs, meaning a tuple without a matching counterpart is discarded.
To keep unmatched tuples, an OUTER JOIN is used. Note that Cartesian Product does NOT require type compatibility.
| INNER JOIN | OUTER JOIN | |
|---|---|---|
| التعامل مع السجلات غير المتطابقة Handling of unmatched tuples | يتم استبعادها من النتيجة Discarded from the result | تظهر في النتيجة (على الأقل مرة واحدة) Appear in the result at least once |
ماذا يحدث إذا حاولنا عمل ربط طبيعي (NATURAL JOIN) بين جدولين لا يحتويان على أي أعمدة بنفس الاسم؟ What happens if we attempt a NATURAL JOIN between two tables that have no attributes with the same name?
سيتحول الربط الطبيعي فعلياً إلى جداء ديكارتي (Cartesian Product) لأنه لا يوجد شرط ضمني للربط.
The NATURAL JOIN effectively degenerates into a Cartesian Product because there are no common attributes to form an implicit equality condition.
6 الجبر العلائقي: عملية القسمة والمجموعة الكاملة
6 Relational Algebra: Division and Complete Set
عملية القسمة تُستخدم للإجابة على استعلامات تتضمن كلمة 'كل' (Every/All). المجموعة الكاملة للعمليات تتكون من 6 عمليات أساسية يمكن اشتقاق الباقي منها.
The DIVISION operation is used for queries involving 'every' or 'all'. The complete set of operations consists of 6 basic operations from which all others can be derived.
- عملية القسمة (DIVISION - $\div$): تُطبق على جدولين $R(Z)$ و $S(X)$ حيث $X$ مجموعة جزئية من $Z$. النتيجة هي السجلات التي تظهر في $R$ مقترنة بكل سجل موجود في $S$.
- المجموعة الكاملة للعمليات (Complete Set): تتكون من {الاختيار $\sigma$، الإسقاط $\pi$، الاتحاد $\cup$، الفرق $-$، إعادة التسمية $\rho$، الجداء الديكارتي $\times$}. تُسمى كاملة لأن أي عملية أخرى (مثل التقاطع أو الربط) يمكن التعبير عنها باستخدام مزيج من هذه العمليات الست.
- DIVISION ($\div$): Applied to $R(Z)$ and $S(X)$ where $X \subset Z$. The result includes a tuple $t$ if it appears in $R$ in combination with every tuple in $S$.
- Complete Set of Operations: Consists of {SELECT $\sigma$, PROJECT $\pi$, UNION $\cup$, DIFFERENCE $-$, RENAME $\rho$, CARTESIAN PRODUCT $\times$}. It is complete because any other RA operation (like JOIN or INTERSECTION) can be derived from these six.
على سبيل المثال، يمكن اشتقاق التقاطع كالتالي: $R \cap S = (R \cup S) - ((R - S) \cup (S - R))$. واشتقاق الربط كالتالي: $R \bowtie S = \sigma(R \times S)$.
هذا يثبت أن العمليات الست الأساسية كافية نظرياً لبناء أي استعلام علائقي.
For instance, INTERSECTION can be derived as: $R \cap S = (R \cup S) - ((R - S) \cup (S - R))$. JOIN is derived as: $R \bowtie S = \sigma(R \times S)$.
This proves that the six basic operations are theoretically sufficient to build any relational query.
لماذا لا تعتبر عملية الربط (JOIN) جزءاً من المجموعة الكاملة للعمليات؟ Why is the JOIN operation not considered part of the complete set of operations?
لأنه يمكن الاستغناء عنها واشتقاقها باستخدام الجداء الديكارتي متبوعاً بعملية الاختيار.
Because it is not strictly necessary; it can be derived by combining the CARTESIAN PRODUCT and SELECT operations.