← Back to PostgreSQL Course | Chapter 10: Advanced Features | Lesson 1 of 7

Views क्या हैं

एक view एक saved query है जिसे आप एक table की तरह इस्तेमाल करते हैं।

In this page:

  1. Views
Syntax
sql
CREATE VIEW view_name AS
SELECT columns FROM table_name WHERE condition;

SELECT * FROM view_name;

Views

CREATE VIEW name AS SELECT ... query store करता है, data नहीं। Views complex joins simplify करते हैं, security के लिए columns hide करते हैं, और tables बदलने पर एक stable interface देते हैं।

Simple views updatable हो सकते हैं।

CREATE OR REPLACE VIEW एक definition update करता है, और DROP VIEW इसे remove करता है।

Note: सिर्फ कुछ columns expose करने के लिए underlying table की बजाय एक view को access grant करें।

उदाहरण: Views

sql
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, dept TEXT, salary INTEGER);
INSERT INTO employees VALUES (1,'Ada','eng',100),(2,'Bob','eng',80),(3,'Cy','ops',60);
CREATE VIEW dept_summary AS SELECT dept, COUNT(*) AS staff, AVG(salary) AS avg_salary FROM employees GROUP BY dept;
SELECT * FROM dept_summary ORDER BY dept;
UPDATE employees SET salary = 120 WHERE id = 2;
SELECT * FROM dept_summary ORDER BY dept;

-- Output:
-- dept | staff | avg_salary
-- eng | 2 | 90.0
-- ops | 1 | 60.0
-- dept | staff | avg_salary
-- eng | 2 | 110.0
-- ops | 1 | 60.0
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. यह मान लेना कि views data store करते हैं
  2. कई views stack करना और performance hurt करना
  3. यह भूल जाना कि एक view table का current data reflect करता है
चैप्टर सारांश
  • एक view एक saved query है
  • कोई data store नहीं होता
  • Joins simplify करता है और column access limit करता है
  • हमेशा current data दिखाता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.