Updating Existing Data
Hey there! π Today, we’re going to talk about updating existing data in MySQL using the UPDATE statement. Imagine you have a notebook π where you wrote down your friend’s phone number, but now they have a new one. Instead of erasing the old number and writing a new one, you just update it. Thatβs exactly what we do in a database with UPDATE!
Let’s dive in! π
π What is the UPDATE Statement?
The UPDATE statement in MySQL allows you to modify existing records in a table. It helps in changing values without deleting or inserting new rows.
π Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
π‘ Key Points to Remember
- The
UPDATEstatement modifies existing data in a table. - The
SETkeyword defines which columns should be updated and what new values should be assigned. - The
WHEREclause is important because it tells MySQL which rows to update. If you forget it, all rows in the table will be updated! π±
π― Example 1: Updating a Single Record
πΉ Scenario
Let’s say we have a table called students, which stores information about students.
π Table: students
| id | name | age | grade |
|---|---|---|---|
| 1 | John | 14 | 8 |
| 2 | Alice | 15 | 9 |
| 3 | Bob | 14 | 8 |
Now, John just turned 15. Let’s update his age in the database.
π Query
UPDATE students
SET age = 15
WHERE id = 1;
π Output
| id | name | age | grade |
|---|---|---|---|
| 1 | John | 15 | 8 |
| 2 | Alice | 15 | 9 |
| 3 | Bob | 14 | 8 |
β Now John’s age is updated in the table.
π― Example 2: Updating Multiple Columns
πΉ Scenario
Alice just got promoted to grade 10, and she also had her birthday. We need to update both age and grade.
π Query
UPDATE students
SET age = 16, grade = 10
WHERE id = 2;
π Output
| id | name | age | grade |
|---|---|---|---|
| 1 | John | 15 | 8 |
| 2 | Alice | 16 | 10 |
| 3 | Bob | 14 | 8 |
β Alice’s age and grade are updated successfully!
π― Example 3: Updating Multiple Rows
πΉ Scenario
All students in grade 8 are moving to grade 9.
π Query
UPDATE students
SET grade = 9
WHERE grade = 8;
π Output
| id | name | age | grade |
|---|---|---|---|
| 1 | John | 15 | 9 |
| 2 | Alice | 16 | 10 |
| 3 | Bob | 14 | 9 |
β Now, all students in grade 8 have been promoted to grade 9.
β οΈ Important: Be Careful with UPDATE!
If you forget the WHERE clause, you update every row in the table! π±
β Example of a Dangerous Update
UPDATE students
SET grade = 12;
π¨ This will make every student on the table move to grade 12! Always use the WHERE clause carefully.
π οΈ Practice Questions
Test your understanding with these exercises. Try writing the SQL queries on your own!
πΉ Question 1:
The employees table has the following data:
| emp_id | name | salary |
|---|---|---|
| 101 | Sam | 50000 |
| 102 | Alex | 55000 |
| 103 | Ryan | 52000 |
Write an SQL query to increase Alexβs salary to 60000.
π‘ Expected Output:
| emp_id | name | salary |
|---|---|---|
| 101 | Sam | 50000 |
| 102 | Alex | 60000 |
| 103 | Ryan | 52000 |
πΉ Question 2:
The products table contains:
| product_id | name | price |
|---|---|---|
| 1 | Laptop | 800 |
| 2 | Phone | 500 |
| 3 | Tablet | 300 |
Write a query to increase the price of all products by 10%.
π‘ Expected Output:
| product_id | name | price |
|---|---|---|
| 1 | Laptop | 880 |
| 2 | Phone | 550 |
| 3 | Tablet | 330 |
π€ Interview Questions with Answers
1οΈβ£ What is the UPDATE statement in SQL?
Answer:
The UPDATE statement in SQL is used to modify existing records in a table. It updates one or more columns of a row or multiple rows based on a specified condition.
2οΈβ£ How can you update multiple columns in a table?
Answer:
To update multiple columns, separate each column assignment with a comma.
Example:
UPDATE employees
SET salary = 60000, department = 'HR'
WHERE emp_id = 101;
3οΈβ£ What happens if you forget the WHERE clause in an UPDATE statement?
Answer:
If you forget the WHERE clause, every row in the table will be updated, which can cause serious data issues.
4οΈβ£ Can we update multiple rows in one query?
Answer:
Yes, we can update multiple rows using a condition in the WHERE clause.
Example:
UPDATE students
SET grade = 10
WHERE grade = 9;
5οΈβ£ How do you increase a numeric value in a column?
Answer:
You can use arithmetic operations like this:
UPDATE products
SET price = price * 1.10;
This increases the price by 10%.
π― Summary
- The
UPDATEstatement modifies existing records. - Use
SETto define the new values. - Always use the
WHEREclause to avoid unwanted updates. - You can update single or multiple rows at once.
- Be cautious when updating data to prevent accidental changes.
π― Final Challenge
Try writing an SQL query to:
- Reduce the price of all
booksin alibrarytable by 5%. - Change the department of all employees in “Sales” to “Marketing” in an
employeestable.