- mysql> grant all on *.* to root@"%" identified by "123456";
2 案例2:视图的基本使用
2.1 问题
- 把/etc/passwd文件的内容存储到db9库下的user表里
- 添加新字段id 存储记录的行号(在所有字段的前边)
- 创建视图v1 结构及数据user表的字段、记录一样。
- 创建视图v2 只有user表shell是/bin/bash用户信息 。
- 分别对视图表和基表执行insert update delete 操作。
- 删除视图v1 和 v2
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:视图的基本使用
什么是视图:是一种虚拟存在的表
内容与真实的表相似,包含一系列带有名称的列和行数据。
视图并不在数据库中以存储的数据的形式存在。
行和列的数据来自定义视图时查询所引用的基本表,并且在具体引用视图时动态生成。
更新视图的数据,就是更新基表的数据
更新基表数据,视图的数据也会跟着改变
1)把/etc/passwd文件的内容存储到db9库下的user表里
- [root@mysql51 ~]# mysql -u root -p123456
- mysql> create database db9;
- Query OK, 1 row affected (10.00 sec)
- mysql> create table db9.user(username char(20),password char(1),uid \
- int(2),gid int(2),comment char(100),homedir char(100),shell char(50));
- Query OK, 0 rows affected (0.02 sec)
- [root@mysql51 ~]# cp /etc/passwd /var/lib/mysql-files/
- [root@mysql51 ~]# ls /var/lib/mysql-files/
- passwd
- mysql> load data infile "/var/lib/mysql-files/passwd" into table db9.user fields terminated by ":" lines terminated by "\n";
- Query OK, 41 rows affected (0.02 sec)
- Records: 41 Deleted: 0 Skipped: 0 Warnings: 0
2)添加新字段id 存储记录的行号(在所有字段的前边)
- mysql> alter table db9.user add id int(2) primary key auto_increment first;
- Query OK, 0 rows affected (0.04 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> use db9;
- mysql> desc user;
- +----------+-----------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+-----------+------+-----+---------+----------------+
- | id | int(2) | NO | PRI | NULL | auto_increment |
- | username | char(20) | YES | | NULL | |
- | password | char(1) | YES | | NULL | |
- | uid | int(2) | YES | | NULL | |
- | gid | int(2) | YES | | NULL | |
- | comment | char(100) | YES | | NULL | |
- | homedir | char(100) | YES | | NULL | |
- | shell | char(50) | YES | | NULL | |
- +----------+-----------+------+-----+---------+----------------+
- 8 rows in set (0.00 sec)
3)创建视图v1 结构及数据user表的字段、记录一样
- mysql> create view v1 as select * from user;
- Query OK, 0 rows affected (0.00 sec)
4)创建视图v2 只有user表shell是/bin/bash用户信息
- mysql> create view v2 as select shell from user;
- Query OK, 0 rows affected (0.01 sec)
5)分别对视图表和基表执行insert update delete 操作
- mysql> insert into v1(username,uid) values("jarry",9);
- Query OK, 1 row affected (0.00 sec)
- mysql> update v1 set uid=9 where username="adm";
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> delete from v1 where uid=9;
- Query OK, 2 rows affected (0.01 sec)
6)删除视图v1 和 v2
- mysql> drop view v1;
- Query OK, 0 rows affected (0.00 sec)
- mysql> drop view v2;
- Query OK, 0 rows affected (0.00 sec)
注意:对视图操作即是对基本操作,反之亦然!!!
3 案例3:视图进阶操作
3.1 问题
- 练习OR REPLACE的选项使用
- 练习WITH LOCAL CHECK OPTION 选项的使用
- 练习WITH CASCADED CHECK OPTION 选项的使用
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:视图进阶操作
1)创建视图完全格式
- mysql> create table user2 select username,uid,gid from user limit 3;
- Query OK, 3 rows affected (0.01 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql> create table info select username,uid,homedir,shell from user limit 5;
- Query OK, 5 rows affected (0.02 sec)
- Records: 5 Duplicates: 0 Warnings: 0
查询user2.username=info.username的字段
- mysql> select * from user2 left join info on user2.username=info.username;
- +----------+------+------+----------+------+---------+---------------+
- | username | uid | gid | username | uid | homedir | shell |
- +----------+------+------+----------+------+---------+---------------+
- | root | 0 | 0 | root | 0 | /root | /bin/bash |
- | bin | 1 | 1 | bin | 1 | /bin | /sbin/nologin |
- | daemon | 2 | 2 | daemon | 2 | /sbin | /sbin/nologin |
- +----------+------+------+----------+------+---------+---------------+
- 3 rows in set (0.00 sec)
2)关联查询建的视图 默认不允许修改视图字段的值
- mysql> create view v4 as select * from user2 left join info on user2.username=info.username;
- ERROR 1060 (42S21): Duplicate column name 'username'
- mysql> create view v4 as select a.username as ausername,b.username as busername, a.uid as auid,b.uid as buid from user2 a left join info b on a.username=b.username;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from v4;
- +-----------+-----------+------+------+
- | ausername | busername | auid | buid |
- +-----------+-----------+------+------+
- | root | root | 0 | 0 |
- | bin | bin | 1 | 1 |
- | daemon | daemon | 2 | 2 |
- +-----------+-----------+------+------+
- 3 rows in set (0.00 sec)
- mysql> desc v4;
- +-----------+----------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-----------+----------+------+-----+---------+-------+
- | ausername | char(20) | YES | | NULL | |
- | busername | char(20) | YES | | NULL | |
- | auid | int(2) | YES | | NULL | |
- | buid | int(2) | YES | | NULL | |
- +-----------+----------+------+-----+---------+-------+
- 4 rows in set (0.00 sec)
3)OR REPLACE的选项使用
创建时,若视图已存在,会替换已有的视图
语法格式:create or replace view视图名as select 查询; //达到修改已有视图的目的
- mysql> create or replace view v4 as select a.username as ausername,b.username as busername, a.uid as auid,b.uid as buid from user2 a left join info b on a.username=b.username;
- Query OK, 0 rows affected (0.00 sec)
4)WITH LOCAL CHECK OPTION
LOCAL和CASCADED关键字决定检查的范围
LOCAL 仅检查当前视图的限制
CASCADED 同时要满足基表的限制(默认值)
- mysql> create table user1 select username,uid,shell from user where uid>=5 and uid <=40;
- Query OK, 11 rows affected (0.01 sec)
- Records: 11 Duplicates: 0 Warnings: 0
- mysql> create view v1 as select username,uid from user1 where uid<=20;
- Query OK, 0 rows affected (0.01 sec)
- mysql> update v1 set uid=21 where username="sync";
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> update user1 set uid=41 where username="ftp";
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> create table a select * from user where uid < 10;
- Query OK, 7 rows affected (0.01 sec)
- Records: 7 Duplicates: 0 Warnings: 0
- mysql> create view v3 as select * from a where uid < 10 with check option;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update v3 set uid=9 where username="adm";
- Query OK, 0 rows affected (0.01 sec)
- Rows matched: 0 Changed: 0 Warnings: 0
- mysql> create view v2 as select * from v1 where uid >= 5 with local check option;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update v2 set uid=9 where username="sync";
- Query OK, 0 rows affected (0.00 sec)
- Rows matched: 0 Changed: 0 Warnings: 0
5)WITH CASCADED CHECK OPTION
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
分享想法到看一看
确定
最多200字,当前共字
微信扫一扫
关注该公众号