Update a record: UPDATE

Syntax for the UPDATE statement of the SQL language and examples for editing records in a table.

Update is the SQL language instruction that is used to modify the records of a table. As in the case of Delete, we need to specify through Where which are the records in which we want to make our modifications effective. In addition, obviously, we will have to specify what are the new values ​​of the fields that we want to update.

The syntax is like this:

Update table_name Set field_name1 = field_value1, field_name2 = field_value2,… Where selection_conditions

An applied example:

Update clients Set name=”José” Where name=”Pepe”

By means of this sentence we change the name Pepe to that of José in all the records whose name is Pepe.

Here, too, we must be careful not to forget to use Where, otherwise we would modify all the records in our table.

Update product Set price=990, discount=25

That sentence would modify the price field and the discount field in all the products in the product table. If we have a table with thousands of products with that sentence, they would all be updated, so that all the records would have the same price and the same discount. I assure you that this problem of forgetting where is not something strange to happen, even for experienced programmers and it can lead to serious problems.

See also  Mail Merge
Loading Facebook Comments ...
Loading Disqus Comments ...