Chapter 5SQL Tutorial~1 min read
UPDATE & DELETE
Data Update आणि Delete करणे
UPDATE statement existing rows बदलतो. DELETE rows काढतो. TRUNCATE सगळ्या rows काढतो (structure ठेवतो). DROP पूर्ण table काढतो. WHERE clause विसरला तर सगळ्या rows affected होतात!
📌
UPDATE आणि DELETE वापरण्यापूर्वी नेहमी SELECT करून check करा कोणते rows affected होतील! WHERE नाही दिले तर सगळ्या rows update/delete होतात.
UPDATE
UPDATE statement
sql
-- Single row update (id ने)
UPDATE students
SET marks = 90
WHERE id = 1;
-- Multiple columns update
UPDATE students
SET city = 'Mumbai', marks = 88
WHERE name = 'Rahul Patil';
-- Calculation ने update
UPDATE students
SET marks = marks + 5 -- सगळ्यांना 5 bonus marks
WHERE city = 'Pune';
-- Multiple rows update (condition नुसार)
UPDATE students
SET city = 'Pune'
WHERE city = 'Punes'; -- typo fix
-- ⚠️ WHERE नाही — सगळ्या rows update होतात!
-- UPDATE students SET marks = 0; -- DANGEROUS!DELETE
DELETE statement
sql
-- Specific row delete
DELETE FROM students WHERE id = 5;
-- Condition ने delete
DELETE FROM students WHERE marks < 30;
-- Multiple conditions
DELETE FROM students
WHERE city = 'Nashik' AND marks < 40;
-- ⚠️ WHERE नाही — सगळ्या rows delete होतात!
-- DELETE FROM students; -- DANGEROUS!TRUNCATE vs DROP
TRUNCATE and DROP
sql
-- TRUNCATE — सगळे rows काढतो, table structure ठेवतो
-- DELETE पेक्षा fast, rollback नाही!
TRUNCATE TABLE students;
-- DROP TABLE — पूर्ण table काढतो
DROP TABLE IF EXISTS students; -- IF EXISTS — error नाही
-- DROP DATABASE — पूर्ण database
DROP DATABASE IF EXISTS school;- ▸DELETE — specific rows काढतो, WHERE वापरतो, rollback शक्य
- ▸TRUNCATE — सगळे rows, fast, rollback नाही, id reset होतो
- ▸DROP TABLE — structure + data दोन्ही, permanent
✅ Key Points — लक्षात ठेवा
- ▸UPDATE table SET col=val WHERE condition
- ▸DELETE FROM table WHERE condition
- ▸WHERE विसरला तर सगळ्या rows affected — ALWAYS check!
- ▸TRUNCATE — fast empty, DROP — complete removal
- ▸UPDATE/DELETE आधी SELECT ने verify करा
0/10 chapters पूर्ण