>

Tuesday, August 18, 2015

SQL UPDATE STATEMENT



It is a DML(Data Manipulation Language)statement, which is used to update a row of a table,i.e. data within table. 

Syntax :

UPDATE table_name SET column_name = value [WHERE condition];
here WHERE CLAUSE is optional.

Lets see an example,

Assume, We have below table : STUDENT,



Now, we want to update age of PAL from 10 to 15 then,

SQL> UPDATE student SET age=15;


Ooops , It changes all three students age to 15 which is wrong.

Note : If you want to update specific data of row then we have to use WHERE CLAUSE.

Below is the right one,

SQL> UPDATE student SET age=15 WHERE name=PAL;


See the above result,now only age of PAL is changed.

Note : If you want to change date or name means.date or char/varchar datatype column's data you have to use single quote.

If we want to change name of PAL to DHRUVIN then,

SQL> UPDATE student SET name='DHRUVIN' where sid=02;


We also update multiple columns at a time,

Here,we update DHRUVIN to PAL AND his age 15 to 10 same as before,

SQL>UPDATE student SET name='PAL',age=10 where sid=02;