Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

SQL Pattern & Spezialitäten. © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen Syntax (SQL:1999) WITH RECURSIVE Rekursionstabelle (Spaltenliste) AS (

Ähnliche Präsentationen


Präsentation zum Thema: "SQL Pattern & Spezialitäten. © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen Syntax (SQL:1999) WITH RECURSIVE Rekursionstabelle (Spaltenliste) AS ("—  Präsentation transkript:

1 SQL Pattern & Spezialitäten

2 © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen Syntax (SQL:1999) WITH RECURSIVE Rekursionstabelle (Spaltenliste) AS ( -- Rekursionsinitialisierung SELECT... FROM Tabelle [WHERE Initialisierungsprädikat] -- Rekursionsschritt UNION ALL SELECT... FROM Tabelle, Rekursionstabelle WHERE Rekursionsbedingung ) [Traversierungsklausel] [Zyklusklausel] SELECT... FROM Rekursionstabelle WITH RECURSIVE Rekursionstabelle (Spaltenliste) AS ( -- Rekursionsinitialisierung SELECT... FROM Tabelle [WHERE Initialisierungsprädikat] -- Rekursionsschritt UNION ALL SELECT... FROM Tabelle, Rekursionstabelle WHERE Rekursionsbedingung ) [Traversierungsklausel] [Zyklusklausel] SELECT... FROM Rekursionstabelle

3 © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen Beispiel Anfrage WITH RECURSIVE Erreichbar (Abflug, Ziel) AS ( SELECT Abflug, Ziel FROM Flug WHERE Abflug = Frankfurt UNION ALL SELECT e.Abflug, f.Ziel FROM Erreichbar e, Flug f WHERE e.Ziel = f.Abflug ) SELECT DISTINCT * FROM Erreichbar WITH RECURSIVE Erreichbar (Abflug, Ziel) AS ( SELECT Abflug, Ziel FROM Flug WHERE Abflug = Frankfurt UNION ALL SELECT e.Abflug, f.Ziel FROM Erreichbar e, Flug f WHERE e.Ziel = f.Abflug ) SELECT DISTINCT * FROM Erreichbar

4 © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen Beispiel AbflugZiel FrankfurtParis FrankfurtLondon ParisNew York AthenNew York FrankfurtBerlin ParisIstanbul LondonBerlin AbflugZiel FrankfurtParis FrankfurtLondon FrankfurtNew York FrankfurtBerlin FrankfurtIstanbul Tabelle Flug Ergebnistabelle

5 © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen in Oracle Beispiel SLAVE_ID SUPERVISOR_ID NAME ---------- ------------- -------------------- 1 Big Boss Man 2 1 VP Marketing 3 1 VP Sales 4 3 Joe Sales Guy 6 1 VP Engineering 7 6 Jane Nerd 8 6 Bob Nerd 5 4 Bill Sales Assistant SLAVE_ID SUPERVISOR_ID NAME ---------- ------------- -------------------- 1 Big Boss Man 2 1 VP Marketing 3 1 VP Sales 4 3 Joe Sales Guy 6 1 VP Engineering 7 6 Jane Nerd 8 6 Bob Nerd 5 4 Bill Sales Assistant

6 © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen in Oracle (2) Anfrage SELECT name, slave_id, supervisor_id FROM corporate_slaves CONNECT BY PRIOR slave_id = supervisor_id; NAME SLAVE_ID SUPERVISOR_ID -------------------- ---------- ------------- Big Boss Man 1 VP Marketing 2 1 VP Sales 3 1 Joe Sales Guy 4 3 Bill Sales Assistant 5 4 VP Engineering 6 1 Jane Nerd 7 6 Bob Nerd 8 6 VP Marketing 2 1 VP Sales 3 1 Joe Sales Guy 4 3 Bill Sales Assistant 5 4 Joe Sales Guy 4 3 Bill Sales Assistant 5 4 VP Engineering 6 1 Jane Nerd 7 6 Bob Nerd 8 6 Jane Nerd 7 6 Bob Nerd 8 6 Bill Sales Assistant 5 4 20 rows selected. NAME SLAVE_ID SUPERVISOR_ID -------------------- ---------- ------------- Big Boss Man 1 VP Marketing 2 1 VP Sales 3 1 Joe Sales Guy 4 3 Bill Sales Assistant 5 4 VP Engineering 6 1 Jane Nerd 7 6 Bob Nerd 8 6 VP Marketing 2 1 VP Sales 3 1 Joe Sales Guy 4 3 Bill Sales Assistant 5 4 Joe Sales Guy 4 3 Bill Sales Assistant 5 4 VP Engineering 6 1 Jane Nerd 7 6 Bob Nerd 8 6 Jane Nerd 7 6 Bob Nerd 8 6 Bill Sales Assistant 5 4 20 rows selected.

7 © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen in Oracle (3) NAME SLAVE_ID SUPERVISOR_ID LEVEL --------------------- -------- ------------- ------ Big Boss Man 1 1 VP Marketing 2 1 2 VP Sales 3 1 2 Joe Sales Guy 4 3 3 Bill Sales Assistant 5 4 4 VP Engineering 6 1 2 Jane Nerd 7 6 3 Bob Nerd 8 6 3 NAME SLAVE_ID SUPERVISOR_ID LEVEL --------------------- -------- ------------- ------ Big Boss Man 1 1 VP Marketing 2 1 2 VP Sales 3 1 2 Joe Sales Guy 4 3 3 Bill Sales Assistant 5 4 4 VP Engineering 6 1 2 Jane Nerd 7 6 3 Bob Nerd 8 6 3 SELECT name, slave_id, supervisor_id, level FROM corporate_slaves CONNECT BY PRIOR slave_id = supervisor_id START WITH slave_id = 1;

8 © Prof. T. Kudraß, HTWK Leipzig Top-N-Queries SELECT * FROM (your ordered query) alias_name WHERE rownum <= Rows_to_return ORDER BY rownum; Ermittle die n Datensätze in einer bestimmten Rangordnung (z.B. die n billigsten X) Beispiel SELECT * FROM (select * from suppliers ORDER BY supplier_name) suppliers2 WHERE rownum <= 3 ORDER BY rownum;

9 © Prof. T. Kudraß, HTWK Leipzig Relationendivision Beispiel: Welche Mitarbeiter haben in allen Projekten mitgearbeitet Basiert auf Relationendivision (kein Operator in SQL) EmpnoPno 7834P1 6374P1 7834P2 6374P3 7834P3 4710P2 PnoPname P1Hochschul-CMS P2WLAN Mensa P3Online Eval Mitarbeit Projekt

10 © Prof. T. Kudraß, HTWK Leipzig Relationendivision in SQL Typisches SQL-Pattern SELECT DISTINCT empno FROM Mitarbeit M WHERE NOT EXISTS ((SELECT P.pno FROM Projekt P) MINUS (SELECT pno FROM Mitarbeit WHERE empno = M.empno))

11 © Prof. T. Kudraß, HTWK Leipzig Outer Join EmpnoPno 7834P1 6374P1 7834P2 6374P3 7834P3 4710P2 EmpnoEname... 6810Cook 7834Miller 6374Stiller 4710Hiller 5819Neverman SELECT e.empno, e.ename, p.pno FROM Emp e LEFT OUTER JOIN Projekt p ON e.empno = p.empno Anzeige aller Datensätze einer Tabelle, auch wenn diese die Join-Bedingung nicht erfüllen

12 © Prof. T. Kudraß, HTWK Leipzig CURSOR-Ausdrücke für hierarchische Ausgaben Beispiel Verwendung bei der dynamischen Erzeugung von XML-Dokumenten aus Datenbanken SELECT dname, CURSOR ( SELECT ename FROM emp e WHERE e.deptno = d.deptno ORDER BY ename ) FROM dept d ORDER BY dname;

13 © Prof. T. Kudraß, HTWK Leipzig DNAME CURSOR(SELECTENAMEFR -------------- -------------------- ACCOUNTING CURSOR STATEMENT : 2 CURSOR STATEMENT : 2 ENAME ---------- CLARK KING MILLER OPERATIONS CURSOR STATEMENT : 2 CURSOR STATEMENT : 2 Es wurden keine Zeilen ausgewählt DNAME CURSOR(SELECTENAMEFR -------------- -------------------- RESEARCH CURSOR STATEMENT : 2 CURSOR STATEMENT : 2 ENAME ---------- ADAMS FORD JONES SCOTT SMITH SALES CURSOR STATEMENT : 2 CURSOR STATEMENT : 2 ENAME ---------- ALLEN BLAKE JAMES MARTIN TURNER WARD 6 Zeilen ausgewählt.

14 © Prof. T. Kudraß, HTWK Leipzig Weitere SQL-(Design)-Pattern - Auswahl - Skyline Query Outer Union Histogramme Complexe Constraints (Tree Constraints, temporale Constraints) Baumalgorithmen (Pfad-Queries) Graphalgorithmen: transitive Hülle Quelle: Vadim Tropashko: SQL Design Patterns, Rampant Tech Press, 2007.


Herunterladen ppt "SQL Pattern & Spezialitäten. © Prof. T. Kudraß, HTWK Leipzig Rekursive Anfragen Syntax (SQL:1999) WITH RECURSIVE Rekursionstabelle (Spaltenliste) AS ("

Ähnliche Präsentationen


Google-Anzeigen