Geek Slack

My SQL Tutorial
    About Lesson



    MySQL UPDATE Statement


    MySQL UPDATE Statement

    The UPDATE statement in MySQL is used to modify existing records in a table.

    Basic Syntax:

    Example: Basic UPDATE Syntax

    UPDATE table_name
    SET column1 = value1, column2 = value2, ...
    WHERE condition;

    This SQL command updates existing records in table_name by setting values for specified columns (column1, column2, …) based on a specified condition.

    Example Using UPDATE:

    Example: Updating Data

    UPDATE users
    SET email = 'newemail@example.com'
    WHERE user_id = 1;

    This SQL command updates the email of the user with user_id 1 in the users table to newemail@example.com.

    Updating Multiple Columns:

    Example: Updating Multiple Columns

    UPDATE products
    SET price = price * 1.1,
        stock = stock - 1
    WHERE product_id = 101;

    This SQL command updates the price of the product with product_id 101 in the products table by increasing it by 10% and decreases the stock by 1.

    Updating All Rows:

    Example: Updating All Rows

    UPDATE orders
    SET status = 'Cancelled';

    This SQL command updates the status column of all rows in the orders table to 'Cancelled', effectively cancelling all orders.

    Conclusion

    The UPDATE statement in MySQL allows for the modification of existing records in database tables, providing flexibility in updating specific columns or entire rows based on specified conditions.