数据库===》select用法、MySQL权限管理

一、select高级用法(连表查询,多表联查)传统连接

1.连表查询

#1.创建student表、kcb表、score表,分别记录学生信息、课程信息、分数信息。
mysql> create table student( stu_id int primary key not null auto_increment comment '学生编号', 
    -> stu_name varchar(20) not null comment '学生姓名',
    -> stu_gender enum('f','m') default 'f' comment '性别',
    -> stu_tel char(11) not null comment '手机号码',
    -> stu_address varchar(20) comment '住址');
    
    
mysql> create table kcb( 
	-> kcb_id int not null primary key auto_increment comment '课程编号',  
	-> kcb_name varchar(10) not null comment '课程名称',  
	-> kcb_teacher varchar(10) not null comment '讲师');
	
	
mysql> create table score(
    -> score_id int not null primary key auto_increment comment '成绩编号',
    -> stu_id int not null comment '学生编号',
    -> kcb_id int not null commnet '课程编号',
    -> score_fenshu int not null comment '分数'
    -> );
    
#2.查看表结构
mysql> desc student;
mysql> desc score;
mysql> desc kcb;
 
 
#3.插入数据
mysql> insert into student values(1,'cuij','f','11111111111','shandong'),(2,'xzx','m','11111111112','anhui'),(3,'sxj','f','11111111113','harbin'),(4,'ycy','f','11111111114','gansu');


mysql> insert into kcb values(1,'shuxue','lin'),(2,'yuwen','cui'),(3,'english','chen');


mysql> insert into score values(1,1,1,77),(2,1,2,82),(3,1,3,55),(4,2,1,91),(5,2,2,78),(6,2,3,80),(7,3,1,69),(8,3,2,88),(9,3,3,70),(10,4,1,77),(11,4,2,60),(12,4,3,76);


#4.查看分数超过80分的所有学生的姓名,课程内容,分数信息
mysql> select student.stu_name,kcb.kcb_name,score.score_fenshu from student,kcb,score where score.stu_id=student.stu_id and score.kcb_id=kcb.kcb_id and score.score_fenshu > 80;

#5.查看考试分数超过80分的所有学生的姓名,课程内容,分数信息,讲师信息
mysql> select student.stu_name,kcb.kcb_name,score.score_fenshu,kcb.kcb_teacher from student,kcb,score where score.stu_id=student.stu_idd and score.kcb_id=kcb.kcb_id and score.score_fenshu > 80;


#6.查看所有学生的姓名以及考试成绩并对所有学生的成绩按照降序排列
mysql> select a.stu_name,b.kcb_name,c.score_fenshu from student a,kcb b,score c where c.stu_id=a.stu_id and c.kcb_id=b.kcb_id order by c.score_fenshu desc;

#7.将cuij同学的数学分数更新为100分。
mysql> select c.score_fenshu from student a,kcb b,score c where c.stu_id=a.stu_id and c.kcb_id=b.kcb_id and a.stu_name='cuij' and b.kcb_name='shuxue';

update student a,kcb b,score c set c.score_fenshu=100 where c.stu_id=a.stu_id and c.kcb_id=b.kcb_id and a.stu_name='cuij' and b.kcb_nam.kcb_name='shuxue';

update score,student,kcb set score.score_fenshu=99 where score.stu_id=student.stu_id and score.kcb_id=kcb.kcb_id and student.stu_name='u_name='cuij' and kcb.kcb_name='shuxue';


2.连表查询练习

1)世界上小于100人的人口城市是哪个国家的?

#1.看看查询的内容有哪些
人口数量  城市名  国家名

#2.查看表字段
mysql> desc city;
mysql> desc country;
mysql> desc countrylanguage;

#3.找出要查询的值
人口数量  			城市名  		国家名
city.Population		city.name	  country.name

#4.找三个表相关联的字段
city.CountryCode
country.code
countrylanguage.CountryCode

#5.编写语句
mysql> select city.population,city.name,country.name from city,country where city.countrycode=country.code and city.population < 100;

2)查询人口数量小于100的城市在哪个国家,说的什么语言?

#1.看看查询的内容有哪些
人口数量  城市名  国家名  语言

#2.查看表字段
mysql> desc city;
mysql> desc country;
mysql> desc countrylanguage;

#3.找出要查询的值
人口数量  			城市名  		国家名  		语言
city.Population		city.name	  country.name	  countrylanguage.language

#4.找两个表相关联的字段
city.CountryCode
country.code

#5.编写语句
mysql> select city.population,city.name,country.name,countrylanguage.language from city,country,countrylanguage where city.CountryCode=country.code and country.code=countrylanguage.CountryCode and city.population < 100;

二、select高级用法 自连接

1.根据语句翻译要查询的内容

SELECT city.name,city.countrycode,countrylanguage.language,city.population
FROM city NATURAL JOIN countrylanguage 
WHERE population > 1000000
ORDER BY population;
#查询人口数量大于一百万的城市的国家代码和国家语言,并升序排序?

#刚才的题没有办法使用该连接方式,查不出来,因为没有相同字段,name字段相同,但是值不同
select city.population,city.name,country.name from city natural join country where city.population < 100;

注意:
	1.自己去查找两个表之间的关联字段(natural join) 主键:聚集索引
	2.查询的表内必须有相同的字段名和数据

三、select高级用法 内连接

#1.内连接格式:
select * from 表1 join 表2 on 关联条件 where 条件

#驱动,命中率概念
重点:表1 (小表)
	表2 (大表)

#1.查看分数超过80分的所有学生的姓名,课程内容,分数信息
mysql> select student.stu_name,kcb.kcb_name,score.score_fenshu from student join kcb join score on score.stu_id=student.stu_id and score.kcb_id=kcb.kcb_id and score.score_fenshu > 80;



四、select高级用法 外连接

1.左外连接

mysql> select a.stu_name,a.stu_gender,b.stu_id,b.kcb_id,b.score_fenshu from student a left join score b on a.stu_id=b.stu_id and b.scorre_fenshu>80;

2.右外连接

mysql> select a.stu_name,a.stu_gender,b.stu_id,b.kcb_id,b.score_fenshu from student a right join score b on a.stu_id=b.stu_id and b.scoore_fenshu>80;

五、 mysql权限管理

MySQL 赋予用户权限命令的简单格式可概括为:

grant 权限 on 数据库对象 to 用户

1、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。

grant select on testdb.* to common_user@'%'
grant insert on testdb.* to common_user@'%'
grant update on testdb.* to common_user@'%'
grant delete on testdb.* to common_user@'%'

或者,用一条 MySQL 命令来替代:

grant select, insert, update, delete on testdb.* to common_user@'%'

2、grant 数据库开发人员,创建表、索引、视图、存储过程、函数、等权限。

grant 创建、修改、删除 MySQL 数据表结构权限。

grant create on testdb.* to developer@'192.168.0.%';
grant alter  on testdb.* to developer@'192.168.0.%';
grant drop   on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 外键权限。

grant references on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 临时表权限。

grant create temporary tables on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 索引权限。

grant index on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 视图、查看视图源代码 权限。

grant create view on testdb.* to developer@'192.168.0.%';
grant show   view on testdb.* to developer@'192.168.0.%';

grant 操作 MySQL 存储过程、函数 权限。

grant create routine on testdb.* to developer@'192.168.0.%'; -- now, can show procedure status
grant alter  routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure
grant execute        on testdb.* to developer@'192.168.0.%';

3、grant 普通 DBA 管理某个 MySQL 数据库的权限。

grant all privileges on testdb.* to dba@'localhost'
其中,关键字 “privileges” 可以省略。

4、grant 高级 DBA 管理 MySQL 中所有数据库的权限。

grant all on *.* to dba@'localhost'

5、MySQL grant 权限,分别可以作用在多个层次上。

(1) grant 作用在整个 MySQL 服务器上:

grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。
grant all    on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库

(2)grant 作用在单个数据库上:

grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。

(3). grant 作用在单个数据表上:

grant select, insert, update, delete on testdb.orders to dba@localhost;

这里在给一个用户授权多张表时,可以多次执行以上语句。例如:

grant select(user_id,username) on test.users to mo_user@'%' identified by '123345';
grant select on test.table_name to mo_user@'%' identified by '123345';

(4)grant 作用在表中的列上:

grant select(id, se, rank) on testdb.table_name to dba@localhost;

(5) grant 作用在存储过程、函数上:

grant execute on procedure testdb.pr_add to 'dba'@'localhost'
grant execute on function testdb.fn_add to 'dba'@'localhost'

6、查看 MySQL 用户权限

查看当前用户(自己)权限:

show grants;

查看其他 MySQL 用户权限:

show grants for dba@localhost;

7、撤销已经赋予给 MySQL 用户权限的权限。

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

grant  all on *.* to   dba@localhost;
revoke all on *.* from dba@localhost;

8、MySQL grant、revoke 用户权限注意事项

(1) grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

(2)如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“

grant select on testdb.* to dba@localhost with grant option;

猜你喜欢

转载自blog.csdn.net/weixin_52492280/article/details/114134548