1.视图
视图是由查询结果形成的一张虚拟表,也可以理解为就是一种类似于表的数据对象。
1.1视图的基本使用
create view 视图名 as select 语句;
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)
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)
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 视图名;
mysql> drop view v_ename_dname;
Query OK, 0 rows affected (0.00 sec)
1.2 视图的规则和限制
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)
字段解释:
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('新密码');
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 '密码']
说明:
grant select on ...
grant select, delete, create on ....
grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
*.*
: 代表本系统中的所有数据库的所有对象(表,视图,存储过程等)库.*
: 表示某个数据库中的所有数据对象(表,视图,存储过程等)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';