博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql视图初探
阅读量:4455 次
发布时间:2019-06-07

本文共 5094 字,大约阅读时间需要 16 分钟。

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 existingview 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 doesexist, CREATE OR REPLACE VIEW replaces it.For information about restrictions on view use, seehttp://dev.mysql.com/doc/refman/8.0/en/view-restrictions.html.The select_statement is a SELECT statement that provides the definitionof the view. (Selecting from the view selects, in effect, using theSELECT statement.) The select_statement can select from base tables orother 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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> 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: 0mysql> 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 themmysql>

删除视图

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

转载于:https://www.cnblogs.com/JuncaiF/p/11315637.html

你可能感兴趣的文章
USACO 3.1 Contact
查看>>
Office之什么是高内聚低耦合
查看>>
一些奇怪的问题求回答
查看>>
这些年踩过的坑
查看>>
iOS开发拓展篇——如何把项目托管到GitHub
查看>>
性能优化之数据库优化
查看>>
类的继承、菱形继承、派生、多态
查看>>
mysql约束
查看>>
javascript鼠标及键盘事件总结及案例
查看>>
mysql表之间的关系及级联操作
查看>>
mac 搭建virtualenv的那些坑
查看>>
多路复用IO模型
查看>>
并发、串行、并行及多道技术原理
查看>>
hashlib、pickle、hmac、logging模块使用
查看>>
javascript常用知识点总结
查看>>
2019秋招复习笔记--数据库基本操作
查看>>
2019秋招复习笔试--手写代码
查看>>
2019秋招复习笔记--智力题
查看>>
MySQL学习笔记
查看>>
2019秋招面试复习 项目重点提问
查看>>