找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
备份表和表数据
create table ceshi1 like ceshi; 创建一个新表,表结构跟ceshi一模一样
insert into xiaocheng1 select * from xiaocheng; 往表xiaocheng1里面插入xiaocheng的数据(所有数据),(select * from xiaocheng)当成一个整体
insert into xiaocheng1(name,class) select name class from xiaocheng;往表xiaocheng1里面插入xiaocheng的部分数据,需要指定字段,插入的表必须先存在

备份库
mysqldump -uroot -p“123456”  xiaocheng>/ xiaocheng.sql:将库xiaocheng备份放到linux系统的根目录的xiaocheng.sql 这个文件中
mysql -uroot -p“123456”  xiaocheng1 xiaocheng.sql:将xiaocheng.sql这个备份文件还原到xiaocheng1这个库当中(前提是要先新建一个库xiaocheng1)

用户权限
use mysql  存放数据库的用户
select host,user,password from user ;查看user表中的用户
localhost 和127.0.0.1 表示是本地用户  可以通过centos 跟xshell对数据库进行操作的用户
% 表示的是具有远程访问权限的用户,可以通过数据库客户端工具进行连接
步骤:
1.往user表中新增一个xiaocheng2的用户
insert into user(host,user,password) values (“localhost”,“xiaocheng2”,password“123456”)
2.查看是否有权限
show grants for “xiaocheng2”@“localhost”
3.刷新加载权限表
flush privileges
4.再次查看权限
show grants for “xiaocheng2”@“localhost”
5.赋权
grant select,update on *.*to "xiaocheng2"@"localhost" identified by"123456",
6.加载权限
flush privileges;
7.再次查看权限(已存在赋权的查询和修改权限)
show grants for “xiaocheng2” @ “localhost”;
8.移除本地用户xiaocheng2的所有权限
revoke all privileges on *.* from “dcs16” @“localhost”
9.创建用户的同时赋权所有权限
grant all  privileges on *.* to "xiaocheng2"@"%" identified by “123456”
10.查找所有的用户
select host,user from user;
11.删除本地用户
delete from user where user=“xiaocheng2” and host=“localhost”;
12.修改密码
update user set password=password(“123456”)where user=“chenghao2” and host=‘%’


navicat 连接不上mysql(有3种原因)
1.用户是否具有远程访问权限
如果没有就可以在mysql里面进行赋权:grant all  privileges on *.* to "xiaocheng2"@"%" identified by “123456”
2.关闭防火墙(在linux界面操作):输入 service iptables stop
3.检查mysql服务是否存在 :service mysqld start

navicat的使用
Ctrl +q 新建一个窗口
Ctrl+w关闭一个窗口
单行注释:用#号注释的sql语句不会被执行
多行注释:/* 注释内容 */
自己输入的条件需要用and
命令值只需要用空格隔开

多表查询:
多表当中字段的值相同时则可以进行拼接(关联)
1.SELECT * FROM  aa,cc WHERE aa.id=cc.s_id; 普通连接(取aa,cc两表当中交集的部分)
2.select *from aa inner join cc on aa .id=cc.s_id; 内连接,普通连接的第二种方法(都是取表交集部分)
3.左连接查询(左边的表中数据全部显示),关键字 left join (以左表为基准)
例:SELECT *FROM aa LEFT JOIN cc on aa .id=cc.s_id; 以左边的aa表为基准全部显示,右边cc表中符合条件的全部显示,不符合条件的数据会以null显示
4.右连接(右边的表中数据全部显示),关键字right join(以右表为基准)
例:SELECT *FROM aa RIGHT JOIN cc on aa .id=cc.s_id; 以右边的cc表为基准全部显示,左表中多余的数据不显示
5.硬连接(追加)前提条件:2张表中的字段数量必须相同
例:select * from aa union select * from cc ; 将表aa当中的数据和cc表中的数据连接起来

例:求aa和cc表中张三的成绩
1.基本连接:SELECT aa.name,cc.score FROM aa,cc WHERE aa.id=cc.s_id AND aa.“name“="zhangsan";
2.临时表:select t.name,t.score from (select * from aa,cc where aa.id=cc.s_id)as t where t.name="zhangsan";
3.嵌套方法:“="(=和in的区别,=只能是一个值,in后面可以是多个值)
第一步:select id from aa where name="zhangsan"; 先求出学号
第二步:select score from cc where s_id=(select id from aa where name="zhangsan"); 再以学号作为条件
嵌套方法“in”
select score from cc where s_id in(select id from aa where name="zhangsan");

求出谁没有参加考试
1.左连接:SELECT aa.name FROM aa LEFT JOIN cc ON aa.id=cc.s_id WHERE cc.score is NULL;
2.右连接:SELECT aa.name FROM cc RIGHT JOIN aa ON aa.id=cc.s_id WHERE cc.score is NULL;
3.基本连接+嵌套:
第一步:SELECT id FROM aa,cc where aa.id=cc.s_id; 先求出已经参加考试的学号
第二步:SELECT * FROM aa WHERE aa.id not IN(SELECT id FROM aa,cc where aa.id=cc.s_id); 把第一步当中求出的结果当成一个条件嵌套进去
4.嵌套模式:SELECT name from aa where id not in(select s_id from cc);

处理mysql的中文乱码问题
1.在linux系统中修改文件,vim /etc/my.cnf 编辑数据库配置文件
2.最后一行加入: character _set_server=utf8
3.重启数据库:service mysqld restart
4.在navicat中点击数据库 -编辑数据库-把属性修改为 utf-8编码格式



例-- 14.列出每一个部门中年纪最大的员工姓名,部门名称;
SELECT * FROM dept,emp WHERE dept.dept1=emp.dept2; #关联
SELECT dept.dept_name FROM dept,emp GROUP BY dept.dept_name;#将部门按照名字进行分组
SELECT dept.dept_name,MAX(emp.age) FROM dept,emp WHERE dept.dept1=emp.dept2 GROUP BY dept.dept_name;#列出每个部门中年纪最大的员工
SELECT  emp.name,dept.dept_name FROM dept,emp WHERE dept.dept1=emp.dept2 AND (dept.dept_name,emp.age) IN (SELECT dept.dept_name,MAX(emp.age) FROM dept,emp WHERE dept.dept1=emp.dept2 GROUP BY dept.dept_name);  嵌套使用  #列出每个部门中年纪最大的员工姓名和部门名称





分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册