modul:m290_guko:learningunits:lu07:theorie:c_update

Action unknown: linkbutton

LU07c – UPDATE: Daten ändern

Der Befehl UPDATE wird verwendet, um bestehende Daten in einer Tabelle zu ändern. Wichtig: Immer mit einer Bedingung (WHERE) einschränken, damit nicht alle Zeilen verändert werden.

Syntax (mit Filter – empfohlen)

UPDATE tabellenname
SET spalte1 = neuer_wert1, spalte2 = neuer_wert2
WHERE bedingung;

Syntax (ohne Filter – gefährlich!)

UPDATE tabellenname
SET spalte1 = neuer_wert1;

Ausgangstabelle:

film_id title director released_year star1
1 Lost in Translation Sofia Coppola 2003 Scarlett Johansson
2 Inception Christopher Nolan 2010 Leonardo DiCaprio
3 Arrival Denis Villeneuve 2016 Amy Adams

Beispiel 1: Gezieltes Update (Primärschlüssel)

UPDATE favourite_film
SET director = 'C. Nolan'
WHERE film_id = 2;
film_id title director released_year star1
1 Lost in Translation Sofia Coppola 2003 Scarlett Johansson
2 Inception C. Nolan 2010 Leonardo DiCaprio
3 Arrival Denis Villeneuve 2016 Amy Adams

Beispiel 2: Mehrere Spalten ändern

UPDATE favourite_film
SET title = 'Arrival (Extended Cut)', star1 = 'Amy Adams'
WHERE film_id = 3;
film_id title director released_year star1
1 Lost in Translation Sofia Coppola 2003 Scarlett Johansson
2 Inception C. Nolan 2010 Leonardo DiCaprio
3 Arrival (Extended Cut) Denis Villeneuve 2016 Amy Adams

Beispiel 3: Semantisches Update

UPDATE favourite_film
SET star1 = 'TBD'
WHERE star1 IS NULL;
Warnung: Ohne WHERE würden alle Zeilen der Spalte mit dem SET-Befehl geändert.
  • modul/m290_guko/learningunits/lu07/theorie/c_update.txt
  • Zuletzt geändert: 2025/09/28 20:09
  • von gkoch