انتقل إلى المحتوى
أمين أبوزيد
درس مخطط لهMID-011-L02

CTEs العودية

  • الدرس 2 من 7
  • Mid-level
في هذا الدرس 4 موضوعات

هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

في هذا الدرس

walk a hierarchy such as the category tree.

الموضوعات المخطط لها

  • Shape: anchor query UNION ALL recursive query that reads the CTE itself.
  • The recursion stops when a step returns no new rows.
  • Carry a depth or path column to order output and to detect cycles.
  • Bad data with a cycle loops forever; guard with a depth limit or the CYCLE clause (PostgreSQL 14+; verify version before recording).
تفاصيل المخطط المبدئي

عرض عملي

sql
WITH RECURSIVE tree AS (
  SELECT id, parent_id, name, 0 AS depth FROM category WHERE parent_id IS NULL
  UNION ALL
  SELECT c.id, c.parent_id, c.name, t.depth + 1
  FROM category c JOIN tree t ON c.parent_id = t.id
)
SELECT * FROM tree ORDER BY depth, name;

اختبر فهمك

What are the two parts of a recursive CTE?

أظهر الإجابة

An anchor query and a recursive query joined with UNION ALL.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

المختبر والتمارين الاختبار

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.