The SQL UPDATE statement is used to modify existing records in a table. It allows you to change the values of one or more columns in one or more rows of a table based on specified conditions.
The basic syntax for the UPDATE statement is as follows:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
In this syntax, table_name is the name of the table that you want to update. column1, column2, and so on, are the names of the columns that you want to modify. value1, value2, and so on, are the new values that you want to set for the columns. The WHERE clause specifies the conditions that the rows must meet in order to be updated.
Here is an example of how to use the UPDATE statement to change the price of a product with an ID of 100:
UPDATE products SET price = 10.99 WHERE product_id = 100;
This statement will update the price column in the products table to 10.99 for the row where product_id is equal to 100.
You can also use the UPDATE statement with joins to modify multiple tables at once. Here is an example:
UPDATE customers SET customers.first_name = 'John', orders.order_date = '2022-01-01' FROM customers JOIN orders ON customers.customer_id = orders.customer_id WHERE customers.last_name = 'Doe';
This statement updates the first_name column in the customers table and the order_date column in the orders table for all rows where the last_name column in the customers table is equal to Doe.
It’s important to be cautious when using the UPDATE statement, as it can permanently modify data in your database. Always test your updates on a small sample of data before applying them to the entire table.