SQL UPDATE Statement

beginner
12 min

SQL UPDATE Statement

Welcome to our SQL UPDATE Statement tutorial! In this comprehensive guide, we'll explore how to modify existing data in a database using the UPDATE statement. By the end, you'll be able to update data like a pro! 🎯

What is the SQL UPDATE Statement?

The SQL UPDATE statement is used to modify or update the existing records in a database table. It helps us change the values of specific columns for one or multiple records. 📝

Syntax of the UPDATE Statement

sql
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
  • table_name: The name of the table you want to update.
  • column1, column2: The names of the columns you want to update.
  • value1, value2: The new values you want to set for the respective columns.
  • condition: A condition that defines which rows should be updated.

Updating a Single Row

Let's update a single row with the UPDATE statement. Let's assume we have a table named users with columns id, name, and email.

sql
UPDATE users SET name = 'John Doe', email = 'johndoe@example.com' WHERE id = 1;

In this example, we're updating the name and email of the user with the ID 1. 💡 Note: Remember to use single quotes for strings.

Updating Multiple Rows

To update multiple rows, you can use the same syntax as above, but without a WHERE clause. All rows will be updated with the specified values.

sql
UPDATE users SET name = 'Updated User', email = 'updated@example.com';

Updating Rows with Conditions

You can also use conditions to update specific rows. Here's an example:

sql
UPDATE users SET email = 'new_email@example.com' WHERE email = 'old_email@example.com';

In this example, the email of all users with the current email old_email@example.com will be updated to new_email@example.com.

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of the SQL UPDATE statement?

SQL Data Types

Before we dive deeper into the UPDATE statement, let's quickly review some common SQL data types:

  • INT: Integer
  • FLOAT: Float
  • VARCHAR: Variable-length character string
  • DATE: Date and time
  • BOOLEAN: True or false

UPDATE Statement with Different Data Types

Now, let's see some examples using different data types.

Updating Integers

sql
UPDATE users SET age = 25 WHERE id = 1;

Updating Floats

sql
UPDATE users SET height = 1.75 WHERE id = 2;

Updating Variable-length Character Strings (VARCHAR)

sql
UPDATE users SET address = '123 Main St, Anytown' WHERE id = 3;

Updating Dates

sql
UPDATE users SET birthday = '1990-01-01' WHERE id = 4;

Updating Booleans

sql
UPDATE users SET is_active = TRUE WHERE id = 5;

Conclusion

In this SQL UPDATE Statement tutorial, we learned how to modify existing data in a database using the UPDATE statement. We covered updating a single row, multiple rows, and rows with conditions. Additionally, we reviewed some common SQL data types. ✅

Now that you understand the basics, practice updating data in different scenarios and explore more complex conditions to make your SQL skills shine!