您好,登录后才能下订单哦!
1)是一种虚拟存在的表
2)内容与真实的表相似,包含一系列带有名称的列和行数据。
3)视图并不在数据库中以存储的数据的形式存在。
4)行和列的数据来自定义视图时查询所引用的基本表,并且在具体引用视图时动态生成。
更新视图的数据,就是更新基表的数据
更新基表数据,视图的数据也会跟着改变
2 视图的优点有哪些
简单: 使用视图的用户完全不需要关心视图中的数据是通过什么查询得到的。
视图中的数据对用户来说已经是过滤好的符合条件的结果集。
安全:
用户只能看到视图中的数据。
数据独立:
一旦视图的结构确定了,可以屏蔽表结构变化对用户的影响
视图的基本使用
1)把/etc/passwd文件的内容存储到db库下的user表里
[root@mysql_59 ~]# cp /etc/passwd  /var/lib/mysql-files/
[root@mysql_59 ~]# ls  /var/lib/mysql-files/
mysql> create table db.user(
-> username char(64),
-> password char(1),
-> uid int(2),
-> gid int(2),
-> comment char(64),
-> homedir char(64),
-> shell  char(64));
Query OK, 0 rows affected (0.30 sec)
mysql> load data infile "/var/lib/mysql-files/passwd"  into table db.user fields terminated by ":"  lines terminated by "\n";
Query OK, 41 rows affected (0.18 sec)
Records: 41  Deleted: 0  Skipped: 0  Warnings: 0
mysql> alter table db.user add id  int(2) primary key auto_increment first; 
Query OK, 0 rows affected (0.72 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> use db;
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> desc user;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(2)   | NO   | PRI | NULL    | auto_increment |
| username | char(64) | YES  |     | NULL    |                |
| password | char(1)  | YES  |     | NULL    |                |
| uid      | int(2)   | YES  |     | NULL    |                |
| gid      | int(2)   | YES  |     | NULL    |                |
| comment  | char(64) | YES  |     | NULL    |                |
| homedir  | char(64) | YES  |     | NULL    |                |
| shell    | char(64) | YES  |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
创建视图语法
create view 视图名声 as SQL查询语句;
create view 视图名称(字段列表) as SQL查询语句 ;
查看当前库下所有表的信息状态
show  table status\G;
show table status where comment="view"\G;
mysql> create 
3)创建视图v1 结构及数据user表的字段、记录一样
mysql> create view v1 as select * from user;
Query OK, 0 rows affected (0.10 sec)
查看视图具体命令
show create view 视图名称\G;
视图的使用:
查询记录
select 字段名列表 from 视图名  where 条件;
mysql> select username from v1 where id=10;
+----------+
| username |
+----------+
| operator |
+----------+
1 row in set (0.00 sec)
插入记录
insert into 视图名(字段列表)   values(字段值列表);
mysql> insert into user(username,uid) values("jack",88);
Query OK, 1 row affected (0.15 sec)
Update 视图名 set    字段名=值 where  条件;
更新记录
update 视图名 set 字段名=值  where 条件 ;
ysql> update  v1 set comment="AAAA"  where username="root";
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from v1 where username="root";
+----+----------+----------+-------+------+---------+---------+-----------+
| id | username | password | uid   | gid  | comment | homedir | shell     |
+----+----------+----------+-------+------+---------+---------+-----------+
|  1 | root     | x        | 44444 |    0 | AAAA    | /root   | /bin/bash |
+----+----------+----------+-------+------+---------+---------+-----------+
1 row in set (0.00 sec)
+----+----------+----------+-------+------+---------+---------+-----------+
| id | username | password | uid   | gid  | comment | homedir | shell     |
+----+----------+----------+-------+------+---------+---------+-----------+
|  1 | fuck     | x        | 44444 |    0 | AAAA    | /root   | /bin/bash |
+----+----------+----------+-------+------+---------+---------+-----------+
1 row in set (0.00 sec)
删除记录
delete from 视图名 where 条件
mysql> delete from v1 where username="fuck";
Query OK, 1 row affected (0.07 sec)
4)创建视图v2 只有user表shell是/bin/bash用户信息
mysql> create view v2 as select shell from user;
Query OK, 0 rows affected (0.12 sec)
5)删除视图   drop view 视图名;
mysql> drop view v2;
Query OK, 0 rows affected (0.19 sec)
设置字段别名
视图中的字段别名不可以重复 所以要顶一别名
格式为:
create view 视图名 
as
select 表别名.源字段名 as 字段别名
from 源表名  表别名 left join 源表名 表别名
on 条件|;
delimiter //
create procedure db.p11()
begin
if  1=2 then
select   from  db.user where id=2;
else
select   from  db user where id=2;
end if;
end
//
delimiter ;call dbp11(1)
mysql> delimiter //
mysql> create procedure db.p18()
-> begin
-> declare x int default 1;
-> while x<=10 do
-> select x;
-> set x=x+1;
-> end while;
-> end
-> //
Query OK, 0 rows affected (0.06 sec)
mysql> call db.p18();
mysql> delimiter //
mysql> create procedure db.p23()
-> begin
-> declare x int default 1;
-> loab1:while x<=10 do;
-> select x;
-> set x=x+1;
-> if x=3 then
-> leave loab1;
-> end if;
-> end while;
-> end
-> //
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。