视图和索引
一、什么是视图
视图是逻辑上来自一个或多个表的数据集合。
例题:create view view1_emp as select empno,ename,job from emp where deptno=10;
练习1:
1 create view view2_emp as select empno a,ename,sal from emp where sal
between 2000 and 5000 and ename like '%A%';
2 select * from view2_emp;
练习2:
1 create view view4_emp as select empno,ename,emp.deptno,hiredate from dept,emp
where dept.deptno=emp.deptno and loc in('NEW YORK','CHICAGO');
二、修改视图
create or replace view view4_emp as select empno,ename,emp.deptno,hiredate from dept,emp
where dept.deptno=emp.deptno and loc in('NEW YORK','CHICAGO');
三、视图上执行DML操作
create or replace view v5_dept as select * from dept where deptno=10 with check option;
注意:with check option使得视图的功能更强大,不仅能通过视图查表,还能通过视图改变表的数据。
update v5_dept set dname=100;
四、删除视图
drop view v5_dept;
五、索引概述
通过索引可以提高查询的速度
六、创建索引
创建索引有三种方式,这三种方式分别是:
1 创建表的时候创建索引
例:
Create table index1(
Id int,
Name varchar(20),
Sex boolean,
Index(id)
);
2 在已经存在的表上创建索引
使用alter table语句来创建索引
alter table dept add index i_deptno(deptno);
drop index i_deptno on dept;
七、索引的设计原则
1 选择唯一性索引
2 为经常需要排序、分组和联合操作的字段建立索引
3 为常作为查询条件的字段建立索引
4 限制索引的数目
5 尽量使用数据量少的索引
6 尽量使用前缀来索引
7 删除不再使用或很少使用的索引
用户和权限管理
一、权限表
权限表共有6个表:user,db,host,tables_priv,columns_priv,procs_priv。
二、帐户管理
1 创建新用户
首先用root登录mysql
然后发出创建新用户的命令:create user scott identified by 'scott';
创建一个新用户,用户名scott,密码scott。
2 授予权限
grant insert,update,delete,select on demo1.* to scott;
grant create,alter,drop,insert,update,delete,select on demo1.* to scott;
练习1:
create user user1 identified by 'user1';
grant select,update on demo1.* to user1;
3 删除用户
drop user user1;
4 修改用户名
rename user scott to scott1;
5 修改密码
alter user scott1 identified by 'scott1';
6 root用户修改自己的密码
alter user root@localhost identified by 'root';
7 授权(远程访问)
scott1用户已经在前面创建并授权了
mysql -uscott1 -pscott1 -h10.25.177.25
7 授权
grant update(deptno) on demo1.dept to scott1;
8 收回权限
create user scott identified by 'scott';
grant insert,update,delete,select on demo1.* to scott;
revoke all privileges,grant option from scott;
grant insert,update,delete,select on demo1.* to scott;
revoke insert on demo1.* from scott;
9查看权限
show grants for scott;
SELECT User, Host FROM user; --查看现有用户及允许连接的主机