In a Database Management System (DBMS), data stored in tables may need to be modified or corrected over time. For example, a student's course may change, or an employee’s salary may be updated. The UPDATE statement is used to modify existing records in a table.
This command allows users to change the value of one or more columns in specific rows of a table.
The UPDATE query is a Data Manipulation Language (DML) command used to modify or update existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Where:
When the UPDATE command is executed:
Suppose we have a students table.
| id | name | age | course |
|---|---|---|---|
| 1 | Rahul | 20 | BCA |
| 2 | Anita | 21 | BBA |
| 3 | Rohit | 19 | BCA |
UPDATE students
SET course = 'BCom'
WHERE id = 2;
| id | name | age | course |
|---|---|---|---|
| 1 | Rahul | 20 | BCA |
| 2 | Anita | 21 | BCom |
| 3 | Rohit | 19 | BCA |
The course of the student with id = 2 is updated from BBA to BCom.
UPDATE students
SET age = 22, course = 'BBA'
WHERE id = 1;
This query updates two columns for the student with id = 1.
If the WHERE clause is omitted, all records in the table will be updated.
Example:
UPDATE students
SET course = 'BCA';
This command will update the course for every student in the table.
⚠ Warning: Omitting the WHERE clause may unintentionally modify all records.
UPDATE command modifies existing data without creating new records.UPDATE is a DML command.The UPDATE command is used to modify existing data stored in a database table. By specifying the columns and conditions, users can update specific records efficiently. Proper use of the WHERE clause ensures that only the intended records are modified.