成都10班-苏燕 发表于 2021-11-23 20:36:03

数据库用户权限操作、多表关联查询

先创建用户再授权;1、进入MySQL数据库:use mysql;2、进入user表查询创建了哪些用户:select host,user from user;3、insert into user(host,user,password)values(‘localhost’,’dxs10’,password(‘123456’));往user表插入一个用户dcs10,使用password这个函数对密码进行加密处理4、插入用户之后查询user表是否存在dcs10用户:select host,user from user;5、flush privileges;   插入用户之后,刷新权限6、双击上边窗口打开新的操作界面,在新的操作界面使用dcs10用户去连接MySQL数据库: mysql -udcs10 -p123456 查看权限:show grants for ‘dcs10’@’localhost’;   查看dcs10用户有哪些权限,显示USAGE表示没有任何操作权限授予权限:grant select,drop,update,delete,create on *.* to ‘dcs10’@’localhost’ identified by ‘123456’;   给dcs10用户赋予 查询、删表、更新、删数据、创建的权限,第一个*表示所有的库,第二个*表示所有的表,即针对所有库里面的所有表取消权限:revoke all on *.* from ‘dcs10’@’localhost’;    在root用户窗口移除dcs10用户所有权限授予权限和取消权限之后都需要进行刷新权限:flush privileges; 创建用户同时授权:grant all privileges on *.* to ‘dcs11’@’localhost’ identified by ‘123456’;   创建用户dcs11的同时赋予所有权限show grants for ‘dcs11’@’localhost’;查看dcs11用户有哪些权限 update user set password=password(‘654321’) where user=’dcs11’;   把用户dcs11的密码更新为654321。刷新权限后,在另一个窗口使用dcs11访问数据库,密码为654321 delete from user where user=’dcs11’;   删除dcs11用户 多表关联查询:1、内连接 inner join:有相同两个值的字段进行拼接,只显示两个表里相同内容select * from aa inner join cc on aa.id=cc.s_id;2、左连接 left join:左边aa表显示所有内容,右边只显示aa表相同内容,不同内容被屏蔽select * from aa left join cc on aa.id=cc.s_id;3、右连接 right join:以右边的表为主,对于不符合的条件显示nullselect * from aa right join cc on aa.id=cc.s_id;4、基本连接:两个表当中有字段的值相同就可以通过基本连接进行连接select * from aa,cc where aa.id=cc.s_id;5、硬连接 union:条件是aa表的字段数目需要和cc表的字段数目一样select * from aa union select * from cc; 求张三的成绩6、临时表:通过SQL语句查询出来显示的表都可以作为临时表,需取别名select * from aa,cc where aa.id=cc.s_id;select name,score from (select * from aa,cc where aa.id=cc.s_id) t where name=’zhangsan’;7、嵌套=法先求张三的idselect id from aa where name=’zhangsan’;select score from cc where s_id=(select id from aa where name=’zhangsan’);8、嵌套in法select id from aa where name=’zhangsan’;select score from cc where s_id in (select id from aa where name=’zhangsan’);9、基本连接法select name,score from aa,cc where aa.id=cc.s_id and name=’zhangsan’; 求出谁没有参加考试1、嵌套法select id from aa,cc where aa.id=cc.s_id;select name from aa where id not in(select id from aa,cc where aa.id=cc.s_id);2、临时表+左连接select * from aa left join cc on aa.id=cc.s_id;select name from (select * from aa left join cc on aa.id=cc.s_id)t where score is null;3、嵌套先求分数不为空的s_idselect s_id from cc where score is not null;select name from aa where id not in (select s_id from cc where score is not null);
页: [1]
查看完整版本: 数据库用户权限操作、多表关联查询