📜  sql update (1)

📅  最后修改于: 2023-12-03 15:05:18.021000             🧑  作者: Mango

SQL Update

If you are a programmer working with databases, then you must have heard about the SQL Update statement. In SQL, the Update statement is used to modify the existing data in a table.

Syntax

The basic syntax of the SQL Update statement is as follows:

UPDATE table_name
SET column1 = new_value1, column2 = new_value2, ...
WHERE condition;
  • table_name: The name of the table you want to modify.
  • column1, column2, ...: The columns you want to update.
  • new_value1, new_value2, ...: The new values you want to set for the columns.
  • WHERE clause: The conditions that must be met for the update to take place.
Example

Let's consider a simple example. Suppose we have a table named students with the following structure:

| id | name | age | grade | |----|---------|-----|-------| | 1 | John | 20 | A | | 2 | Jane | 22 | B | | 3 | Michael | 21 | A | | 4 | Sarah | 19 | C |

Let's say we want to update the grade of John from A to B. The SQL code for this would be:

UPDATE students
SET grade = 'B'
WHERE id = 1;

After executing this statement, the students table will look like this:

| id | name | age | grade | |----|---------|-----|-------| | 1 | John | 20 | B | | 2 | Jane | 22 | B | | 3 | Michael | 21 | A | | 4 | Sarah | 19 | C |

Conclusion

The SQL Update statement is a powerful tool for modifying data in a database table. It allows you to change the values in one or more columns of a table based on a condition. By understanding this statement and using it effectively, you can keep your database tables up-to-date and accurate.