← Back to MySQL Course | Chapter 14: Views & Indexes | Lesson 2 of 5

UPDATE और DROP View

एक view update करना आपकी window की instructions को उसे तोड़े बिना फिर से लिखने जैसा है। DROP VIEW बस window हटा देता है।
Syntax
sql
CREATE OR REPLACE VIEW view_name AS
SELECT columns FROM table_name;

ALTER VIEW view_name AS
SELECT columns FROM table_name;

DROP VIEW [IF EXISTS] view_name;

एक View Replace करना

CREATE OR REPLACE VIEW आपको एक मौजूदा view की underlying query को एक step में redefine करने देता है, बिना पहले इसे drop करके अलग से recreate किए — requirements बदलने और view को नई definition चाहिए होने पर उपयोगी।

उदाहरण: Replacing a View

sql
CREATE TABLE orders (id INT, total INT);
INSERT INTO orders VALUES (1, 500), (2, 30);
CREATE VIEW big_orders AS SELECT * FROM orders WHERE total > 100;
CREATE OR REPLACE VIEW big_orders AS SELECT * FROM orders WHERE total > 200;
SELECT * FROM big_orders;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

View Structures Alter करना

ALTER VIEW view के name और उस पर दी गई privileges को intact रखते हुए view जो query चलाता है उसे update करता है, जो permissions दोबारा assign करने से बचाता है जो drop करके recreate करना ज़रूरी बना देता।

उदाहरण: Altering View Structures

sql
CREATE TABLE orders (id INT, total INT);
INSERT INTO orders VALUES (1, 500);
CREATE VIEW order_view AS SELECT id FROM orders;
ALTER VIEW order_view AS SELECT id, total FROM orders;
SELECT * FROM order_view;

एक View के through Data Update करना

अगर एक view बिना aggregation या joins के एक single underlying table से बना है, आप actually खुद view के against UPDATE statements चला सकते हैं, और MySQL इसके पीछे की real table पर change apply कर देगा।

उदाहरण: Updating Data Through a View

sql
CREATE TABLE customers (id INT, name TEXT);
INSERT INTO customers VALUES (1, 'Amit');
CREATE VIEW customer_view AS SELECT * FROM customers;
UPDATE customer_view SET name = 'Amit Kumar' WHERE id = 1;
SELECT * FROM customers;

WITH CHECK OPTION Clause

किसी view की definition में WITH CHECK OPTION add करना किसी भी ऐसे update को block करता है जो एक row produce करता है जो अब view के अपने WHERE clause से match न करे, आपके edit करते ही एक row को view से चुपचाप 'गायब' होने से रोकते हुए।

उदाहरण: The WITH CHECK OPTION Clause

sql
CREATE TABLE orders (id INT, total INT);
INSERT INTO orders VALUES (1, 500);
CREATE VIEW big_orders AS SELECT * FROM orders WHERE total > 100 WITH CHECK OPTION;
-- Blocked: would drop the row below the view's own 100 threshold
-- UPDATE big_orders SET total = 50 WHERE id = 1;
SELECT * FROM big_orders;

DROP VIEW से Views हटाना

DROP VIEW database से permanently एक view definition हटा देता है। क्योंकि एक view अपना कोई data store नहीं करता, इसे drop करना underlying tables को कभी नहीं छूता — यह बस saved query delete कर देता है।

उदाहरण: Removing Views with DROP VIEW

sql
CREATE TABLE orders (id INT, total INT);
CREATE VIEW big_orders AS SELECT * FROM orders WHERE total > 100;
DROP VIEW big_orders;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. GROUP BY, DISTINCT, या aggregates इस्तेमाल करने वाले एक view के through INSERT या UPDATE करने की कोशिश करना, जिसकी अनुमति नहीं है।
  2. WITH CHECK OPTION भूल जाना, ताकि एक update किसी row को view के filter से बाहर ले जाए और यह गायब हो जाए।
  3. किसी view को बदलना चाहते हुए DROP VIEW इस्तेमाल करना, जबकि CREATE OR REPLACE VIEW या ALTER VIEW name और privileges रखता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.