La UPDATE
puede usarse para actualizar datos en una tabla.
El WHERE
La cláusula se puede utilizar para especificar exactamente qué filas se deben actualizar.
Puede actualizar todas las filas, algunas filas o ninguna, según las condiciones de filtrado aplicadas por WHERE
cláusula.
Una actualización sencilla
Aquí hay una actualización simple que cambia el número de artista 16 a Madres de la invención :
UPDATE Artists SET ArtistName = 'Mothers of Invention' WHERE ArtistId = 16;
Y verifique la actualización con un rápido SELECT
:
sqlite> SELECT * FROM Artists WHERE ArtistId = 16; ArtistId ArtistName Bio -------------------- -------------------------------------------------- ---------- 16 Mothers of Invention
Actualizar todas las filas
Puede actualizar todas las filas de una tabla simplemente omitiendo WHERE
cláusula.
Intentémoslo:
UPDATE Artists SET Bio = 'Australian jazz band centred around polyrhythms.';
Y compruébalo con un SELECT
declaración:
sqlite> SELECT ArtistName, Bio FROM Artists; ArtistName Bio -------------------- -------------------------------------------------- Joe Satriani Australian jazz band centred around polyrhythms. Steve Vai Australian jazz band centred around polyrhythms. The Tea Party Australian jazz band centred around polyrhythms. Noiseworks Australian jazz band centred around polyrhythms. Wayne Jury Australian jazz band centred around polyrhythms. Mr Percival Australian jazz band centred around polyrhythms. Iron Maiden Australian jazz band centred around polyrhythms. Atmasphere Australian jazz band centred around polyrhythms. Ian Moss Australian jazz band centred around polyrhythms. Magnum Australian jazz band centred around polyrhythms. Strapping Young Lad Australian jazz band centred around polyrhythms. Slayer Australian jazz band centred around polyrhythms. Primus Australian jazz band centred around polyrhythms. Pat Metheny Australian jazz band centred around polyrhythms. Frank Gambale Australian jazz band centred around polyrhythms. Mothers of Invention Australian jazz band centred around polyrhythms. The Wiggles Australian jazz band centred around polyrhythms.
¡Ups! No creo que todas estas bandas sean bandas de jazz australianas centradas en la polirritmia.
No te preocupes, podemos arreglar eso. Podemos establecer esa columna en NULL
para todos los artistas a los que no se aplica esta biografía. En este caso, la biografía solo se aplica al ID de artista 8
por lo que anularemos todas las filas donde
ArtistId
no es igual a
8
.
UPDATE Artists SET Bio = NULL WHERE ArtistId <> 8;
Y ahora la biografía solo se almacena contra el artista correcto.
sqlite> SELECT ArtistName, Bio FROM Artists; ArtistName Bio -------------------- -------------------------------------------------- Joe Satriani Steve Vai The Tea Party Noiseworks Wayne Jury Mr Percival Iron Maiden Atmasphere Australian jazz band centred around polyrhythms. Ian Moss Magnum Strapping Young Lad Slayer Primus Pat Metheny Frank Gambale Mothers of Invention The Wiggles
¡Cuidado!
Es extremadamente conveniente para nosotros que todas las biografías contenían previamente NULL
valores. Esto significaba que simplemente podíamos volver a establecerlos en NULL
y marcharse.
Esto hubiera sido mucho más complicado si los otros artistas ya tuvieran biografías completas. Configurándolos de nuevo en NULL
no trae de vuelta su antigua bios. Así que tenga cuidado al actualizar los datos.