(在班级管理系统v1.1中显示班级信息也有实现)
一、什么是视图
¨ 视图是查看数据库表中数据的一种方法;
¨ 视图提供了存储预定义的查询语句作为数据库中的对象以备以后使用的能力;
¨ 视图只是一种逻辑对象,并不是物理对象,因为视图不占物理存储空间;
¨ 在视图中被查询的表称为视图的基表;
¨ 视图的内容包括:基表的列的子集或者行的子集;两个或者多个基表的联合;两个或者多个基表的连接;基表的统计汇总;另外一个视图的子集;视图和基表的混合。
视图是原始数据库数据的一种变换,是查看表中数据的另外一种方式。可以将视图看成是一个移动的窗口,通过它可以看到感兴趣的数据。
* 简单性。看到的就是需要的。视图不仅可以简化用户对数据的理解,也可以简化他们的操作。那些被经常使用的查询可以被定义为视图,从而使得用户不必为以后的操作每次指定全部的条件。
二、视图的优点
1.集中用户使用的数据;
2.掩码数据库的复杂性,视图把数据库设计的复杂性与用户屏蔽分开;
3.简化用户权限的管理;
4.为向其他应用程序输出而重新组织数据。
创建视图:
mysql> CREATE VIEW employee_deparment AS SELECT * FROM employee,department WHERE dept_no = d_no;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM employee_deparment;
+------+--------+-------+---------+----------+----------+-------------+------+--------+------------+
| e_no | e_name | e_sex | dept_no | e_job | e_salary | hire_date | d_no | d_name | d_location |
+------+--------+-------+---------+----------+----------+-------------+------+--------+------------+
| 1006 | 王二 | 1 | 10 | 产品策划 | 2450 | 2015-4-23 | 10 | 产品部 | 上海 |
| 2001 | 小明 | 1 | 10 | 界面设计 | 2500 | 2016-11-315 | 10 | 产品部 | 上海 |
| 1001 | 李强 | 1 | 20 | 会计 | 1600 | 2015-9-9 | 20 | 财务部 | 北京 |
| 1004 | 张阳 | 1 | 20 | 出纳 | 2975 | 2015-4-9 | 20 | 财务部 | 北京 |
| 1002 | 王刚 | 1 | 30 | HR | 1600 | 2014-10-9 | 30 | 人事部 | 广州 |
| 1003 | 珊珊 | 0 | 30 | HR | 1250 | 2014-11-7 | 30 | 人事部 | 广州 |
| 1008 | 何刚 | 1 | 30 | HR | 1600 | 2014-11-9 | 30 | 人事部 | 广州 |
| 1005 | 小丽 | 0 | 40 | 销售经理 | 2850 | 2015-2-10 | 40 | 销售部 | 成都 |
| 1007 | 小冬 | 1 | 40 | 销售经理 | 2750 | 2015-3-10 | 40 | 销售部 | 成都 |
+------+--------+-------+---------+----------+----------+-------------+------+--------+------------+
9 rows in set (0.00 sec)
mysql> SELECT e_name,e_no,e_salary,d_name FROM employee_deparment;
+--------+------+----------+--------+
| e_name | e_no | e_salary | d_name |
+--------+------+----------+--------+
| 王二 | 1006 | 2450 | 产品部 |
| 小明 | 2001 | 2500 | 产品部 |
| 李强 | 1001 | 1600 | 财务部 |
| 张阳 | 1004 | 2975 | 财务部 |
| 王刚 | 1002 | 1600 | 人事部 |
| 珊珊 | 1003 | 1250 | 人事部 |
| 何刚 | 1008 | 1600 | 人事部 |
| 小丽 | 1005 | 2850 | 销售部 |
| 小冬 | 1007 | 2750 | 销售部 |
+--------+------+----------+--------+
9 rows in set (0.00 sec)
mysql> CREATE VIEW e_dep_view (eno,ename,dname,salary,job) AS SELECT e_no,e_name,d_name,e_salary,e_job FROM employee,
-> department WHERE dept_no = d_no;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM e_dep_view;
+------+-------+--------+--------+----------+
| eno | ename | dname | salary | job |
+------+-------+--------+--------+----------+
| 1006 | 王二 | 产品部 | 2450 | 产品策划 |
| 2001 | 小明 | 产品部 | 2500 | 界面设计 |
| 1001 | 李强 | 财务部 | 1600 | 会计 |
| 1004 | 张阳 | 财务部 | 2975 | 出纳 |
| 1002 | 王刚 | 人事部 | 1600 | HR |
| 1003 | 珊珊 | 人事部 | 1250 | HR |
| 1008 | 何刚 | 人事部 | 1600 | HR |
| 1005 | 小丽 | 销售部 | 2850 | 销售经理 |
| 1007 | 小冬 | 销售部 | 2750 | 销售经理 |
+------+-------+--------+--------+----------+
9 rows in set (0.00 sec)
mysql> SELECT MAX(salary) FROM e_dep_view;
+-------------+
| MAX(salary) |
+-------------+
| 2975 |
+-------------+
1 row in set (0.00 sec)
mysql> SHOW CREATE VIEW e_dep_view;
+------------+---------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------+
---------------------+----------------------+
| View | Create View
|
character_set_client | collation_connection |
+------------+---------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------+
---------------------+----------------------+
| e_dep_view | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `e_dep_view` AS select `
mployee`.`e_no` AS `eno`,`employee`.`e_name` AS `ename`,`department`.`d_name` AS `dname`,`employee`.`e_salary` AS `sala
y`,`employee`.`e_job` AS `job` from (`employee` join `department`) where (`employee`.`dept_no` = `department`.`d_no`) |
gbk | gbk_chinese_ci |
+------------+---------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------+
---------------------+----------------------+
1 row in set (0.00 sec)
mysql> SHOW CREATE VIEW e_dep_view\G;
*************************** 1. row ***************************
View: e_dep_view
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `e_dep_view` AS s
lect `employee`.`e_no` AS `eno`,`employee`.`e_name` AS `ename`,`department`.`d_name` AS `dname`,`employee`.`e_salary` A
`salary`,`employee`.`e_job` AS `job` from (`employee` join `department`) where (`employee`.`dept_no` = `department`.`d
no`)
character_set_client: gbk
collation_connection: gbk_chinese_ci
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> SHOW VIEW STAUTE;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
for the right syntax to use near 'VIEW STAUTE' at line 1
mysql> SHOW VIEW statue;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
for the right syntax to use near 'VIEW statue' at line 1
mysql> SHOW VIEW statu;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
for the right syntax to use near 'VIEW statu' at line 1
mysql> SHOW VIEW status;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
for the right syntax to use near 'VIEW status' at line 1
mysql>