Standard (virtual) view — stores only the query. Re-evaluated every access.
Materialised view — actually stores the result rows on disk for speed, and must be refreshed. (Different trade-off; the default CREATE VIEW is virtual.)
UPDATE cheap_items SET price = 90 WHERE item_id = 7; rewrites cleanly to
UPDATE items SET price = 90 WHERE item_id = 7; — one view row ↔ one base row, and price is a real base column. ✅
Now a non-updatable view:
CREATE VIEW dept_stats ASSELECT dept_id, AVG(salary) AS avg_payFROM employees GROUP BY dept_id;
Try UPDATE dept_stats SET avg_pay = 60000 WHERE dept_id = 3;Which employee's salary do you change, and to what, so the average becomes 60000? Infinitely many answers → the engine rejects it. ❌ That's the rule, derived — not memorised.
Imagine a magic window stuck on a big messy room. Through the window you only see the toys that are red (your WHERE). The window holds no toys itself — it just shows you a slice of the real room. If you reach through the window and move a toy, you're moving the real toy in the real room. But you can't reach through the window to change a sign that says "average toy height" — there's no single toy to move to make an average true. That's exactly why some views you can edit and some you can't.
Dekho, ek view basically ek saved SQL query hota hai jo dikhne mein table jaisa lagta hai, par uske paas apna koi data nahi hota. Jab bhi tum view se SELECT karte ho, database peeche se wahi original query dobara chala deta hai aur fresh result dikha deta hai. Isiliye view hamesha base table ke saath sync mein rehta hai — base table change karo, view automatically update dikhega. Banane ke liye simple: CREATE VIEW naam AS SELECT ....
View use karne ke teen bade faayde: simplicity (badi complex join ko ek chhote naam ke peeche chhupa do), security (intern ko sirf name, dept dikhao, salary column view mein dalo hi mat), aur stability (schema badle to sirf view fix karo, app code safe). Yeh exam aur real projects dono mein bahut kaam aata hai.
Ab asli twist — updatable view. Tum view pe UPDATE/INSERT tabhi kar sakte ho jab database har view-row ko exactly ek base-table row se match kar paaye. Agar view mein GROUP BY, AVG/SUM, DISTINCT, UNION ya multi-table JOIN hai, to mapping unique nahi rehti — socho "average salary 60000 kar do" matlab kis employee ki salary badloge? Koi clear jawab nahi, isliye DB mana kar deta hai. Yaad rakhne ka mantra: GADU-JS wale views read-only.
Ek aakhri trap: filtered view (WHERE price<100) mein agar tum 150 price ka row insert karoge to wo base table mein chala jaata hai par view mein dikhega hi nahi (kyunki filter fail). Isko rokne ke liye WITH CHECK OPTION lagao — phir galat row reject ho jaata hai. Aur complex view pe likhna ho to INSTEAD OF trigger se DB ko khud bata do kaunsa base table change karna hai.