CTEs العودية
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
walk a hierarchy such as the category tree.
الموضوعات المخطط لها
- Shape: anchor query
UNION ALLrecursive query that reads the CTE itself. - The recursion stops when a step returns no new rows.
- Carry a
depthorpathcolumn to order output and to detect cycles. - Bad data with a cycle loops forever; guard with a depth limit or the
CYCLEclause (PostgreSQL 14+; verify version before recording).
تفاصيل المخطط المبدئي
عرض عملي
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.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.