How do I see what a MySQL view is made of?


Following is the syntax −

show create view yourViewName;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> StudentName varchar(20)
   -> );
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| StudentName |
+-------------+
| Chris       |
| Robert      |
| David       |
+-------------+
3 rows in set (0.00 sec)

Following is the query to create view for a table −

mysql> CREATE VIEW view_DemoTable AS SELECT StudentName from DemoTable;
Query OK, 0 rows affected (0.12 sec)

Following is the query to see what a MySQL view is made of −

mysql> show create view view_DemoTable;

This will produce the following output −

+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View              | Create View                                                                                                                                                             | character_set_client | collation_connection |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| view_DemoTable     | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL
SECURITY DEFINER VIEW `view_DemoTable` AS select `DemoTable`.`StudentName` AS
`StudentName` from `DemoTable` | utf8 | utf8_general_ci |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

Let us check view records −

mysql> select *from view_DemoTable;

This will produce the following output −

+-------------+
| StudentName |
+-------------+
| Chris       |
| Robert      |
| David       |
+-------------+
3 rows in set (0.05 sec)
raja
Published on 11-Jul-2019 11:20:30
Advertisements