当前位置:网站首页>Mysql database (26): View

Mysql database (26): View

2022-06-10 12:58:00 51CTO

View view

1、 Create view

The essence of view is SQL Instructions

Basic grammar

--  It can be single table data , It can also be a connection query , Union query or subquery 
create view  View name  as select  Instructions 

     
  • 1.
  • 2.

Example

mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name   | class_id | age  | gender |
+----+--------+----------+------+--------+
|  1 |  Liu bei    |        1 |   18 |      2 |
|  2 |  Li Si    |        1 |   19 |      1 |
|  3 |  Wang Wu    |     NULL |   20 |      2 |
|  4 |  Zhang Fei    |     NULL |   21 |      1 |
|  5 |  Guan yu    |     NULL |   22 |      2 |
|  6 |  Cao Cao    |        1 |   20 |   NULL |
+----+--------+----------+------+--------+
6 rows in set (0.00 sec)

mysql> select * from my_class;
+----+--------+
| id | name   |
+----+--------+
|  1 |  Class one    |
|  3 |  Class three    |
+----+--------+
2 rows in set (0.00 sec)

--  Create view 
create view student_class_view as 
select s.*, c.name class_name from my_student as s 
left join my_class as c
on s.class_id = c.id;

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

View view structure , The view itself is a virtual table , So all operations on tables apply to views

show tables;
show create view <table_name>;
desc <table_name>;

     
  • 1.
  • 2.
  • 3.

2、 Use view

The view is a virtual table , You can directly operate the view as a table , The view itself has no data , Is temporary select Statement to get the corresponding result , Views are mainly used for query operations

Basic grammar

select  Field list  from  View name  [ Clause ];

     
  • 1.

Example

select * from student_class_view;
+----+--------+----------+------+--------+------------+
| id | name   | class_id | age  | gender | class_name |
+----+--------+----------+------+--------+------------+
|  1 |  Liu bei    |        1 |   18 |      2 |  Class one        |
|  2 |  Li Si    |        1 |   19 |      1 |  Class one        |
|  3 |  Wang Wu    |     NULL |   20 |      2 | NULL       |
|  4 |  Zhang Fei    |     NULL |   21 |      1 | NULL       |
|  5 |  Guan yu    |     NULL |   22 |      2 | NULL       |
|  6 |  Cao Cao    |        1 |   20 |   NULL |  Class one        |
+----+--------+----------+------+--------+------------+
6 rows in set (0.00 sec)

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

3、 Modify the view

The essence is to modify the query statement corresponding to the view

Basic grammar

alter view  View name  as select  sentence ;

     
  • 1.

Example

alter view student_class_view as 
select s.id, s.name, c.name class_name from my_student as s 
left join my_class as c
on s.class_id = c.id;

mysql> select * from student_class_view;
+----+--------+------------+
| id | name   | class_name |
+----+--------+------------+
|  1 |  Liu bei    |  Class one        |
|  2 |  Li Si    |  Class one        |
|  3 |  Wang Wu    | NULL       |
|  4 |  Zhang Fei    | NULL       |
|  5 |  Guan yu    | NULL       |
|  6 |  Cao Cao    |  Class one        |
+----+--------+------------+

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

4、 Delete view

Basic grammar

drop view  View name ;

     
  • 1.

Example

drop view student_class_view;

     
  • 1.
原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101233451423.html