mysql视图初探

官方例子如下,从官方的例子就可以看出来视图就是提供一种快捷查询。用视图来查询一些常用的结果。

mysql> help create view;
Name: 'CREATE VIEW'
Description:
Syntax:
CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION] The CREATE VIEW statement creates a new view, or replaces an existing
view if the OR REPLACE clause is given. If the view does not exist,
CREATE OR REPLACE VIEW is the same as CREATE VIEW. If the view does
exist, CREATE OR REPLACE VIEW replaces it. For information about restrictions on view use, see
http://dev.mysql.com/doc/refman/8.0/en/view-restrictions.html. The select_statement is a SELECT statement that provides the definition
of the view. (Selecting from the view selects, in effect, using the
SELECT statement.) The select_statement can select from base tables or
other views.
...
mysql> CREATE TABLE t (qty INT, price INT);
mysql> INSERT INTO t VALUES(3, 50);
mysql> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql> SELECT * FROM v;
+------+-------+-------+
| qty | price | value |
+------+-------+-------+
| 3 | 50 | 150 |
+------+-------+-------+

官网例子使用

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| city |
| country |
| dept |
| emp |
| t1 |
+----------------+
5 rows in set (0.00 sec) mysql> create table goodsview (num int,price int);
Query OK, 0 rows affected (0.25 sec) mysql> insert into goodsview values (3,50),(5,60);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from goodsview;
+------+-------+
| num | price |
+------+-------+
| 3 | 50 |
| 5 | 60 |
+------+-------+
2 rows in set (0.00 sec) mysql> create view gview as select num,price ,num*price as value from goodsview;
Query OK, 0 rows affected (0.03 sec) mysql> select * from gview;
+------+-------+-------+
| num | price | value |
+------+-------+-------+
| 3 | 50 | 150 |
| 5 | 60 | 300 |
+------+-------+-------+
2 rows in set (0.00 sec)

表名替换后查询view

表名替换后原有的view肯定不可用

mysql> alter  table goodsview rename to goodviewtable;
Query OK, 0 rows affected (0.11 sec) mysql> show create table goodviewtable;
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| goodviewtable | CREATE TABLE `goodviewtable` (
`num` int(11) DEFAULT NULL,
`price` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> show create table gview;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View | Create View | character_set_client | collation_connection |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| gview | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `gview` AS select `goodsview`.`num` AS `num`,`goodsview`.`price` AS `price`,(`goodsview`.`num` * `goodsview`.`price`) AS `value` from `goodsview` | latin1 | latin1_swedish_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set, 1 warning (0.00 sec) mysql> select * from gview;
ERROR 1356 (HY000): View 'test.gview' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
mysql>

删除视图

mysql> drop view gview;
Query OK, 0 rows affected (0.03 sec) mysql>

最新文章

  1. Enable Cross-Origin Requests in Asp.Net WebApi 2[Reprint]
  2. OGG for DB2 i 12.2发布
  3. bzoj1067 降雨量&&vijos1265 暴风雨
  4. 【 D3.js 入门系列 --- 3 】 做一个简单的图表!
  5. Spark GraphX学习资料
  6. C++类的构造、拷贝构造、析构函数等
  7. C#学习笔记之线程 - 同步上下文
  8. Java_Iterator-------迭代器配合Listarray使用,具有更多的功能(转载)
  9. swift 语法 - 以及学习资料
  10. 性能监控工具javamelody与spring的集成
  11. Android中倒计时代码
  12. 前端学PHP之正则表达式基础语法
  13. ubuntu:通过封装验证码类库一步步安装php的gd扩展
  14. [20190328]简单探究sql语句相关mutexes.txt
  15. Linux inotify功能及实现原理【转】
  16. Expo大作战(十)--expo中的App Icon,expo中的Assets,expo中的ErrorHandling错误处理
  17. Eclipse中执行Maven命令时控制台输出乱码
  18. [转]Java中的JavaBean类
  19. 【程序练习】——ini格式转换为xml格式
  20. Go 收藏

热门文章

  1. JS中的数组创建,初始化
  2. Codeforces 1311F Moving Points
  3. 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!
  4. PTA的Python练习题(十二)-第4章-7 统计学生平均成绩与及格人数
  5. mysql_pw 指令 数据库创建过程
  6. Codeforces Round #611 (Div. 3) C
  7. 19市赛 树状数组 第k个小的糖果
  8. C++11常用特性介绍——constexpr变量
  9. Live2d技术
  10. 安装插件报错error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++