MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL UPDATE - multi-table association update

In MySQL, you can update multiple tables at once using the multi-table UPDATE syntax. This can be extremely useful when tables are related to each other.

Here is the syntax:

UPDATE table1
INNER JOIN table2
ON table1.common_field = table2.common_field
SET table1.target_column = value_or_expression,
    table2.target_column = value_or_expression
WHERE condition;

Example:

Consider the following two tables:

orders table:

idcustomer_idproductquantity
1101Apple10
2102Banana5
3103Cherry15

customers table:

idname
101John Doe
102Jane Doe
103Jim Smith

Let's say you want to update the product in the orders table to 'Orange' and the name in the customers table to 'Updated Customer' where the customer_id is '101'. Here is how you would do it:

UPDATE orders o
INNER JOIN customers c ON o.customer_id = c.id
SET o.product = 'Orange',
    c.name = 'Updated Customer'
WHERE o.customer_id = 101;

After executing the above SQL, the 'Apple' product associated with 'John Doe' will change to 'Orange', and the name 'John Doe' will change to 'Updated Customer'.

Important Notes:

  • Be very careful when using multi-table update as it can update large amounts of data at once, and you cannot undo this operation.

  • If the UPDATE statement includes an ORDER BY clause, the rows are updated in the order that is specified but the ORDER BY clause is valid only in single-table updates. In multiple-table updates, it is undefined which rows are updated first.

  • You can also use LEFT JOIN instead of INNER JOIN depending on your requirements.

  1. MySQL UPDATE Multi-Table Example:

    • Description: The UPDATE statement can be used to update records in multiple tables simultaneously.
    • Example Code:
      UPDATE table1, table2
      SET table1.column1 = value1, table2.column2 = value2
      WHERE table1.common_column = table2.common_column
        AND condition;
      
  2. How to Perform Multi-Table Updates in MySQL:

    • Description: Use the UPDATE statement with multiple tables to update records based on a common condition.
    • Example Code:
      UPDATE customers, orders
      SET customers.status = 'Gold'
      WHERE customers.customer_id = orders.customer_id
        AND orders.total_amount > 1000;
      
  3. Updating Multiple Tables with JOIN in MySQL:

    • Description: Use JOIN to update records in multiple tables based on associated columns.
    • Example Code:
      UPDATE products
      INNER JOIN inventory ON products.product_id = inventory.product_id
      SET products.stock_status = 'In Stock'
      WHERE inventory.quantity > 0;
      
  4. MySQL UPDATE with INNER JOIN for Association Updates:

    • Description: Use INNER JOIN in the UPDATE statement for updating records based on associations between tables.
    • Example Code:
      UPDATE employees
      INNER JOIN departments ON employees.department_id = departments.department_id
      SET employees.manager_id = 1001
      WHERE departments.department_name = 'IT';
      
  5. Multi-Table Updates Using Aliases in MySQL:

    • Description: Use aliases for table names to simplify the syntax in multi-table updates.
    • Example Code:
      UPDATE c
      SET c.status = 'Platinum'
      FROM customers c
      INNER JOIN orders o ON c.customer_id = o.customer_id
      WHERE o.total_amount > 5000;
      
  6. Using Subqueries in MySQL UPDATE for Associations:

    • Description: Subqueries can be used in the SET clause to update records based on the result of a subquery.
    • Example Code:
      UPDATE employees
      SET salary = salary * 1.1
      WHERE department_id IN (SELECT department_id FROM departments WHERE department_name = 'Sales');
      
  7. Updating Related Records in MySQL with JOIN:

    • Description: JOIN can be used to update records based on related data in other tables.
    • Example Code:
      UPDATE products
      INNER JOIN categories ON products.category_id = categories.category_id
      SET products.price = products.price * 1.05
      WHERE categories.category_name = 'Electronics';
      
  8. Examples of Complex Updates with MySQL Multi-Table Association:

    • Description: Various examples showcasing more complex scenarios involving multi-table updates.
    • Example Code:
      UPDATE employees
      SET bonus = bonus + 500
      WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'New York')
        AND salary > 70000;
      
      UPDATE orders
      SET total_amount = total_amount * 1.1
      WHERE customer_id IN (SELECT customer_id FROM customers WHERE status = 'Platinum')
        AND order_date < '2023-01-01';