Monday, April 30, 2012

SQL UPDATE

Hello Friends welcome to this post and today I will be discussing about the SQL UPDATE query.

Well from the last post we know how the INSERT INTO statement works, ie, we can insert or add a record to a Table in specific columns . . . .

So lets assume we have a Table named Employee where last time we did an INSERT INTO and inserted some data to specific columns, and the columns where we haven't inserted by default the value becomes Null.

So UPDATE query helps us to update those values also with other values, we have to be very careful with the Update statement, I will show you why.

Lets see our Table First:

PID    Name        Designation
222    Whiskey       Admin
444    Onty            Admin
111    Gaurav         Admin
333    Kumaar       Admin
555    Rishabh       Admin
Null    Rose           Admin

Now our Manager comes and says Hey I have just demoted Whiskey from Admin to Moderator, and I also want the PID of Miss Rose to be 666. Update the Records . . .

So our Query would be:

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


Ex:

UPDATE Employee
SET Designation="Moderator"
WHERE PID=222 AND Name="Whiskey"


With this our result-set would be:

PID    Name        Designation
222    Whiskey       Moderator
444    Onty            Admin
111    Gaurav         Admin
333    Kumaar       Admin
555    Rishabh       Admin
Null    Rose           Admin

And next,

UPDATE Employee
SET PID=666
WHERE Name="Rose" AND Designation="Admin"


and our result-set would be:

PID    Name        Designation
222    Whiskey       Moderator
444    Onty            Admin
111    Gaurav         Admin
333    Kumaar       Admin
555    Rishabh       Admin
666    Rose           Admin

Be careful with the UPDATE and Do Not forget to use the WHERE clause otherwise the Whole column would be update,

Ex:

UPDATE Employee
SET PID=666


With this, the whole Employee table have PID as 666.

Thats all for this post, hope this was informative and Do not forget to check my next post.

Thank You!

0 comments:

Post a Comment