成都10期-陈钱 发表于 2021-11-23 19:49:49

数据库授权与多表连接

第十二天学习笔记
数据库授权与多表连接
用户权限操作
给普通的数据库用户赋予相应的权限(增删改查)
1、进入数据库show database;use mysql;
2、查看创建的有哪些用户selecthost,userfrom user;
3、创建用户但不授权   insert into user(host,user,password)values('localhost','dcs10',password('123456'));
      查询用户    select host,user from user;
      刷新   flush privileges;
4、打开新的窗口:进入mysql,mysql -udcs10 -p123456;
   查看数据库显示的信息:show databases;
5、打开第一个窗口查看新建用户的权限   show grant for 'dcs10'@'localhost';
6、给新用户进行授权   grantselect,drop,update,delete,createon *.* to 'dcs10'@'localhost' identifiel by '123456';
      刷新flush privileges;
7、在第一个窗口移除新用户的权限revoke all on *.* from 'dcs10'@'localhost';
      刷新flush privileges;
8、创建用户同时进行授权grant all privileges on *.* to 'dcs11'@'localhost' identifiel by '123456';
      刷新flush privileges;
9、修改用户的登录密码update user set password=password('123456') where user='dcs11';
      刷新flush privileges;
10、再用另一个窗口用新密码登录mysql -udcs11 -p654321;
11、删除用户delete from user where user='dcs11';
查询数据库的基本信息
查询数据库版本   select version();
查询当前时间       select now();
查询当前使用的是哪个数据库    select database();
查询当前登录用户   selectuser();
赋予root用户远程权限grant all privileges on *.* to 'root'@'%'identifiel by '123456';
多表连接
1、内连接inner join
对于2个表中有字段的值相同的,就可以通过内连接进行拼接。
select *from aa inner cc on aa.id=cc.s_id;
2、左连接 left join
左边显示全部字段,对于右边不符合条件的数据显示null
select * from aa left join cc on aa.id=cc.s_id;
3、右连接 right join
右边显示全部字段,对于左边不符合条件的数据显示null
select * from aa right join cc on aa.id=cc.s_id;
4、基本连接
对于两个表中有字段相同的就可以通过基本连接拼接
select * from aa,cc where aa.id=cc.s_id;
5、硬连接(机械拼接)
条件是aa表的字段数目需要和cc表的字段数目一样
select *from aa union select *from cc;
6、临时连接
通过sql语句查询出来的表都可以作为临时表
求张三的成绩信息:
selectname,score from (select * from aa,cc where aa.id=cc.s_id)twhere name='zhangsan';
7、嵌套=的方法
求张三的成绩信息
第一步:select idfrom aawhere name='zhangsan';
第二步:selectscore from ccwhere s_id=(select idfrom aawhere name='zhangsan);
8、嵌套in的方法
求张三的成绩信息
第一步:select idfrom aawhere name='zhangsan';
第二步:selectscore from ccwhere s_id in (select idfrom aawhere name='zhangsan);
9、基本连接法
求张三的成绩信息
selectname,score from aa,cc where aa.id=cc.s_id and name='zhangsan';

页: [1]
查看完整版本: 数据库授权与多表连接