找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7Vjr0tg5K6BKLpBh0jEhjgoPw\c1882f0041a34b548c6f17ed61d25ecc\c]@%26n9gl$kxex)u{vywcs.png

C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7Vjr0tg5K6BKLpBh0jEhjgoPw\997077cdc21f40ef92585f1016b3878d\l%vx7plsxfabca%@npw6mjx.png

1、先查看数据库,show databases;
2、选择进入mysql  --use mysql;
3、查询select host,user from user;
[img=620,0]C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7Vjr0tg5K6BKLpBh0jEhjgoPw\76e0eaed83754d2ab9555f0974b43deb\9$y47j4__`65w)vj0f3[`(3.png[/img]
4、增加%百分号权限:grant all privileges on *.* to 'root'@'%' identified by '123456';
5、打开navicat 点击连接,输入连接名,主机ip地址,账户及密码(前提是需要先在root里面进行重启service mysqld restart,在关闭防火墙service iptables stop,)点击连接即可。就能看到对应的数据库里面建立的信息(创建的数据库跟表),如果有权限的话则可以对表结构做增删改,随意的删除表,
点击查询就能看到对应的数据,并进行指令操作,双击就是运行。Ctrl s 就是保存当前输入的指令,给要保存的数据命名,再双击对应数据库里面的查询就就能看到命名的文件,双击就能打开刚才的指令界面。

连接满足的条件:
  ①、账号: root
  ②、密码:123456
  ③、端口:3306
  ④、域名、ip地址  通过ifconfig查询ip  localhost 192.168.63.128
  ⑤、数据库需要重启 service mysqld restart
  ⑥、防火墙要关闭 service iptables stop
   且对应的账户要拥有远程访问的权限(%)百分号权限
   如果没有%权限需要通过指令来赋予
1、use mysql 先进入数据库
2、select host ,user from user;再查询即可
如果没有远程权限给他赋予权限的指令:grant all privileges on *.* to 'root'@'%' identified by '123456';
3、创建用户不赋予权限(就在当前进入的mysql当中创建,创建前后使用上一条查询进行查看)
insert into user (host,user,password)values('localhost','qian',password('123456'));新增加一个用户赋予本地访问权限,用户名称是qian
4、创建用户后进行授权,,需要进行刷新flush privileges;
grant select,update,delete,drop on *.* to 'qian'@'localhost' identified by '123456';
5、查询指定用户拥有哪些权限---需要进行刷新
show grants for 'qian'@'localhost';
[img=620,0]C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7Vjr0tg5K6BKLpBh0jEhjgoPw\7b9d9b6095ae47b494e8036079876abf\xy(q}_15w(hpfv`b@2b84h3.png[/img]
6、取消指定用户的权限
revoke all on *.* from 'qian'@'localhost';
C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7Vjr0tg5K6BKLpBh0jEhjgoPw\826cb4cc7e3c43cc86a1545a999c5d51\@m`{{0m075ft}n4emj8u14a.png
7、删除用户
delete from user where user= 'qian' and host= 'localhost';
[img=620,0]C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7Vjr0tg5K6BKLpBh0jEhjgoPw\d78731fd62ca40dbac3a71c8f1fbdd1c\)@1k`stui$9o[vo0@tm$iy0.png[/img]
8、改用户密码
在user表中把root用户的密码改为123456
update user set password=password('123456') where user='root';


多表查询的方法(重点)
1、多表的条件:必须要关联的表中有相同的字段
2、有哪些方法可以把表和表进行关联?
  A、基本连接(常用):select * from aa,cc where aa.id=cc.s_id; #表关联
                                   select * from aa,cc where id=s_id;#表关联
                                            select * from cc,aa ,ll  where id=s_id and id=c_id; #from后面表是可随意的排列
查询出来的结果只会显示两个表中有得值,两两为真则为真,一真一假,一假一真、两两为假则为假
  B、内连接inner join on
                        select * from aa inner join cc on aa.id=cc.s_id;
                        select * from aa inner join cc on id=s_id;
                        select * from cc inner join aa on id=s_id;
查询出来的结果只会显示两个表中有得值,两两为真则为真,一真一假,一假一真、两两为假则为假

  C、左连接方法:左边的表为主表,右表为子表    left join on
                       select * from aa left join  cc on  aa.id=cc.s_id;   主表:aa,子表:cc                  
                            select * from aa left join  cc on  id=s_id; #aa是主表,cc是子表,aa表中有2个字段,cc表中只有1个字段,由于aa是主所以主全部显示内容,cc是子如果子表小于主表子表自动在缺少的位置补齐null。
                      select * from cc left join aa on  id=s_id;  #cc是主表,子表aa
                      from 后面是左边,on的前面是右边
解释:左连接以左边表为基准
如果主表的值是大于子表的值,子表缺少的值自动补齐null;
如果主表的值是小于子表的值,只取和主表相同的值其他不显示
只有基本连接才需要接where条件句,其他不需要

D、右连接方法:右边的表为主表,左表为子表   right join on
      select * from aa right join  cc on  aa.id=cc.s_id;   主表:cc,子表:aa               
       select * from aa right join  cc on  id=s_id; # cc是主表,aa是子表
      select * from cc right join aa on  id=s_id;  #aa是主表,子表cc
解释:右连接以右边表为基准
如果主表的值是大于子表的值,子表缺少的值自动补齐null;
如果主表的值是小于子表的值,只取和主表相同的值其他不显示

mysql面试题常问的一个问题--左右连接的区别?左连接以左边的为主,右边为子,右连接是以右边的为主,左边的为子。

union 连接方法
select * from aa union select * from cc;此方法连接是必须多个表字段要相同

eg:临时表写法:就是起别名
1、select * from aa,cc where id=s_id;
+------+----------+------+-------+
| 1001 | zhangsan | 1001 | 99    |
+------+----------+------+-------+
2、select * from (select * from aa,cc where id=s_id)a;  起别名
+------+----------+------+-------+
| 1001 | zhangsan | 1001 | 99    |
+------+----------+------+-------+
3、select * from (select * from aa,cc where id=s_id)a where a.name='zhang';
     select score from (select * from aa,cc where id=s_id)a where a.name='zhang';
      select * from (select * from aa,cc where id=s_id)a where name='zhang';
举例:select * from (select * from aa order by score desc)a group by class;
select name,class from (select max(score),name,class from aa)a;查看分数最大的并显示名字班级,临时表
select score from aa,cc where id=s_id and name='zhangsan'; 基本连接方法

嵌套:
select * from aa where name='zhangsan';显示张三的所有信息
| 1001 | zhangsan |
select id from aa where name='zhangsan';显示张三的id信息
| 1001 |
select * from cc;
| 1001 | 99    |
select score from cc where s_id in(select id from aa where name='zhangsan');嵌套方法
| 1001 | 99    |
select score from cc where s_id in(select id from aa where name='zhangsan'); in也可以换成=号
| 99    |

分享至 : QQ空间
收藏

1 个回复

倒序浏览
秀儿
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册