Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung |
faecher:informatik:oberstufe:datenbanken:normalisierung:1_normalform:start [25.11.2020 17:56] – [Ergebnis: Die Universaltabelle in der 1NF] sbel | faecher:informatik:oberstufe:datenbanken:normalisierung:1_normalform:start [08.02.2024 07:45] (aktuell) – alte Version wiederhergestellt (08.02.2024 08:40) Frank Schiebel |
---|
</code> | </code> |
| |
Mit Hilfe von Subquerys kann man nun die am Komma aufgesplitteten Werte in die neuen Felder übertragen: | Jetzt kann man die am Komma aufgesplitteten Werte in die neuen Felder übertragen: |
| |
<code sql> | <code sql> |
UPDATE zahnarztbedarf t1 SET | UPDATE zahnarztbedarf SET name = SUBSTRING_INDEX(doktor, ',', 1) |
t1.vorname = (SELECT SUBSTRING_INDEX(doktor, ',', -1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ), | UPDATE zahnarztbedarf SET vorname = SUBSTRING_INDEX(doktor, ',', -1) |
t1.name = (SELECT SUBSTRING_INDEX(doktor, ',', 1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ) | |
WHERE t1.name='' | |
</code> | </code> |
| |
Subquerys sind die in Klammern gesetzten SQL Abfragen, mit denen die Werte ermittelt werden, die für ''t1.vorname'' und ''t1.name'' gesetzt werden. | |
| |
Deine Tabelle sollte jetzt so aussehen: | Deine Tabelle sollte jetzt so aussehen: |
++++ Lösung | | ++++ Lösung | |
<code sql> | <code sql> |
UPDATE zahnarztbedarf t1 SET | UPDATE zahnarztbedarf SET telefon = SUBSTRING_INDEX(telefon_fax, ',', 1) |
t1.fax = (SELECT SUBSTRING_INDEX(telefon_fax, ',', -1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ), | UPDATE zahnarztbedarf SET fax = SUBSTRING_INDEX(telefon_fax, ',', -1) |
t1.telefon = (SELECT SUBSTRING_INDEX(telefon_fax, ',', 1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ) | |
WHERE t1.fax='' | |
</code> | </code> |
++++ | ++++ |
---- | ---- |
| |
**(iii)** Zerlege das Feld ''adresse'' in die Felder ''strasse'', ''wohnort'' und ''plz''. Dabei musst du **zweischrittig** vorgehen, da du den Feldinhalt an verschiedenen Trennzeichen splitten musst: Überführe mit dem Statement von oben zunächst die Strassen in das Feld ''strasse'' und die Kombination aus PLZ und Wohnort in ein temporäres Feld ''plzwo'' indem du am Komma teilst. | **(iii)** Zerlege das Feld ''adresse'' in die Felder ''strasse'', ''wohnort'' und ''plz''. Dabei musst du **zweischrittig** vorgehen, da du den Feldinhalt an verschiedenen Trennzeichen splitten musst: Überführe mit dem Statement von oben zunächst die Strassen in das Feld ''strasse'' und die Kombination aus PLZ und Wohnort in das ''wohnort'' indem du am Komma teilst. |
| |
Überführe dann den Inhalt des temporären Felds nach ''plz'' und ''wohnort'' indem du am Leerzeichen splittest. Lösche dann das Feld ''adresse'' und das Zwischenfeld ''plzwo''. | Überführe dann den Inhalt des Felds ''wohnort'' nach ''plz'' und ''wohnort'' indem du am Leerzeichen splittest. Lösche dann das Feld ''adresse''. |
| |
<wrap hi>Wichtig:</wrap> Um das plzwo Feld sauber am Leerzeichen trennen zu können, muss man sicherstellen, dass kein führendes Leerzeichen mehr vorhanden ist, wo zuvor die Zeichenkombination '',<LEER>'' war, das kann man mit dem mysql Befehl ''TRIM'' erreichen: ''SELECT SUBSTRING_INDEX(TRIM(plzwo), ' ', 1) FROM `zahnarztbedarf`''. | <wrap hi>Wichtig:</wrap> Um das ''wohnort'' Feld sauber am Leerzeichen trennen zu können, muss man sicherstellen, dass kein führendes Leerzeichen mehr vorhanden ist, wo zuvor die Zeichenkombination '',<LEER>'' war, das kann man mit dem mysql Befehl ''TRIM'' erreichen: ''SELECT SUBSTRING_INDEX(TRIM(wohnort), ' ', 1) FROM `zahnarztbedarf`''. |
| |
| |
++++ Lösung Schritt 1 | | ++++ Lösung Schritt 1 | |
<code sql> | <code sql> |
UPDATE zahnarztbedarf t1 SET | UPDATE zahnarztbedarf SET strasse = SUBSTRING_INDEX(adresse, ',', 1) |
t1.plzwo = (SELECT SUBSTRING_INDEX(adresse, ',', -1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ), | UPDATE zahnarztbedarf SET wohnort = SUBSTRING_INDEX(adresse, ',', -1) |
t1.strasse = (SELECT SUBSTRING_INDEX(adresse, ',', 1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ) | |
WHERE t1.plzwo = '' | |
</code> | </code> |
++++ | ++++ |
| |
++++ Lösung Schritt 2 | | ++++ Lösung Schritt 2 | |
| <wrap hi>Achtung! Reihenfolge - warum?</wrap> |
<code sql> | <code sql> |
UPDATE zahnarztbedarf t1 SET | UPDATE zahnarztbedarf SET plz = SUBSTRING_INDEX(TRIM(wohnort), ' ', 1) |
t1.wohnort = (SELECT SUBSTRING_INDEX(TRIM(plzwo), ' ', -1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ), | UPDATE zahnarztbedarf SET wohnort = SUBSTRING_INDEX(TRIM(wohnort), ' ', -1) |
t1.plz = (SELECT SUBSTRING_INDEX(TRIM(plzwo), ' ', 1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ) | |
WHERE t1.plz = '' | |
</code> | </code> |
| |
| ++++ |
| |
| ++++ In einem (komplizierteren) Schritt | |
| Man kann das auch direkt machen, indem man die folgende Anweisung entsprechend für alle 4 Informationen anpasst: |
| <code sql> |
| UPDATE zahnarztbedarf SET ort=SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(adresse, ',', -1)), ' ', -1); |
| </code> |
++++ | ++++ |
| |
++++ Lösung | | ++++ Lösung | |
| |
4 neue Tabellenfelder + temporäres Feld: | 4 neue Tabellenfelder: ''firma'', ''f_strasse'', ''f_plz'', ''f_ort'' |
| |
<code sql> | <code sql> |
Nun haben wir alles zusammen: | Nun haben wir alles zusammen: |
<code sql > | <code sql > |
UPDATE zahnarztbedarf t1 SET | UPDATE zahnarztbedarf SET firma = SUBSTRING_INDEX(hersteller, ',', 1) |
t1.firma = (SELECT SUBSTRING_INDEX(hersteller, ',', 1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ), | UPDATE zahnarztbedarf SET f_strasse = SUBSTRING_INDEX(SUBSTRING_INDEX(hersteller, ',' , 2), ',' , -1) |
t1.f_strasse = (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(hersteller, ',' , 2), ',' , -1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ), | UPDATE zahnarztbedarf SET f_ort = SUBSTRING_INDEX(hersteller, ',', -1) |
t1.temp = (SELECT SUBSTRING_INDEX(hersteller, ',', -1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ) | UPDATE zahnarztbedarf SET f_plz = SUBSTRING_INDEX(TRIM(ort), ' ', 1) |
WHERE t1.firma = '' | UPDATE zahnarztbedarf SET f_ort = SUBSTRING_INDEX(TRIM(ort), ' ', -1) |
</code> | |
| |
Jetzt noch analog zu oben PLZ und Ort aufteilen, TRIM nicht vergessen: | |
| |
<code sql> | |
UPDATE zahnarztbedarf t1 SET | |
t1.f_ort = (SELECT SUBSTRING_INDEX(TRIM(temp), ' ', -1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ), | |
t1.f_plz = (SELECT SUBSTRING_INDEX(TRIM(temp), ' ', 1) FROM `zahnarztbedarf` t2 WHERE t2.id = t1.id ) | |
WHERE t1.f_plz = '' | |
</code> | </code> |
| |
Jetzt kann man ''hersteller'' und ''temp'' löschen. | Jetzt kann man ''hersteller'' löschen. |
++++ | ++++ |
| |