- Dynamic Sidebar mit window.COURSES_MANIFEST für 13 Kurse (216 Kapitel) - Kurswechsler Dropdown mit Direktnavigation - Fortschrittsanzeige mit Solved-Checkmarks (✓) aus localStorage - Responsive Off-Canvas Drawer mit Floating Action Button (☰ Kapitel) & Backdrop auf Mobile/Tablets - Web-IDE Topbar Drawer-Button (☰ Kapitel) für unterbrechungsfreien Vollbild-Editor - Saubere Bereinigung redundanter statischer Legacy-Sidebars
140 lines
8.8 KiB
HTML
140 lines
8.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<link rel="icon" type="image/svg+xml" href="../../../assets/favicon.svg">
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>SQL 08: n:m Verknüpfungstabellen</title>
|
|
<link rel="stylesheet" href="../../../assets/style.css">
|
|
</head>
|
|
<body>
|
|
<div class="app-layout">
|
|
<div class="app-main">
|
|
<header>
|
|
<div class="header-container">
|
|
<div class="header-badge">🏗️ SQL Lehrpfad 2: Joins & Design</div>
|
|
<h1>SQL 08: n:m Beziehungen & Junction Tables</h1>
|
|
<p>Lerne viele-zu-viele Beziehungen mit Junction Tables, zusammengesetzten Primärschlüsseln und 3-Wege-Joins professionell abzubilden.</p>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container">
|
|
<!-- 1. THEORIE: N:M BEZIEHUNGEN & JUNCTION TABLES -->
|
|
<section class="card">
|
|
<h2>💡 1. Warum Junction Tables (Verknüpfungstabellen)?</h2>
|
|
<p>In relationalen Datenbanken kann ein Fremdschlüssel immer nur auf genau eine Zeile zeigen. Um eine n:m Beziehung (ein Student ↔ viele Kurse / ein Kurs ↔ viele Studenten) abzubilden, teilen wir sie über eine <strong>Junction Table</strong> in zwei 1:n Beziehungen auf:</p>
|
|
|
|
<div style="background: var(--bg-code); color: #e2e8f0; padding: 18px; border-radius: 8px; font-family: var(--font-mono); font-size: 0.88rem; line-height: 1.5; margin: 15px 0; overflow-x: auto;">
|
|
┌─────────────────────────┐ ┌─────────────────────────┐ ┌─────────────────────────┐
|
|
│ studenten │ 1 n │ einschreibungen │ n 1 │ kurse │
|
|
├─────────────────────────┤ ───────────── ├─────────────────────────┤ ───────────── ├─────────────────────────┤
|
|
│ PK id │ │ PK,FK student_id │ │ PK id │
|
|
│ matrikelnummer │ │ PK,FK kurs_id │ │ kurs_code │
|
|
│ vorname, nachname │ │ semester │ │ titel │
|
|
│ studiengang │ │ note │ │ ects │
|
|
└─────────────────────────┘ └─────────────────────────┘ └─────────────────────────┘
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 2. THEORIE: COMPOSITE PRIMARY KEY & 3-WEGE-JOIN -->
|
|
<section class="card">
|
|
<h2>🔑 2. Zusammengesetzter Primärschlüssel & 3-Wege-Join</h2>
|
|
<p>Der Primärschlüssel der Zwischentabelle besteht aus beiden Fremdschlüsseln (<code>PRIMARY KEY (student_id, kurs_id)</code>). Dadurch wird garantiert, dass sich ein Student nicht versehentlich doppelt für denselben Kurs einschreibt:</p>
|
|
|
|
<pre><code>-- 3-Wege-Join zur Auflösung der n:m-Beziehung:
|
|
SELECT
|
|
s.matrikelnummer,
|
|
s.vorname || ' ' || s.nachname AS student_name,
|
|
k.kurs_code,
|
|
k.titel AS kurs_titel,
|
|
e.semester,
|
|
e.note
|
|
FROM studenten s
|
|
JOIN einschreibungen e ON s.id = e.student_id
|
|
JOIN kurse k ON e.kurs_id = k.id
|
|
ORDER BY s.nachname, k.kurs_code;</code></pre>
|
|
|
|
<div class="box box-analogie">
|
|
<div class="box-title">🌉 Didaktische Analogie: Die Hängebrücke zwischen zwei Inseln</div>
|
|
<p>Die Tabelle <code>studenten</code> ist die eine Insel, die Tabelle <code>kurse</code> die andere. Die Junction Table <code>einschreibungen</code> ist die Hängebrücke dazwischen! Sie verbindet beide Welten und trägt zusätzliche Informationen wie den Zeitpunkt des Übertritts (Semester) und das Ergebnis (Note).</p>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 3. CHEAT-SHEET -->
|
|
<section class="card">
|
|
<h2>📑 3. Cheat-Sheet: n:m Pattern</h2>
|
|
<div style="overflow-x: auto;">
|
|
<table style="width: 100%; border-collapse: collapse; font-size: 0.9rem; text-align: left;">
|
|
<thead>
|
|
<tr style="border-bottom: 2px solid var(--border-color); background: var(--bg-subtle);">
|
|
<th style="padding: 10px;">Element</th>
|
|
<th style="padding: 10px;">SQL-Code</th>
|
|
<th style="padding: 10px;">Bedeutung</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr style="border-bottom: 1px solid var(--border-color);">
|
|
<td style="padding: 10px;"><strong>Composite PK</strong></td>
|
|
<td style="padding: 10px;"><code>PRIMARY KEY (a_id, b_id)</code></td>
|
|
<td style="padding: 10px;">Verhindert doppelte Verknüpfungen</td>
|
|
</tr>
|
|
<tr style="border-bottom: 1px solid var(--border-color);">
|
|
<td style="padding: 10px;"><strong>FK mit Cascade</strong></td>
|
|
<td style="padding: 10px;"><code>FOREIGN KEY (a_id) REFERENCES t1(id) ON DELETE CASCADE</code></td>
|
|
<td style="padding: 10px;">Löscht Verknüpfungen beim Entfernen einer Entität</td>
|
|
</tr>
|
|
<tr style="border-bottom: 1px solid var(--border-color);">
|
|
<td style="padding: 10px;"><strong>Beziehungs-Attribute</strong></td>
|
|
<td style="padding: 10px;"><code>note REAL, datum TEXT</code></td>
|
|
<td style="padding: 10px;">Zusatzdaten, die nur für das Paar (A, B) gelten</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 10px;"><strong>3-Wege-Join</strong></td>
|
|
<td style="padding: 10px;"><code>FROM a JOIN j ON a.id = j.a_id JOIN b ON j.b_id = b.id</code></td>
|
|
<td style="padding: 10px;">Löst die n:m-Verknüpfung in lesbare Daten auf</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 4. AUFGABEN CHECKLISTE -->
|
|
<section class="card">
|
|
<h2>🎯 Aufgaben & Checkliste in <code>aufgabe.sql</code></h2>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t1">
|
|
<label class="task-label" for="t1"><strong>TODO 1:</strong> Tabellen <code>studenten</code> und <code>kurse</code> anlegen.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t2">
|
|
<label class="task-label" for="t2"><strong>TODO 2:</strong> Junction Table <code>einschreibungen</code> mit zusammengesetztem Primärschlüssel und <code>ON DELETE CASCADE</code> erstellen.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t3">
|
|
<label class="task-label" for="t3"><strong>TODO 3:</strong> 4 Studenten, 3 Kurse und 7 Einschreibungen einfügen.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t4">
|
|
<label class="task-label" for="t4"><strong>TODO 4:</strong> 3-Wege-Join-Abfrage (Studentenname, Kurs-Titel, ECTS, Semester, Note) verfassen.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t5">
|
|
<label class="task-label" for="t5"><strong>TODO 5:</strong> Studenten ermitteln, die den Kurs <code>'DB101'</code> mit Note ≤ 2.0 bestanden haben.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t6">
|
|
<label class="task-label" for="t6"><strong>TODO 6:</strong> Kaskadierendes Löschen testen: Kurs <code>'WEB201'</code> löschen und verifizieren, dass keine verwaisten Zeilen in der Junction Table verbleiben.</label>
|
|
</div>
|
|
|
|
<div style="margin-top: 25px; display: flex; gap: 12px; flex-wrap: wrap;">
|
|
<a href="../../../workspace.html?course=sql&track=track_2_joins_und_design&chapter=08_m_zu_n_verknuepfungstabellen" class="btn" style="background: #3b82f6; color: #fff; font-weight: 700;">💻 In Web-IDE öffnen →</a>
|
|
<a href="../07_datenbank_normalisierung_1nf_bis_3nf/index.html" class="btn btn-secondary">← Zurück zu SQL 07</a>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
<script src="../../../assets/courses_manifest.js"></script>
|
|
<script src="../../../assets/course_sidebar.js"></script>
|
|
</body>
|
|
</html>
|