11. MySQL Part 2: UPDATE, ALTER, DELETE, Keys, Constraints and Users
11.1 UPDATE
UPDATE astitvat aslelya rows madhe value badalto. WHERE shivay UPDATE mhanje sagalya rows badaltat – he lakshat theva.
USE cybercourse;
-- 1. first SELECT the rows you plan to change
SELECT student_id, name, fees_paid FROM students WHERE student_id = 6;
-- 2. then UPDATE with the same WHERE
UPDATE students SET fees_paid = 15000 WHERE student_id = 6;
-- several columns at once
UPDATE students SET city = 'Pune', age = 24 WHERE student_id = 4;
-- calculated update for a group
UPDATE students SET fees_paid = fees_paid + 1000 WHERE city = 'Nagpur';
-- update with a join: add 5 bonus marks for the Networking course
UPDATE enrollments e JOIN courses c ON c.course_id = e.course_id
SET e.score = e.score + 5 WHERE c.course_name = 'Networking';
SELECT ROW_COUNT(); -- how many rows the last statement changed
Why this matters for security
Injection is not only about reading. A vulnerable "update profile" page that builds UPDATE users SET bio='...' WHERE id=5 from input can be abused to change other columns (such as role='admin') or other users' rows. Prepared statements and server-side authorization checks prevent both.
Ravindra Bagale's Tip
Khup students UPDATE lihitat aani WHERE lihinyapurvi chukun Enter dabtat – purna table badalto! Majha niyam: aadhi SELECT ... WHERE ... lihaa, rows bagha, mag tech WHERE copy karun UPDATE banva. Aani mahatvacha data asel tar aadhi transaction suru kara (section 11.2).
Practice task
Increase the fees of all Nashik students by 500, change Zoya's city to Pune, and add 2 marks to every score below 60. Check each change with SELECT before and after.