MySQL常见使用(二)

主键约束:

创建表时给表添加的条件限制
主键:表示数据唯一性的建成为主键
  • 主键约束:用于限制数据唯一且不能为空
 create table t1(id int primary key,name varchar(10))charset=utf8;
 create table t2(id int(5) zerofill primary key comment '这个是主键',name varchar(10));//主键约束,同时设置主键的数字个数
  • 约束加自增
//自增规则:历史最大值+1
primary key auto_increment
  • 创建表时添加注释:
comment "这个是主键"
create table t2(id int(5) zerofill primary key comment '这个是主键',name varchar(10));//主键(10))charset=utf8;

导入数据:

linux:
windows:source e:/emp.sql;
解决导入乱码问题: set names utf8;
source e:/emp.sql;
set names gbk;
select * from emp;

运算符

  • is null和is not null;

select ename, sal,comm from emp where mgr is null;

  • 去重:distinct;
select distinct job from emp;
  • 数值计算:+ - * / %

select ename 姓名,sal’工资’,sal*5 as '年终奖’from emp;

  • 比较运算符:

< > = >= <= !=和<> select ename ,job ,sal from emp where job<>‘销售’;

  • and和or and类似Java中的&&,or类似Java中的||
select * from emp where deptno=1 and sal<1500;
select ename,sal ,deptno from emp where deptno=2 || sal<2000;
  • between …and …
select * from emp where sal between 2000 and 3000;
  • in关键字
select * from emp where sal in(800,1500,3000);
  • 模糊查询:like

%:代表0或多个未知字符
_代表一未知字符

select * from emp where ename like ‘孙%’;

关键字

排序

  • 升序: order by
select ename,sal from emp order by sal;
  • 降序 desc
select ename,sal from emp order by sal desc;

案例: 工资按照降序排列
select ename,sal from emp where deptno=3 order by sal
desc;

多字段排序(先将部门编号按照降序排列,再将公职按照升序排列)
select ename,sal ,deptno from emp order
by deptno desc ,sal; select * from emp order by deptno ,sal desc;

  • 分页查询:

limit 跳过的条数,请求的条数
举例:第一页的三条数据 :limit 0,3
第七页的六条数据 :limit 36,6

//查询工资最高的前三名 姓名和工资
select ename,sal from emp  order by sal desc limit 0,3;
//查询按工资升序排列的第三页的2条数据;
select ename ,sal from emp order by sal  limit 4,2;

聚合查询:对多条数据进行统计查询

  • 求和:sum(字段名)
select sum(sal) 工资 from emp where deptno=1;
  • 平均值:avg(字段名)

select avg(sal) 工资 from emp where deptno=1;

  • 最大值:max(字段名)
select max(sal) 工资 from emp where deptno=1;
  • 最小值:min(字段名)
select min(sal) 工资 from emp where deptno=1;
  • 计数:count
select count(*) from emp where deptno=2;

案例:

select min(sal) from emp where deptno=3||deptno=1 group by deptno;
select deptno 部门,count(deptno) 人数 from emp where sal>1500 group by
deptno;

  • having 关键字(需要与group by结合使用)

聚合条件的函数不能写在where的后面,要用having 案例:
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;

案例: select deptno 部门,count(deptno) 部门出现次数,sum(sal)工资总和,avg(sal) 平均工资
from emp where sal>=1000 and sal<=3000 group by deptno having
平均工资>2000 order by 平均工资 ;

练习题:
1.select empno, ename, sal from emp where mgr is null;
2.select ename, comm from emp where comm is not null;
3.select ename, sal from emp where ename like ‘%精%’;
4.select * from emp where ename like ‘_八%’;
5.select * from emp where sal>2000 and deptno=1;
6.select * from emp where deptno=2||sal<1500;
7.select * from emp where sal in(3000,1500,5000) order by sal desc;
8.select sum(sal) from emp where deptno=3;
9.> select count(sal) s from emp where sal>1000 group by deptno order by s;
10.select count(mgr) m from emp where mgr is not null group by deptno order by m desc;
11.]> select * from emp order by deptno ,sal desc;
12.select distinct deptno,max(sal) from emp where mgr is not null group by deptno;
13.select * from emp where mgr is not null order by sal limit 4,2;

关联查询:(多语句查询):

  • 等值连接:
select * from emp where sal>(select avg(sal) from emp);

查询工作与孙悟空相同的员工信息
select * from emp where job=(select job from emp where ename=‘孙悟空’) and ename!=‘孙悟空’;

  • 内连接查询:
select e.* from emp e join dept d on d.deptno=e.deptno where d.dname='神仙';

等值连接和内连接查询到的都是他们的交集数据

  • 外连接:(join前后两种表的顺序可以任意调换)

当需要使用一张表的全部和另外一张表的交集数据时使用外连接

select d.dname,e.ename from emp e right join dept d on d.deptno=e.deptno;
select d.dname,e.ename from dept d left join emp e on d.deptno=e.deptno;

关联总结: 如果需要同时查询多张表的数据,则使用关联查询 关联查询必须写关联关系 ,关联查询有三中方式:等值连接、内连接和外连接
查询两种表的交集数据时使用等值连接或者内连接(推荐) 查询两张表的一张的全部数据和另外一张的交接数据使用外连接

关联关系:指创建表时表于表之间的业务关系:一对一、一对多、多对多

猜你喜欢

转载自blog.csdn.net/qq_43050077/article/details/107177390