UPDATE – Aggiorniamo i nostri dati

UPDATE – Aggiorniamo i nostri dati

Abbiamo già visto, attraverso alcuni post precedenti, come sia possibile effettuare la gestione dei dati che vengono memorizzati nelle nostre tabelle. Più in particolare abbiamo già trattato i metodi di inserimento, cancellazione e ricerca.
Trovo quindi indispensabile trattare in questo post un'altra operazione fondamentale quale l'aggiornamento e la modifica dei nostri dati concludendo così la prima parte del nostro mini - how- to sulla trattazione delle quattro operazioni fondamentali di ogni database che ricordo essere le seguenti:

  1. INSERIMENTO DATI
  2. MODIFICA
  3. CANCELLAZIONE
  4. RICERCA


Incominciamo subito, dopo essersi loggati al server di nostro interesse e procedendo dal monitor, con il reperire la documentazione ufficiale del comando SQL che consente l'aggiornamento delle informazioni già presenti all'interno di tabelle create precedentemente.
Andiamo quindi ad eseguire:

mysql> h update
Name: 'UPDATE'
Description:
Syntax:
Single-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

Multiple-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]

For the single-table syntax, the UPDATE statement updates columns of
existing rows in tbl_name with new values. The SET clause indicates
which columns to modify and the values they should be given. Each value
can be given as an expression, or the keyword DEFAULT to set a column
explicitly to its default value. The WHERE clause, if given, specifies
the conditions that identify which rows to update. With no WHERE
clause, all rows are updated. If the ORDER BY clause is specified, the
rows are updated in the order that is specified. The LIMIT clause
places a limit on the number of rows that can be updated.

For the multiple-table syntax, UPDATE updates rows in each table named
in table_references that satisfy the conditions. In this case, ORDER BY
and LIMIT cannot be used.

where_condition is an expression that evaluates to true for each row to
be updated. It is specified as described in [HELP SELECT].

The UPDATE statement supports the following modifiers:

o If you use the LOW_PRIORITY keyword, execution of the UPDATE is
delayed until no other clients are reading from the table. This
affects only storage engines that use only table-level locking
(MyISAM, MEMORY, MERGE).

o If you use the IGNORE keyword, the update statement does not abort
even if errors occur during the update. Rows for which duplicate-key
conflicts occur are not updated. Rows for which columns are updated
to values that would cause data conversion errors are updated to the
closest valid values instead.

URL: http://dev.mysql.com/doc/refman/5.0/en/update.html

Dalla documentazione ufficiale è facile intuire che la sintassi di questo comando sarà indispensabile al fine di poter modificare e quindi di "mantenere aggiornate" le informazioni che avevamo precedentemente inserito.
La sintassi di tale istruzione SQL si presenta in modo analogo a quanto già visto per il comando di inserimento con la sola differenza che ci consentirà per l'appunto di modificare dati già esistenti.

Se per esempio desideriamo modificare tutti i nomi degli studenti di una ipotetica tabella 'student' modificando tutti i valori in 'George', dovremmo quindi eseguire qualcosa del tipo:
UPDATE student SET name='George';

Se altresì desideriamo invece modificare  il nome ed il cognome di un cantante inserito in modo errato nella tabella cantanti allora in questo caso ci serviremo anche della clausola where al fine di filtrare, attraverso la chiave primaria o comunque alcune condizioni che mi identifichino in modo univoco i dati di nostro interesse. Si potrebbe scrivere qualcosa del tipo:

UPDATE attori SET
nome='Loredana',
cognome='Bertè'
WHERE id = 3;

L'istruzione vista sopra modifica quindi il singolo record che possiede id=3 e modifica i dati dei campi nome, cognome con i valori che gli vengono inseriti.

Pubblicato in Documentazione Tecnica, MySQL e taggato , .