Structured Query Language/UPDATE 1< Structured Query Language
Hint: Be careful and deactivate AUTOCOMMIT.
The UPDATE command modifies one or more existing rows.
UPDATE <tablename> SET <columnname> = <value>, <columnname> = <value>, ... WHERE <search_condition>;
Values are assigned to the named columns. Unmentioned columns remain unchanged. The search_condition acts in the same way as in the SELECT command. It restricts the coverage of the command to those rows that satisfy the specified criteria. If the WHERE keyword and the search_condition are omitted, all rows of the table are affected. It is possible to specify search_conditions which hit no rows. In this case no rows are updated - and no error or exception occurs.
As shown previously in the introductory page, the UPDATE command is very flexible. Within one command we can choose an arbitrary combination of the two features:
- change one or more columns
- change zero, one or more rows - depending on the search_condition.
Additionally, values can have a dynamic nature like system date, timestamp or timezone - depending on your implementation.
For a recapitulation here is an example:
UPDATE person SET firstname = 'Jimmy Walker', lastname = 'de la Crux' WHERE id = 5; -- revoke the changes ROLLBACK;
We present some more information about additional opportunities of the UPDATE command here.
Change column remark of row Fishing in table hobby to: 'Catching fishes with or without tools.'.
-- Change one column of one row UPDATE hobby SET remark = 'Catching fishes with or without tools.' WHERE hobbyname = 'Fishing'; -- or: WHERE id = 2; COMMIT; -- Check the result SELECT * FROM hobby;
Change column remark of all rows in table hobby to: 'A creative leisure activity.'. Check the success of the command without COMMIT-ting. Revert the changes.
-- no WHERE clause ==> all rows affected UPDATE hobby SET remark = 'A creative leisure activity.'; -- Check the result SELECT * FROM hobby; -- We don't what to save this values. So we revert the changes. ROLLBACK;
-- A WHERE clauses, which hits all rows UPDATE hobby SET remark = 'A creative leisure activity.' WHERE id >= 1; -- Check the result SELECT * FROM hobby; -- We don't what to save this values. So we revert the changes. ROLLBACK;