×

How to Update Table in SQL?

How to Update Table in SQL

Introduction

  • UPDATE query is used to update a record in a table.
  • UPDATE is a DML command, which operates on the data of the table and not on the structure of the table.
  • Using UPDATE Query, one can update all the records as well as update specific records within a table.
  • To update specific records within a table, UPDATE Query is used with the WHERE Clause.
  1. UPDATE Query to change a single record in a table

We can use an UPDATE query with the WHERE clause to change one particular column value of an entire record.

Syntax:

UPDATE TABLE_NAME SET column_name = value WHERE[condition];

Example:

First we will create a database with name “employeedb”. Then in that database we will create a table “employee” and insert records into the table. We will consider this table and database for all the subsequent examples.

 mysql> USE employeedb;
 Database changed
 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Nikita   | 30000      |
 |      2 | Riddhi   | 25000      |
 |      3 | Nayan    | 45000      |
 |      4 | Shruti   | 15000      |
 |      5 | Anurati  | 50000      |
 +--------+----------+------------+
 5 rows in set (0.06 sec) 
Update Table in SQL

We will update the employee (Emp_ID = 3) with a new salary.

 mysql> UPDATE employee SET Emp_Salary = 40000 WHERE Emp_ID = 3;
 Query OK, 1 row affected (0.07 sec)
 Rows matched: 1  Changed: 1  Warnings: 0
 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Nikita   | 30000      |
 |      2 | Riddhi   | 25000      |
 |      3 | Nayan    | 40000      |
 |      4 | Shruti   | 15000      |
 |      5 | Anurati  | 50000      |
 +--------+----------+------------+
 5 rows in set (0.00 sec) 
Update Table in SQL

The salary of an employee (Emp_ID=3) is now changed to 40,000 from 45,000.

  • UPDATE Query to change multiple records in a table

We can use an UPDATE query with the WHERE clause to change multiple column values of a particular record.

Syntax:

UPDATE TABLE_NAME SET column_name1 = value1, column_name2 = value2...., column_nameN = valueN WHERE [condition];

Example:

We will update the employee (Emp_ID = 1) with a new salary and name.

 mysql> UPDATE employee SET Emp_Salary = 40000, Emp_Name = "Mayuri" WHERE Emp_ID = 1;
 Query OK, 1 row affected (0.07 sec)
 Rows matched: 1  Changed: 1  Warnings: 0
 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Mayuri   | 40000      |
 |      2 | Riddhi   | 25000      |
 |      3 | Nayan    | 45000      |
 |      4 | Shruti   | 15000      |
 |      5 | Anurati  | 50000      |
 +--------+----------+------------+
 5 rows in set (0.00 sec) 
Update Table in SQL

The salary and name of an employee (Emp_ID=1) is now changed to “40,000” and “Mayuri” respectively.

  • UPDATE Query to change all records in a table

We can use an UPDATE query omitting the WHERE clause to change one or more than one column values for all the records within a table.

Syntax:

UPDATE TABLE_NAME SET column_name1 = value1, [column_name2 = value2...., column_nameN = valueN];

Example:

We will update all the employees with a new salary and name.

 mysql> UPDATE employee SET Emp_Salary = 40000, Emp_Name = "Mayuri";
 Query OK, 5 rows affected (0.08 sec)
 Rows matched: 5  Changed: 5  Warnings: 0
 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Mayuri   | 40000      |
 |      2 | Mayuri   | 40000      |
 |      3 | Mayuri   | 40000      |
 |      4 | Mayuri   | 40000      |
 |      5 | Mayuri   | 40000      |
 +--------+----------+------------+
 5 rows in set (0.00 sec) 
Update Table in SQL

Since, we have not specified the condition in the query on the basis of which records are to be updated so, our query will change the column values of all the records present in a table. The salary and name of all the employees is now changed to “40,000” and “Mayuri” respectively.


Related Topics

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.