SQL Tutorial
The UPDATE
statement is used to modify existing records in a table.
Here is the basic syntax for the UPDATE
statement:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
table_name
: Specify the name of the table you want to update.SET
: This keyword is used to set the values of the specified columns.column1 = value1, column2 = value2, ...
: The columns you wish to update and the corresponding values.WHERE
: This is an optional clause that can be used to specify the rows that will be updated. If the WHERE
clause is not provided, all rows in the table will be updated.Let's walk through an example:
Assume we have a table called Employees
that has data as follows:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Rick | 35 | California | 65000 |
2 | Dan | 31 | New York | 75000 |
3 | Michelle | 24 | Los Angeles | 55000 |
4 | Ryan | 19 | Missouri | 45000 |
Now, if we want to update the address of the employee with the ID of 1, we could use an UPDATE
statement like this:
UPDATE Employees SET ADDRESS = 'San Francisco' WHERE ID = 1;
After the above SQL is executed, the Employees
table will look like this:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Rick | 35 | San Francisco | 65000 |
2 | Dan | 31 | New York | 75000 |
3 | Michelle | 24 | Los Angeles | 55000 |
4 | Ryan | 19 | Missouri | 45000 |
Notice that only the address of the employee with ID 1 has been updated.
Also, be very careful when using the UPDATE
statement without a WHERE
clause. This will update all rows in the table.
For instance, running the following command will increase everyone's salary by 5000:
UPDATE Employees SET SALARY = SALARY + 5000;
Modifying Data in SQL with UPDATE:
UPDATE
is used to modify existing records in a table.UPDATE your_table SET column1 = new_value WHERE condition;
Updating Specific Columns in SQL:
UPDATE your_table SET column1 = new_value1, column2 = new_value2 WHERE condition;
Using WHERE Clause in SQL UPDATE:
WHERE
clause specifies which rows to update.UPDATE your_table SET column1 = new_value WHERE column2 = 'specific_condition';
Updating Multiple Rows in SQL:
UPDATE your_table SET column1 = new_value WHERE column2 IN ('value1', 'value2');
SQL UPDATE Examples:
UPDATE employees SET salary = salary * 1.1 WHERE department = 'Engineering';
UPDATE products SET stock_quantity = stock_quantity - 10 WHERE product_id = 123;
Updating Data in [Your Database System]:
UPDATE your_table SET column1 = new_value WHERE condition;
UPDATE your_table SET column1 = new_value WHERE condition;
UPDATE your_table SET column1 = new_value WHERE condition;
UPDATE your_table SET column1 = new_value WHERE condition;
Conditional Updates in SQL:
UPDATE your_table SET column1 = CASE WHEN condition1 THEN new_value1 WHEN condition2 THEN new_value2 ELSE column1 END;
Bulk Updates in SQL:
UPDATE
with a JOIN
or a subquery for bulk updates.UPDATE orders SET status = 'Shipped' FROM customers WHERE orders.customer_id = customers.customer_id AND customers.country = 'USA';
Updating Data Based on Subqueries in SQL:
UPDATE products SET price = price * 1.1 WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
Handling NULL Values in SQL UPDATE:
UPDATE your_table SET column1 = COALESCE(new_value, default_value) WHERE condition;
Rolling Back Updates in SQL Transactions:
BEGIN TRANSACTION; UPDATE your_table SET column1 = new_value WHERE condition; -- Check for errors or conditions COMMIT; -- If everything is fine ROLLBACK; -- If an error occurs
Updating Data in Related Tables Using SQL JOIN:
UPDATE orders SET status = 'Shipped' FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id WHERE customers.country = 'USA';