Display the contents of a VIEW in MySQL?

MySQLMySQLi Database

Following is the syntax −

select * from yourViewName;

Let us first create a table −

mysql> create table DemoTable1388
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(40)
   -> );
Query OK, 0 rows affected (0.71 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1388(StudentName) values('Chris');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable1388(StudentName) values('Bob');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1388(StudentName) values('David');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1388(StudentName) values('Mike');
Query OK, 1 row affected (0.29 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1388;

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         1 | Chris       |
|         2 | Bob         |
|         3 | David       |
|         4 | Mike        |
+-----------+-------------+
4 rows in set (0.00 sec)

Following is the query to create view in MySQL −

mysql> create view view1388 as select *from DemoTable1388 where StudentId=3;
Query OK, 0 rows affected (0.13 sec)

Now, display the contents of view in MySQL −

mysql> select * from view1388;

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         3 | David       |
+-----------+-------------+
1 row in set (0.19 sec)
raja
Published on 11-Nov-2019 10:14:41
Advertisements