SQL UPDATE Statement

How to update data in a Table?

Answer is to use UPDATE query. It is used with WHERE clause to avoid accidental update in all the columns rather than updates required for one column only.

Syntax:

UPDATE "tableName"
set columnName1 = value1,
columnName2 = value2,
....  ,
columnNameN = valueN
WHERE columnName = value;

Parameters :

tableName : Name of the table in which we are going to insert data.

column1, column2, columnN : Columns name of the table which will have specified data in it.

value1, value2, valueN : Values which are going to stored in table for e.g. column1 will have value1, column2 will have value2 and so on columnN will have value valueN.

columnName, value : They will be used as criteria i.e columnName in the table containing specified value in it.

UPDATE with one column : We will be using the same table which we have created in the CREATE TABLE and data which is added during insert query.

Using Select to see all the records before update.

SELECT * FROM `employee`;

before-update-query-execution-example1

UPDATE  `employee`  SET  `name` = ‘sachin’  WHERE `id` = 3;

Using Select to see all the records after update.

after-update-query-execution-example1

UPDATE with some sql operators is below :

Using Select to see all the records before update.

SELECT * FROM `employee`;

selecting all records

UPDATE  `employee`  SET  `salary` = 60000  WHERE `age` >= 30 ;

Using Select to see all the records after update.

after-update-query-execution-example2

Example 2 :

UPDATE  `employee`  SET  `salary` = 70000  WHERE `age` = 35 and name=’Leena’ ;

after-update-query-execution-example3

Note : we can use multiple combination of operators in WHERE clause.

Note : 1. You should add WHERE clause to avoid accidental updates of values in all columns.

2. Please remember about the datatypes of your column to avoid errors while updating data in table.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *