Saturday, 25 April 2015

update query in mysql

Update

update query is used to modify / change the columns value of a table 


UPDATE Books
SET  InStock= InStock+10;


Here Books is table name. InStock is column name. It update the value of  InStock by adding 10

--------------------------------------------------------------------------------

UPDATE Orders
SET Quantity=2
WHERE OrderID=1001;


Here update affects only where OrderID=1001

-----------------------------------------------------------------------------------------
UPDATE  LOW_PRIORITY Books
SET InStock=InStock+10
WHERE InStock<30;


--------------------------------------------------------------------------------------------

An UPDATE statement can be further qualified by the use of the ORDER BY clause
and the LIMIT clause. 

UPDATE Orders
SET Quantity=Quantity+1
WHERE BookID=103
ORDER BY DateOrdered DESC
LIMIT 5;


--------------------------------------------------------------------------------


UPDATE Books, Orders
SET Books.InStock=Books.InStock-Orders.Quantity
WHERE Books.BookID=Orders.BookID
AND Orders.OrderID=1002;



In this exercise, you used an UPDATE statement to modify data in multiple InnoDB tables
even though this method is normally not recommended. 

No comments:

Post a Comment