简述什么是视图
admin
2023-04-20 16:42:40
0

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
-> //

相关内容

热门资讯

“一次愉快的会晤”,泽连斯基披... 在俄乌互相加大对对方纵深目标打击之际,乌克兰总统泽连斯基7月28日造访了白宫,与美国总统特朗普举行会...
总统连遭“逼宫”,少帅临危受命 躲避着燃烧瓶和火箭弹袭击,四辆苏制BMP步兵战车快速穿行在马里乌波尔市区。即将出城的路口,迎面出现1...
独居女子在卧室玩偶内发现摄像头... 租房独居女子意外发现家中玩偶内藏着偷拍设备,报警后偷拍设备竟“不翼而飞”,门锁记录显示有人在她出门的...
伊学者:乌克兰袭击行动适得其反... 乌克兰袭击与伊朗有关的船只后,伊朗和俄罗斯接连作出强硬回应。伊朗外交学院教授法拉吉扎德在接受凤凰卫视...
20万人怒火不会停!蓝议员挺蒋... 海峡导报综合报道 在蓝白号召台湾民众上凯道呐喊“反毒台”后,台北市长蒋万安日前透露,近日不少民代私下...
柯文哲妻子陈佩琪畅游大陆,民进... 台湾民众党前主席柯文哲妻子陈佩琪27日在脸书发文证实,自己早前由儿子陪同前往上海、杭州与北京旅游一周...
高市早苗被问如何改善中日关系 据日本东京新闻报道,当地时间7月27日,日本首相高市早苗在首相官邸举行记者会。当被问及如何改善中日关...
扎哈罗娃批小泉进次郎涉核言论:... 【环球网报道 记者 张江平】综合俄新社等媒体报道,对于日本防卫大臣小泉进次郎涉核言论,俄罗斯外交部发...
“特高课”再现?日本强推“国家... 无视反对战争正义呼声 复刻战前军国主义体制日本强推“国家情报局”引发广泛担忧和批评(国际视点)本报记...
6G相比5G不只是速度更快 将...   6G相比5G不只是速度更快  【6G相比5G不只是速度更快】7月28日,相比5G,6G不仅是速度...