从为知笔记粘过来格式不太对,没有修改了
视图是从一个或者多个表中导出来的表,是一种虚拟存在的表,视图就像一个窗口,通过它可以看到系统专门提供的数据。
视图可以使用户的操作方便,保证并且数据库系统的安全。
内容:
视图的作用
创建视图
查看视图
修改视图
更新视图
删除视图
视图简介
视图从已经存在的表中导出,还可以从已经存在的视图导出,数据库只保存视图的定义,而没有存放视图的数据。视图的数据还是保存在原来的表中。视图的数据依赖于原来的表,一旦原来的表发生改变,视图的数据也相应变化。
如果需要经常查询多个表的制定字段的数据,可以在这些表上建立一个视图
MySQL的视图不支持输入参数的功能。
视图的作用:可以起到筛选器的作用,那些对用户没有用或者用户没有权限了解的信息可以直接屏蔽掉,归纳为:
使操作简化,增加数据的安全性,提高表的逻辑独立性
创建视图
mysql通过create view实现创建视图,语法:
create
[algorithm
=
{undifined|merge|temptable}]
view
view_name [(properties)]
as
select
*
[
with
[
CASCADED
|
LOCAL
]
check
option
];
解析:
algorithm是可选参数,表示视图选择的算法,undefined表示mysql自动选择算法,merge表示将使用视图的语句与视图定义合并,使视图的定义部分取代语句的对应部分,temptable表示视图的结构保存到临时表,然后使用临时表执行语句。
view_name表示要创建的视图的名字。
properties是可选参数,指定视图中各属性的名词,默认与select中查询的相同。
select是一个完整的查询语句从某个表中查询某些满足条件的记录并导入视图中。
with check option是可选参数,表示更新视图是要保证在视图的权限范围之内。
最好使用with cascaded check option参数
权限查询:在mysql数据库的user表中保存着权限信息,使用如下语句查询
select
select_priv, create_view_priv
from
mysql.
user
whrer
user
=
'username'
;
其中‘username’是数据库用户的用户名。
查询结果:
mysql
>
select
select_priv, create_view_priv
from
mysql.
user
where
user
=
'root'
;
+ -------------+------------------+
| select_priv | create_view_priv |
+ -------------+------------------+
| Y | Y |
| Y | Y |
+ -------------+------------------+
2 rows in set ( 0 . 09 sec)
+ -------------+------------------+
| select_priv | create_view_priv |
+ -------------+------------------+
| Y | Y |
| Y | Y |
+ -------------+------------------+
2 rows in set ( 0 . 09 sec)
在单表创建视图:
mysql
>
use login;
Database changed
mysql > select * from login;
+ -----+----------+----------------------------------+
| uid | username | password |
+ -----+----------+----------------------------------+
| 1 | test | 098f6bcd4621d373cade4e832627b4f6 |
| 2 | test1 | 5a105e8b9d40e1329780d62ea2265d8a |
| 3 | test2 | ad0234829205b9033196ba818f7a872b |
| 4 | user | ee11cbb19052e40b07aac0ca060c23ee |
| 5 | testk | f2fc2720249d97db37e2a5a3330baa4e |
+ -----+----------+----------------------------------+
5 rows in set ( 0 . 05 sec)
mysql > create view login_view as select * from login where uid > = 2 and uid < = 4 ;
Query OK, 0 rows affected ( 0 . 12 sec)
mysql > desc login_view;
+ ----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+ ----------+-------------+------+-----+---------+-------+
| uid | int ( 11 ) | NO | | 0 | |
| username | varchar ( 32 ) | NO | | NULL | |
| password | varchar ( 32 ) | NO | | NULL | |
+ ----------+-------------+------+-----+---------+-------+
3 rows in set ( 0 . 07 sec)
mysql > select * from login_view;
+ -----+----------+----------------------------------+
| uid | username | password |
+ -----+----------+----------------------------------+
| 2 | test1 | 5a105e8b9d40e1329780d62ea2265d8a |
| 3 | test2 | ad0234829205b9033196ba818f7a872b |
| 4 | user | ee11cbb19052e40b07aac0ca060c23ee |
+ -----+----------+----------------------------------+
3 rows in set ( 0 . 04 sec)
Database changed
mysql > select * from login;
+ -----+----------+----------------------------------+
| uid | username | password |
+ -----+----------+----------------------------------+
| 1 | test | 098f6bcd4621d373cade4e832627b4f6 |
| 2 | test1 | 5a105e8b9d40e1329780d62ea2265d8a |
| 3 | test2 | ad0234829205b9033196ba818f7a872b |
| 4 | user | ee11cbb19052e40b07aac0ca060c23ee |
| 5 | testk | f2fc2720249d97db37e2a5a3330baa4e |
+ -----+----------+----------------------------------+
5 rows in set ( 0 . 05 sec)
mysql > create view login_view as select * from login where uid > = 2 and uid < = 4 ;
Query OK, 0 rows affected ( 0 . 12 sec)
mysql > desc login_view;
+ ----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+ ----------+-------------+------+-----+---------+-------+
| uid | int ( 11 ) | NO | | 0 | |
| username | varchar ( 32 ) | NO | | NULL | |
| password | varchar ( 32 ) | NO | | NULL | |
+ ----------+-------------+------+-----+---------+-------+
3 rows in set ( 0 . 07 sec)
mysql > select * from login_view;
+ -----+----------+----------------------------------+
| uid | username | password |
+ -----+----------+----------------------------------+
| 2 | test1 | 5a105e8b9d40e1329780d62ea2265d8a |
| 3 | test2 | ad0234829205b9033196ba818f7a872b |
| 4 | user | ee11cbb19052e40b07aac0ca060c23ee |
+ -----+----------+----------------------------------+
3 rows in set ( 0 . 04 sec)
在多表上建立视图
在多表创建视图类似单表操作,只在select语句使用多表查询即可。
查看视图
1:使用desc语句
2:show table status语句查询视图基本信息。
show table status like 'view_name';
mysql
>
show
table
status
like
'login_view'
\
G
;
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
Name : login_view
Engine: NULL
Version : NULL
Row_format: NULL
Rows : NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation : NULL
Checksum: NULL
Create_options: NULL
Comment : VIEW
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
Name : login_view
Engine: NULL
Version : NULL
Row_format: NULL
Rows : NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation : NULL
Checksum: NULL
Create_options: NULL
Comment : VIEW
这条语句用于查看表信息:
mysql
>
show
table
status
like
'login'
\
G
;
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
Name : login
Engine: InnoDB
Version : 10
Row_format: Compact
Rows : 5
Avg_row_length: 3276
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 7340032
Auto_increment: 6
Create_time: 2012 - 10 - 28 18 : 28 : 57
Update_time: NULL
Check_time: NULL
Collation : utf8_general_ci
Checksum: NULL
Create_options:
Comment :
1 row in set ( 0 . 01 sec)
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
Name : login
Engine: InnoDB
Version : 10
Row_format: Compact
Rows : 5
Avg_row_length: 3276
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 7340032
Auto_increment: 6
Create_time: 2012 - 10 - 28 18 : 28 : 57
Update_time: NULL
Check_time: NULL
Collation : utf8_general_ci
Checksum: NULL
Create_options:
Comment :
1 row in set ( 0 . 01 sec)
从上面两个查询结构可以直观看出视图和表的区别。
虽然show table status可以查看视图的基本信息,但是不推荐使用,因为能提供的信息太少。
3.show create view语句查看视图信息
mysql
>
show
create
view
login_view \
G
;
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
View : login_view
Create View : CREATE ALGORITHM = UNDEFINED DEFINER = `root`@`localhost` SQL SECURITY DEFINER VIE
W `login_view` AS select `login`.`uid` AS `uid`,`login`.`username` AS `username`,`login`.` password `
AS ` password ` from `login` where ((`login`.`uid` > = 2 ) and (`login`.`uid` < = 4 ))
character_set_client: gbk
collation_connection: gbk_chinese_ci
1 row in set ( 0 .00 sec)
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
View : login_view
Create View : CREATE ALGORITHM = UNDEFINED DEFINER = `root`@`localhost` SQL SECURITY DEFINER VIE
W `login_view` AS select `login`.`uid` AS `uid`,`login`.`username` AS `username`,`login`.` password `
AS ` password ` from `login` where ((`login`.`uid` > = 2 ) and (`login`.`uid` < = 4 ))
character_set_client: gbk
collation_connection: gbk_chinese_ci
1 row in set ( 0 .00 sec)
4:在view表中查看视图的详细信息。
select
*
from
information_schema.views \
G
;
information_schema是mysql自带的一个数据库。查询结果如下:
mysql
>
select
*
from
information_schema.views \
G
;
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
TABLE_CATALOG: def
TABLE_SCHEMA: login
TABLE_NAME : login_view
VIEW_DEFINITION: select `login`.`login`.`uid` AS `uid`,`login`.`login`.`username` AS `username`
,`login`.`login`.` password ` AS ` password ` from `login`.`login` where ((`login`.`login`.`uid` > = 2 ) a
nd (`login`.`login`.`uid` < = 4 ))
CHECK_OPTION: NONE
IS_UPDATABLE: YES
DEFINER : root@localhost
SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: gbk
COLLATION_CONNECTION: gbk_chinese_ci
1 row in set ( 0 . 10 sec)
* * * * * * * * * * * * * * * * * * * * * * * * * * * 1 . row * * * * * * * * * * * * * * * * * * * * * * * * * * *
TABLE_CATALOG: def
TABLE_SCHEMA: login
TABLE_NAME : login_view
VIEW_DEFINITION: select `login`.`login`.`uid` AS `uid`,`login`.`login`.`username` AS `username`
,`login`.`login`.` password ` AS ` password ` from `login`.`login` where ((`login`.`login`.`uid` > = 2 ) a
nd (`login`.`login`.`uid` < = 4 ))
CHECK_OPTION: NONE
IS_UPDATABLE: YES
DEFINER : root@localhost
SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: gbk
COLLATION_CONNECTION: gbk_chinese_ci
1 row in set ( 0 . 10 sec)
修改视图
create or replace view语句与alter语句修改
1.create or replace view
语法:
create
or
replace [algorithm
=
{undefined|merge|temptable}]
view
view_name [(propertise)]
as
selecte
*
[
with
[
cascaded
|
local
]
check
option
];
有点像create view,create or replace在视图不存在的情况下创建视图,视图已存在则修改视图。
2.alter语句
alter
[algorithm
=
{undefined|merge|temptable}]
view
view_name [(propertise)]
as
selecte
*
[
with
[
cascaded
|
local
]
check
option
];
参数和create view相同。
更新视图
update,注意不要写成updata。
语法:
update
view_name
set
property_1
=
a[, property_2
=
b..];
注意:更新会修改源表,而不只是修改视图,因为视图没有存储,实际操作都是针对源表。主键字段不能update。update会修改视图中的所有数据。
不能更新视图的情况
- 视图包含sum(),count(),max(),min()等函数。
- 视图包含union,union all,distinct,group by, having等关键字。
- 常量视图
- 创建视图的select语句包含select子句。
- 由不可更新的视图导出的视图。//视图可以继续导出视图。
- 创建视图是algorithm参数为temptable。
- 视图对应的表上没有默认值的列,并且该列不再视图里。
- 除此外with check option可能决定视图鞥否更新。
删除视图
删除视图时只删除视图的定义,而不删除视图源表的数据。
语法:
drop
view
[if exits] view_name_list [
restrict
|cascad];
解析:
if exits判断视图是否存在,存在则执行删除,不存在则不执行。
可以一次删除多个视图。
mysql
>
drop
view
if exits login_view;
ERROR 1064 ( 42000 ): You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'exits login_view' at line 1
mysql > drop view if exists login_view;
Query OK, 0 rows affected ( 0 .00 sec)
mysql > desc login_view;
ERROR 1146 ( 42S02 ): Table 'login.login_view' doesn 't exist
ERROR 1064 ( 42000 ): You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'exits login_view' at line 1
mysql > drop view if exists login_view;
Query OK, 0 rows affected ( 0 .00 sec)
mysql > desc login_view;
ERROR 1146 ( 42S02 ): Table 'login.login_view' doesn 't exist
删除视图需要权限允许。查看是否有删除权限的命令:
select
Drop_priv
from
mysql.
user
where
user
=
'username'
;
结果为:
mysql
>
select
drop_priv
from
mysql.
user
where
user
=
'root'
;
+ -----------+
| drop_priv |
+ -----------+
| Y |
| Y |
+ -----------+
2 rows in set ( 0 .00 sec)
+ -----------+
| drop_priv |
+ -----------+
| Y |
| Y |
+ -----------+
2 rows in set ( 0 .00 sec)
<
完
>