Which SQL statement is used to update data in a database?a)UPDATEb)DEL...
The UPDATE statement is used to modify existing data in a database table.
View all questions of this test
Which SQL statement is used to update data in a database?a)UPDATEb)DEL...
UPDATE Statement in SQL
The UPDATE statement is used in SQL to modify or update existing data in a database table. It allows you to change the values of one or more columns in a table based on specified conditions. The syntax of the UPDATE statement is as follows:
```
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
```
Let's break down the components of the UPDATE statement:
- table_name: Specifies the name of the table that you want to update.
- SET: Indicates the columns and their new values that you want to update.
- column1, column2: Names of the columns that you want to update.
- value1, value2: New values that you want to assign to the specified columns.
- WHERE: Specifies the condition(s) that must be met for the update to occur. It is optional, but without it, all rows in the table will be updated.
Example:
Let's consider a simple example to understand the usage of the UPDATE statement. Suppose we have a table named "employees" with the following structure:
```
+----+-----------+----------+
| ID | Name | Salary |
+----+-----------+----------+
| 1 | John | 5000 |
| 2 | Sarah | 6000 |
| 3 | Michael | 4500 |
+----+-----------+----------+
```
If we want to update the salary of employee with ID 1 to 5500, we can use the following UPDATE statement:
```
UPDATE employees
SET Salary = 5500
WHERE ID = 1;
```
After executing this statement, the table will be updated as follows:
```
+----+-----------+----------+
| ID | Name | Salary |
+----+-----------+----------+
| 1 | John | 5500 |
| 2 | Sarah | 6000 |
| 3 | Michael | 4500 |
+----+-----------+----------+
```
Conclusion
The UPDATE statement in SQL is used to modify existing data in a database table. It allows you to change the values of one or more columns based on specified conditions. Understanding how to use the UPDATE statement is essential for manipulating and updating data effectively in a database.