What is an UPDATE QUERY?
The SQL UPDATE statement is used to modify the data in tables’ records. A condition determines which rows are to be updated. The WHERE clause is used to specify conditions.
Syntax:
UPDATE table_name
SET column_name = expression
WHERE condition
where,
Here we have a Student table.
Student_Id | FirstName | LastName | Course |
---|---|---|---|
12 | Ada | Sharma | BCA |
22 | Rahul | Maurya | B.Des |
30 | James | Walker | M.Tech |
Query for updating only a single field in the Student table based on Student_Id.
UPDATE Student
SET LastName = 'Kumar'
WHERE Student_Id = '22'
Output:
Student_Id | FirstName | LastName | Course |
---|---|---|---|
12 | Ada | Sharma | BCA |
22 | Rahul | Kumar | B.Des |
30 | James | Walker | M.Tech |
Query for updating multiple values in a single statement based on the Student_Id.
UPDATE Student
SET FirstName='Adiba', COURSE='BBA'
where Student_Id=12;
Output:
Student_Id | FirstName | LastName | Course |
---|---|---|---|
12 | Adiba | Sharma | BBA |
22 | Rahul | Kumar | B.Des |
30 | James | Walker | M.Tech |
If we leave out the WHERE clause in the update query, all of the rows will be updated.
UPDATE Student
SET Course = 'MBA'
Output:
Student_Id | FirstName | LastName | Course |
---|---|---|---|
12 | Adiba | Sharma | MBA |
22 | Rahul | Kumar | MBA |
30 | James | Walker | MBA |
As we can notice, Course field for all three students is updated with “MBA”, due to the omission of where clause.
To increment an integer value with the UPDATE command, we use the SET clause with the + operator to add the desired value to the column’s existing value. Here’s an illustration:
Sr. | Item | Quantity |
1 | Candles | 89 |
2 | Balls | 230 |
3 | Lights | 122 |
UPDATE Product SET Quantity = Quantity+10;
After executing this query the quantity of all the items will get incremented by 10.
Note: also read about SQL: INSERT Query
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…