Notes

`UPDATE` Query in DBMS (MySQL) [ English ]

< Prev Next >

Introduction

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.

Definition

The UPDATE query is a Data Manipulation Language (DML) command used to modify or update existing records in a table.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Where:

How It Works

When the UPDATE command is executed:

  1. The DBMS locates the specified table.
  2. It identifies the rows that satisfy the WHERE condition.
  3. The specified column values are modified.
  4. The table is updated with the new values.

Example Table

Suppose we have a students table.

id name age course
1 Rahul 20 BCA
2 Anita 21 BBA
3 Rohit 19 BCA

Example 1: Updating a Specific Record

UPDATE students
SET course = 'BCom'
WHERE id = 2;

Result

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.

Example 2: Updating Multiple Columns

UPDATE students
SET age = 22, course = 'BBA'
WHERE id = 1;

This query updates two columns for the student with id = 1.

Updating All Records

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.

Important Notes

Key Points

Summary

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.

< Prev Next >