ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Update in MySql

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='7' AND `tutorial_submenu`='39' AND `tutorial_status`=1 LIMIT 1

Update in MySql

UPDATE in MySQL

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


1. Basic Syntax

UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;

πŸ”Ή Important Notes:
βœ… Always use WHERE to avoid updating all records.
βœ… You can update multiple columns at once.


2. Example Table: employees

emp_idnamedepartmentsalary
1AliceIT6000
2BobHR5000
3CharlieIT5500


3. Updating a Single Column

UPDATE employeesSET salary = 6500WHERE emp_id = 1;

βœ… Changes Alice’s salary to 6500.


4. Updating Multiple Columns

UPDATE employeesSET department = 'Finance', salary = 7000WHERE emp_id = 2;

βœ… Changes Bob's department to "Finance" and salary to 7000.


5. Updating Multiple Rows

UPDATE employeesSET salary = salary + 500WHERE department = 'IT';

βœ… Increases salary by 500 for all IT employees.


6. Updating All Rows (Use with Caution ⚠️)

UPDATE employeesSET salary = 6000;

βœ… Changes salary for ALL employees! (Use WHERE to avoid this.)


7. Using ORDER BY & LIMIT

UPDATE employeesSET salary = 7500ORDER BY salary ASCLIMIT 1;

βœ… Updates only the employee with the lowest salary.


8. Using UPDATE with JOIN

UPDATE employees eJOIN departments d ON e.department = d.dept_nameSET e.salary = e.salary + 1000WHERE d.dept_id = 2;

βœ… Increases salary by 1000 for employees in department ID 2.


9. Checking Changes with SELECT

After running an UPDATE, you can verify the changes using:

SELECT * FROM employees;


10. Using Transactions (To Undo Changes)

START TRANSACTION;UPDATE employees SET salary = 8000 WHERE emp_id = 3;ROLLBACK; -- Undo the changeCOMMIT; -- Save the change

βœ… Use ROLLBACK to undo an accidental update.


Key Takeaways

βœ… Use WHERE to avoid updating all records.
βœ… You can update one or multiple columns at once.
βœ… Use ORDER BY & LIMIT to control updates.
βœ… Use JOIN for updating data from multiple tables.
βœ… Use START TRANSACTION and ROLLBACK to prevent mistakes.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql