当前位置:网站首页>MySQL数据库(26):视图 view
MySQL数据库(26):视图 view
2022-06-10 09:32:00 【彭世瑜】
视图 view
1、创建视图
视图本质是SQL指令
基本语法
-- 可以是单表数据,也可以是连接查询,联合查询或子查询
create view 视图名字 as select 指令
示例
mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 1 | 刘备 | 1 | 18 | 2 |
| 2 | 李四 | 1 | 19 | 1 |
| 3 | 王五 | NULL | 20 | 2 |
| 4 | 张飞 | NULL | 21 | 1 |
| 5 | 关羽 | NULL | 22 | 2 |
| 6 | 曹操 | 1 | 20 | NULL |
+----+--------+----------+------+--------+
6 rows in set (0.00 sec)
mysql> select * from my_class;
+----+--------+
| id | name |
+----+--------+
| 1 | 一班 |
| 3 | 三班 |
+----+--------+
2 rows in set (0.00 sec)
-- 创建视图
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;
查看视图结构,视图本身是虚拟表,所以关于表的操作都适用于视图
show tables;
show create view <table_name>;
desc <table_name>;
2、使用视图
视图是一张虚拟表,可以直接把视图当做表操作,视图本身没有数据,是临时执行select语句得到对应的结果,视图主要用于查询操作
基本语法
select 字段列表 from 视图名字 [子句];
示例
select * from student_class_view;
+----+--------+----------+------+--------+------------+
| id | name | class_id | age | gender | class_name |
+----+--------+----------+------+--------+------------+
| 1 | 刘备 | 1 | 18 | 2 | 一班 |
| 2 | 李四 | 1 | 19 | 1 | 一班 |
| 3 | 王五 | NULL | 20 | 2 | NULL |
| 4 | 张飞 | NULL | 21 | 1 | NULL |
| 5 | 关羽 | NULL | 22 | 2 | NULL |
| 6 | 曹操 | 1 | 20 | NULL | 一班 |
+----+--------+----------+------+--------+------------+
6 rows in set (0.00 sec)
3、修改视图
本质是修改视图对应的查语句
基本语法
alter view 视图名字 as select 语句;
示例
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 | 刘备 | 一班 |
| 2 | 李四 | 一班 |
| 3 | 王五 | NULL |
| 4 | 张飞 | NULL |
| 5 | 关羽 | NULL |
| 6 | 曹操 | 一班 |
+----+--------+------------+
4、删除视图
基本语法
drop view 视图名字;
示例
drop view student_class_view;
边栏推荐
- 在 Kubernetes 中基于 StatefulSet 部署 MySQL(上)
- How to Understand Your Data With Descriptive Statistics
- BlockingQueue、SynchronousQueue、ArrayBlockingQueue、ReentrantLock、Semaphore、公平与非公平
- PHP语言的课程体系了解怎么样呢
- 阿里巴巴数字化转型的启示
- Case sharing | digital intelligence upgrading: the transformation of Red Dragonfly (Part 2)
- No module named ‘pyautogui‘
- Inspiration de la transformation numérique d'Alibaba
- C language define variable parameter__ VA_ ARGS__ And__ VA_ ARGS__ Use of
- FormBuilder
猜你喜欢

Leetcode level 19 - valid parentheses

CF1304C Air Conditioner(思维)

Switch the formatting plug-in of vscode

Printk learning part 1: basic principles

工业数字化转型中的数据治理

Share the 2022 China supply chain digital upgrading industry research report (PDF attached)
![QQ wechat enables continuous message sending [code implementation]](/img/a1/9ceb1bd855fdfbb9926d00ac8ac151.png)
QQ wechat enables continuous message sending [code implementation]

【timm】一个基于pytorch的图像模型库

SOLR Advanced Query Application - - Grouper les requêtes par champ

Linear Regression
随机推荐
小bai月赛49 E 禅(DP)
JS obtient l'heure actuelle
利用requests库爬取网页获取数据
PHP语言的课程体系了解怎么样呢
牛客月赛50 D 生日(计算贡献)
FormBuilder
Error reporting and solution of request calling get method
10 revelations from the success of digital transformation
QQ wechat enables continuous message sending [code implementation]
CF1304C Air Conditioner(思维)
Summary of MATLAB error reporting
After Zotero beta 6.0 is installed, the problem that the built-in PDF reader cannot be used is solved
Method of adding status bar in MFC window
Oracle dual 表生成多行伪记录
Why should the R & D effectiveness team of Internet companies be independent? When is independence?
股票问题 - 121. 买卖股票的最佳时机
YOLOX: Exceeding YOLO Series in 2021
Alignment HR_ MySQL storage engine, so it is
GNSS定位资料收集
Redis的配置优化