Monday, June 1, 2020

Update Statement


The UPDATE statement in SQL is used to modify the existing data of a table in database. We can update single columns as well as multiple columns and single record as well as multiple records using UPDATE statement as per our requirement.

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

table_name: name of the table
SET: Set statement is used to set new values to the particular column.
column1: name of first, second, third column....
value1: new value for first, second, third column....
WHERE: WHERE clause is used to select the rows for which the columns are needed to be updated. If we have not used the WHERE clause then the columns in all the rows will be updated.
condition: condition to select the rows for which the
values of columns need to be updated.

Example:
ID
NAME
ADDRESS
DEPARTMENT
101
Rajesh
Ghaziabad
IT
102
Swati
Delhi
Non IT
103
Manoj
Noida
IT
104
Sachin
Noida
IT

  •          Update address to Delhi of ID 104:


SCRIPT:

    UPDATE EMPLOYEE
         SET ADDRESS = ‘Delhi’
    WHERE ID = 104;

Now EMPLOYEE table have following records:
ID
NAME
ADDRESS
DEPARTMENT
101
Rajesh
Ghaziabad
IT
102
Swati
Delhi
Non IT
103
Manoj
Noida
IT
104
Sachin
Delhi
IT

  • Update all employee address to Delhi:

SCRIPT:

    UPDATE EMPLOYEE
         SET ADDRESS = ‘Delhi’;

Now EMPLOYEE table have following records:
ID
NAME
ADDRESS
DEPARTMENT
101
Rajesh
Delhi
IT
102
Swati
Delhi
Non IT
103
Manoj
Delhi
IT
104
Sachin
Delhi
IT


  •          Update address to Delhi of ID 104 and also change Department to “Non IT”:

 SCRIPT:

   UPDATE EMPLOYEE
         SET ADDRESS = ‘Delhi’,
                DEPARTMENT = ‘Non IT’
   WHERE ID = 104;

Now EMPLOYEE table have following records:
ID
NAME
ADDRESS
DEPARTMENT
101
Rajesh
Ghaziabad
IT
102
Swati
Delhi
Non IT
103
Manoj
Noida
IT
104
Sachin
Delhi
Non IT

No comments:

Post a Comment