SQL as A Data Manipulation Language : UPDATE

The UPDATE statement is used to modify the existing records in a table. We also need to use the WHERE clause with the UPDATE statement to update the selected rows, otherwise all the rows would be affected. The basic syntax of the UPDATE statement with WHERE clause is as follows :
 UPDATE table_name SET column_name1 = value, column_name2 = value, ... column_nameN = value WHERE [condition];
At above we can also combine two more conditions using AND or the OR operators.

Example :
 SQL > SELECT * FROM employee;

 ENAME           HIREDATE   SAL   PIN     PH
 -------------   ---------  ----- ----------   -------------
 Priya           23-DEC-17       3000  978900     8975436445
 Nidhi           23-DEC-17       3000  123454     9876543210
 Dinesh          21-DEC-17       3400  768754     8798765676
 AJAY            21-DEC-17      10000  207521     9287656544
 SQL> UPDATE employee SET sal = 10000, pin = 978910 WHERE ename = 'Priya';
After running the above query, employee table will be :
 ENAME            HIREDATE       SAL   PIN        PH
--------------    ---------    ------  ----------   --------------
 Priya            23-DEC-17     10000  978910      8975436445
 Nidhi            23-DEC-17      3000  123454      9876543210
 Dinesh           21-DEC-17      3400  768754      8798765676
 AJAY             21-DEC-17     10000  207521      9287656544
If we want to modify all the sal column values in the employee table, then we do not need to use the WHERE clause, for example :
 SQL> UPDATE employee SET sal = 10000;
Now the employee table will be :
 ENAME            HIREDATE       SAL   PIN        PH
--------------    ---------    ------   ----------  --------------
 Priya            23-DEC-17     10000  978910      8975436445
 Nidhi            23-DEC-17      3000  123454      9876543210
 Dinesh           21-DEC-17      3400  768754      8798765676
 AJAY             21-DEC-17     10000  207521      9287656544



Next Topic :

No comments:

Post a Comment