SQL - UPDATE Query (2024)

SQL - UPDATE Query (1)

SQL - UPDATE Query (2)

Table of content
  • The SQL UPDATE Statement
  • Update Multiple ROWS and COLUMNS

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

The SQL UPDATE Statement

The SQL UPDATE Statement is used to modify the existing records in a table. This statement is a part of Data Manipulation Language (DML), as it only modifies the data present in a table without affecting the table's structure.

To filter records that needs to be modified, you can use a WHERE clause with UPDATE statement. Using a WHERE clause, you can either update a single row or multiple rows.

Since it only interacts with the data of a table, the SQL UPDATE statement needs to used cautiously. If the rows to be modified aren't selected properly, all the rows in the table will be affected and the correct table data is either lost or needs to be reinserted.

The SQL UPDATE statement makes use of locks on each row while modifying them in a table, and once the row is modified, the lock is released. Therefore, it can either make changes to a single row or multiple rows with a single query.

Syntax

The basic syntax of the SQL UPDATE statement with a WHERE clause is as follows −

UPDATE table_nameSET column1 = value1, column2 = value2,..., columnN = valueNWHERE [condition];

You can combine N number of conditions using the AND or the OR operators.

Example

Assume we have created a table named CUSTOMERS using the CREATE TABLE statement as shown below −

CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID));

Now, insert values into this table using the INSERT statement as follows −

INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),(2, 'Khilan', 25, 'Delhi', 1500.00 ),(3, 'Kaushik', 23, 'Kota', 2000.00 ),(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),(5, 'Hardik', 27, 'Bhopal', 8500.00 ),(6, 'Komal', 22, 'Hyderabad', 4500.00 ),(7, 'Muffy', 24, 'Indore', 10000.00 );

The table will be created as −

IDNAMEAGEADDRESSSALARY
1Ramesh32Ahmedabad2000.00
2Khilan25Delhi1500.00
3Kaushik23Kota2000.00
4Chaitali25Mumbai6500.00
5Hardik27Bhopal8500.00
6Komal22Hyderabad4500.00
7Muffy24Indore10000.00

The following query will update the ADDRESS for a customer whose ID number is 6 in the table.

UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;

Output

The query produces the following output −

Query OK, 1 row affected (0.13 sec)Rows matched: 1 Changed: 1 Warnings: 0

Verification

To verify whether the records of the table are modified or not, use the following SELECT query below −

SELECT * FROM CUSTOMERS WHERE ID=6;

Now, the CUSTOMERS table would have the following records −

IDNAMEAGEADDRESSSALARY
6Komal22Pune4500.00

Update Multiple ROWS and COLUMNS

Using SQL UPDATE statement, multiple rows and columns in a table can also be updated. To update multiple rows, specify the condition in a WHERE clause such that only the required rows would satisfy it.

However, to update multiple columns, set the new values to all the columns that need to be updated. In this case, using the WHERE clause would narrow down the records of the table and not using the clause would change all the values in these columns.

Syntax

Following is the syntax to update multiple rows and columns −

UPDATE table_nameSET column_name1 = new_value, column_name2 = new_value...WHERE condition(s)

Example

If you want to modify all the AGE and the SALARY column values in the CUSTOMERS table, you do not need to use the WHERE clause as the UPDATE query would be enough. Following query increases the age of all the customers by 5 years and adds 3000 to all the salary values −

UPDATE CUSTOMERS SET AGE = AGE+5, SALARY = SALARY+3000;

Output

The query produces the following output −

Query OK, 7 rows affected (0.12 sec)Rows matched: 7 Changed: 7 Warnings: 0

Verification

To verify whether the records of the table are modified or not, use the following SELECT query below −

SELECT * FROM CUSTOMERS;

Now, CUSTOMERS table would have the following records −

IDNAMEAGEADDRESSSALARY
1Ramesh37Ahmedabad5000.00
2Khilan30Delhi4500.00
3Kaushik28Kota5000.00
4Chaitali30Mumbai9500.00
5Hardik32Bhopal11500.00
6Komal27Pune7500.00
7Muffy29Indore13000.00

Example

But, if you want to modify the ADDRESS and the SALARY columns of selected records in the CUSTOMERS table, you need to specify a condition to filter the records to be modified, using the WHERE clause, as shown in the following query −

UPDATE CUSTOMERS SET ADDRESS = 'Pune', SALARY = 1000.00 WHERE NAME = 'Ramesh';

Output

This query produces the following output −

Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0

Verification

To verify whether the records of the table are modified or not, use the following SELECT query below −

SELECT * FROM CUSTOMERS WHERE NAME = 'Ramesh';

Now, CUSTOMERS table would have the following records −

IDNAMEAGEADDRESSSALARY
1Ramesh37Pune1000.00

Advertisem*nts

';adpushup.triggerAd(ad_id); });

SQL - UPDATE Query (2024)

FAQs

How to check if an UPDATE is successful in SQL? ›

The value of @@ERROR is checked for any indication of an error, and @@ROWCOUNT is used to ensure that the update was successfully applied to a row in the table.

Which query is faster, insert or UPDATE? ›

INSERT is faster than an UPDATE statement provided a database is properly optimized.

How do I optimize an UPDATE query in SQL? ›

Here are a few tips for SQL Server Optimizing the updates on large data volumes.
  1. Removing the index on the column to be updated.
  2. Executing the update in smaller batches.
  3. Disabling Delete triggers.
  4. Replacing the Update statement with a Bulk-Insert operation.
Feb 8, 2023

How to pass multiple values in UPDATE query in SQL? ›

Syntax. WHERE column_name IN('column_name1', 'column_name2'); As you can see, to update value of a column based on multiple conditions, use the CASE clause with WHERE clause, creating multiple conditions inside a single statement and updating the values on multiple conditions.

How to check if an update query was successful in MySQL? ›

Is there any way we can find whether the update query is executed? UPDATE table SET table.name = "Anthony" WHERE table.id = 3; IF (queryUpdated) THEN SELECT 1 AS updated; ELSE SELECT 0 AS updated; END IF; mysql. update.

How can I check the progress of a SQL query? ›

You can also view access the live query execution plan by right-clicking on a selected query in Management Studio and then click Include Live Query Statistics. Now execute the query. The live query plan displays the overall query progress and the run-time execution statistics (e.g. elapsed time, progress, etc.)

What are the best practices for SQL update? ›

RULES & BEST PRACTICE FOR SQL UPDATE STATEMENT

Do not update all the table rows at once. Always filter rows to update using the WHERE clause. Table relationships, e.g., foreign key constraints, must be considered while updating a table. Major updates should only be performed during low peak usage times.

Do indexes make updates faster? ›

Indexes are great for faster data lookup, however, this comes with some drawbacks. The main drawbacks of using indexes are as follows: Slower Writes: Indexes slow down INSERT , UPDATE , and DELETE queries.

Is delete faster than update? ›

Obviously, the answer varies based on what database you are using, but UPDATE can always be implemented faster than DELETE+INSERT.

How to increase the performance of an update query in MySQL? ›

To improve UPDATE query performance, we perform updates in batches, meaning we perform many smaller updates instead of one big update. To improve DELETE query performance, we switch the DELETE query to TRUNCATE . TRUNCATE deletes all rows in a table.

How do I optimize a slow SQL query? ›

12 Ways to Optimize SQL Queries
  1. Use indexes effectively. ...
  2. Avoid SELECT queries. ...
  3. Reduce the use of wildcard characters. ...
  4. Use appropriate data types and layouts. ...
  5. Avoid redundant or unnecessary data retrieval. ...
  6. Use EXIST() instead of COUNT() queries. ...
  7. Avoid subqueries. ...
  8. Make use of cloud database-specific features.

Can we UPDATE multiple rows in a single UPDATE statement? ›

The UPDATE statement can be used to update multiple rows at once.

Can we UPDATE two columns at a time in SQL? ›

Multiple columns can be updated at once by separating each column-value pair with a comma.

What is the syntax for UPDATE in SQL? ›

The UPDATE statement has the following form: UPDATE table_name SET column_name = value [, column_name = value ...] [ WHERE condition]

How do I check for SQL updates? ›

You can also check the update history of your SQL Server by using SSMS or the Windows Update History tool. You should also run some tests on your databases and applications to ensure that they are functioning properly and that there are no errors or performance issues.

How do you check if an insert was successful in SQL? ›

Also, INSERT will either succeed or fail, so you can check the @@ROWCOUNT right after inserting. If it's more than 0, then the insert succeeded, if @@ERROR <> 0 it fails. Also, if @@ERROR = 0 after insert, it was successful. What is the list of the SQL statements required to end the transaction?

How to check update statistics status in SQL Server? ›

Right-click on the statistics and go to properties. It opens the statistics properties and shows the statistics columns and last update date for the particular statistics.

How to check if an update query was successful in Oracle? ›

How would you check if something was really updated? The only way I know how to do this is to query the table and then compare input parameters with the query to see if anything changed. If someone updates data and changes it compared to previous state I need to return 1 or 0 if update did not change anything.

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6412

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.