1.视图
视图是由查询结果形成的一张虚拟表,也可以理解为就是一种类似于表的数据对象。
1.1视图的基本使用
- 创建视图:
create view 视图名 as select 语句;
eg:
mysql> create view v_ename_dname as select ename, dname from EMP, DEPT
where EMP.deptno=DEPT.deptno;
Query OK, 0 rows affected (0.10 sec)
//查看视图
mysql> select * from v_ename_dname;
+--------+------------+
| ename | dname |
+--------+------------+
| SMITH | RESEARCH |
| ALLEN | SALES |
| WARD | SALES |
| JONES | RESEARCH |
| MARTIN | SALES |
| BLAKE | SALES |
| CLARK | ACCOUNTING |
| SCOTT | RESEARCH |
| KING | ACCOUNTING |
| TURNER | SALES |
| ADAMS | RESEARCH |
| JAMES | SALES |
| FORD | RESEARCH |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows in set (0.04 sec)
- 修改了视图会对基表数据有影响:
eg:
mysql> update v_ename_dname set dname='sales' where ename='CLARK';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from EMP where ename='CLARK';
+--------+-------+---------+------+---------------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+-------+---------+------+---------------------+---------+------+--------+
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
+--------+-------+---------+------+---------------------+---------+------+--------+
1 row in set (0.01 sec)
- 修改了基表,对视图也会有影响
eg:
mysql> update EMP set deptno=20 where ename='JAMES';
Query OK, 1 row affected (0.01 sec)
mysql> select * from v_ename_dname where ename='JAMES';
+-------+----------+
| ename | dname |
+-------+----------+
| JAMES | RESEARCH |
+-------+----------+
1 row in set (0.00 sec)
可以看到视图中的数据也发生了变化。
- 删除视图:
drop view 视图名;
eg:
mysql> drop view v_ename_dname;
Query OK, 0 rows affected (0.00 sec)
1.2 视图的规则和限制
- 视图与表一样,必须唯一命名(不能出现同名的视图名或者表名)
- 创建视图数目无限制,但是要考虑复杂查询创建为视图之后的性能影响
- 视图不能添加索引,也不能有关联的触发器或者默认值
- 视图可以提高安全性,必须具有足够的访问权限
- order by可以用在视图中,但是如果从该视图检索数据select中也含有order by,那么视图中的order by 将会被覆盖
- 视图可以和表一起使用
2.MySQL的用户管理
2.1 用户信息
MySQL的用户都存储在系统数据库mysql的user表中。
mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.03 sec)
字段解释:
- host:表示这个用户可以从哪个主机登陆,如果是localhost,表示只能从本机登陆
- user:用户名
- authentication_string:用户密码通过password函数加密后的
- *_priv:用户拥有的权限
2.2 创建用户
语法:create user '用户名'@'登陆主机/ip' identified by '密码';
mysql> create user 'Cyj'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.07 sec)
2.3 删除用户
语法:drop user '用户名'@'主机名'
删除时只写用户名不能删除成功:
mysql> drop user 'Cyj';
ERROR 1396 (HY000): Operation DROP USER failed for 'Cyj'@'%'
mysql> drop user 'Cyj'@'localhost';
Query OK, 0 rows affected (0.00 sec)
2.4 修改用户密码
- 自己修改自己的密码:
set password = password('新密码');
- root用户修改指定用户的密码:
set password for '用户名'@'主机名'=password('新的密码');
eg:
mysql> set password for 'Cyj'@'localhost'=password('abcdefg');
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.5 给用户授权
刚创建的用户没有任何权限,需要给用户授权。
语法:
grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码']
说明:
- 权限列表,多个权限用逗号分开
eg:
grant select on ...
grant select, delete, create on ....
grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
*.*
: 代表本系统中的所有数据库的所有对象(表,视图,存储过程等)库.*
: 表示某个数据库中的所有数据对象(表,视图,存储过程等)- identified by可选:
如果用户存在,赋予权限的同时修改密码,如果该用户不存在,就是创建用户
eg:
此时未授权的用户看不到mytest库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| chatroom |
| db2 |
| db3 |
| for_search |
| for_sy |
| forview |
| hhh |
| jdbc |
| mysql |
| performance_schema |
| scott |
| shiwu |
| sys |
| test1 |
+--------------------+
15 rows in set (0.01 sec)
root 给 Cyj 赋予账户查询mytest库的DEPT表的权限 :
mysql> grant select on mytest.DEPT to 'litao'@'localhost';
Query OK, 0 rows affected (0.03 sec
此时Cyj账户可以看到mytest库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mytest |
| test |
+--------------------+
3 rows in set (0.01 sec)
在给用户授权后,发现没有生效,可执行flush privileges;
命令:
2.6 权限回收
语法:revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置';
eg:
-- 回收Cyj对数据库的所有权限
revoke all on mytest.DEPT from 'litao'@'localhost';