Create View In MySQL In Hindi

📔 : MySQL 🔗

पिछले topic में आपने View के concept के बारे में समझा और जाना , इस topic हम सीखेंगे कि कैसे view को create और list करते हैं।

MySQL Create View

view create करने के लिए CREATE VIEW command का use किया जाता है , जिसका syntax कुछ इस तरह से होता है -

CREATE [OR REPLACE] VIEW view_name [column_list] AS query;

Explanation

  1. सबसे पहले आपको CREATE VIEW के बाद view का नाम specify करना है हालाँकि MySQL में views और tables same database और same namespace में होते हैं , इसलिए ध्यान रहे view का नाम unique और table name से अलग होना चाहिए।

  2. OR REPLACE , का use करके existing view को replace कर सकते हैं। हालाँकि यह optional होता है।

  3. इसके बाद columns list को specify कर सकते हैं , by default columns को आपके select query से use किया जाता है।

  4. और finally AS keyword के बाद आपको query को लिख देना है।

By default create किया गया view आपके current database में होता है लेकिन view_name के साथ database का name specify (database.view_name) करके आप different database में view create कर सकते हैं।

MySQL CREATE VIEW example

Suppose, हमारे पास users name की एक table है, और एक दूसरी table भी है user_orders नाम की जिसमे user के orders हैं कि user ने कौन सा product किस price पर buy किया है , और कुछ इस तरह से records मौजूद हैं।

तो हर particular user ने कितने products buy किये और उनकी total cost क्या है , के लिए view create करेंगे -

CREATE OR REPLACE VIEW userorders AS SELECT users.id, users.name, GROUP_CONCAT(user_orders.product) AS buy_products , SUM(user_orders.total) AS total_order FROM users LEFT JOIN user_orders ON user_orders.user_id=users.id GROUP BY users.id;
Query OK, 0 rows affected (0.078 sec)

Query OK, होने का मतलब है view create हो गया है।

MySQL List All Views

जैसा कि आपने अभी पढ़ा कि views और tables same namespace में होते हैं , जिसका मतलब है हम उन्हें SHOW TABLES statement से list कर सकते हैं।

SHOW TABLES;
+---------------------+
| Tables_in_tutorials |
+---------------------+
| students            |
| user_orders         |
| userorders          |
| users               |
+---------------------+

Example में आप देख सकते हैं कि userorders एक view है जो कि बाकी tables के साथ list हो रहा है , हालाँकि SHOW FULL TABLES statement का use करके table type भी show करा सकते हैं।

For Example

SHOW FULL TABLES;
+---------------------+------------+
| Tables_in_tutorials | Table_type |
+---------------------+------------+
| students            | BASE TABLE |
| user_orders         | BASE TABLE |
| userorders          | VIEW       |
| users               | BASE TABLE |
+---------------------+------------+

इसके अलावा आप same query पर WHERE Clause use करके records filter भी कर सकते हैं , जैसे -

SHOW FULL TABLES WHERE Table_type="View";
+---------------------+------------+
| Tables_in_tutorials | Table_type |
+---------------------+------------+
| userorders          | VIEW       |
+---------------------+------------+

View create करने के लिए ऊपर दिए गए example में 2 tables को JOIN किया गया है , आप अपनी need के according single table , sub query या किसी भी valid SQL query का use करके view create कर सकते हैं।

So I hope, आपको MySQL में view के बारे में अच्छे से समझ आया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers