目录
#3.51、比较运算符 >、<、 >=、<=、=、!=和<>(不等于)
#3.53、in(关键字) 当查询某个字段的值为多个值的时候使用
#3.54、between x and y 在x与y之间 包含x和y
#3.55、如果查询的有空值时使用 is not和is not null
一、数据库的基本操作
#1.1、新增数据库
新建school数据库,字符集默认
create database school;
新建school数据库,数据库字符集设置为utf8
create database school charset utf8;
#1.2、查询数据库
查询所有数据库
show databases;
查询 school 数据库字符集
show create database school;
#1.3、删除数据库
删除school数据库
drop database school;
#1.4、导入数据库数据
二、数据库表的基本操作
#2.1、进入需要操作的数据库
首先新建一个school数据库,然后进入school数据库进行操作
新建school数据库
create database school charset utf8;
进入school数据库操作
use school
#2.2、数据库新增表
建一个 id为int类型,name 为string类型的表,int类型限制不了字符长度,string类型用char、或varchar,固定长度用char效率高,长度不固定用varchar节省空间,varchar(255)字符长度为255
create table teachar(id int,name varchar(255)) charset utf8;
#2.21、新增表,并将id设为主键非空,name为非空
primary key 设为主键,唯一且非空即该字段不能重复且不能为空
auto_increment 给字段设为自增,只能给int类型字段设置,即新增数据时该字段如果为 null 则在新增的数据该字段为之前出现的最大数字加1,
not null 设为非空即该字段不能为空
create table teachar1(id int primary key auto_increment,name varchar(255) not null);
#2.3、查询表基本信息
查询当前数据库所有表
show tables;
查询当前数据库teachar表字符集
show create table teachar;
查询teachar表查询表字段信息
desc teachar;
#2.4、表信息操作
##2.41、修改表名
格式: rename table 原名 to 新名;
修改表名
rename table teachar to student;
##2.42、表字段操作
添加表字段位置
最后添加格式: alter table 表名 add 字段名 类型;
最前面添加格式: alter table 表名 add 字段名 类型 first;
xxx字段后面添加格式: alter table 表名 add 字段名 类型 after xxx;
给新增的表字段附加注释:comment '注释'
删除表字段
格式:alter table 表名 drop 字段名;
新增表字段
末尾新增age字段int类型,中文注释为年龄
alter table student add column age int comment '年龄';
在name字段后面新增salary字段int类型
alter table student add salary int after name;
在首位新增gender字段varchar类型
alter table student add gender varchar(1) first;
删除表字段gender
alter table student drop gender;
##2.43修改表字段
格式: alter table 表名 change 原名 新名 新类型;
表字段age改为gender,类型为varchar
alter table student change age gender varchar(1);
三、表数据操作
#3.1、表数据插入
格式:insert into 表名(字段1名,字段2名) values(值1,值2),(值1,值2),(值1,值2);
新增一条数据
insert into student(id,name,salary,gender) values(1,'jack',100,'男');
或
insert into student values(1,'jack',100,'男');
新增一条信息,只增加id,name字段
insert into student(id,name) values(1,'jack');
新增多条数据
insert into student values(1,'jack',100,'男'),(1,'jack',100,'男');
#3.2、表数据修改
格式:update 表名 set name='陈'(修改的内容) where id=1(条件);
将student表id为1的name字段数据改为陈
update student set name='陈' where id=1;
将student表id为1的name字段数据改为陈,salary字段改为200
update student set name='陈',salary=200 where id=1;
#3.3、表数据查询
格式:select*from 表名;
查询student表所有数据
select*from student;
#3.4、表数据删除
格式:delete from 表名 where 条件
删除student表字段id为1的数据
delete from student where id=1;
删除student表所有数据
delete from student
#3.5、表数据运算符和查询空值
#3.51、比较运算符 >、<、 >=、<=、=、!=和<>(不等于)
#3.52、与或非 and(与),or(或),not(非)
#3.53、in(关键字) 当查询某个字段的值为多个值的时候使用
查询工资sal为3000,1500和5000的员工信息
select * from emp where sal=3000 or sal=1500 or sal=5000;
用in可改为
select * from emp where sal in(3000,1500,5000);
#3.54、between x and y 在x与y之间 包含x和y
查询工资sal在1000到2000之间的员工信息
select * from emp where sal between 1000 and 2000;
#3.55、如果查询的有空值时使用 is not和is not null
1. 查询有领导manager的员工姓名和领导id
select name,manager from emp where manager is not null;
2. 查询没有领导manager的员工姓名和领导id
select name,manager from emp where manager is null;
#3.56、去重distinct 查询时出现重复的可去除
格式:select distinct 查询字段 from 表名
#3.561、去重前
查询员工表中出现了哪几种不同的工作
SELECT job FROM emp;
#3.562、去重后
查询员工表中出现了哪几种不同的工作
select distinct job from emp;
#3.6、模糊查询
格式:select * from 表名 where name like "孙%"; 查询name字段以孙开头的信息
select * from 表名 where name like "%孙"; 查询以孙结尾
select * from 表名 where name like "%孙%"; 查询包含孙字
select * from 表名 where name like "_孙"; 查询第二位为孙字的
select * from 表名 where name like "_ _孙"; 查询第三位为孙字
select * from 表名 where name like "孙_"; 查询倒数第二位为孙字
#3.7、排序
格式: 在表名或条件后面添加 order by 排序字段名 asc(默认升序)/desc(降序)
查询emp表信息按 sal 字段从小到大排序
select*from emp order by sal;
或
select*from emp order by sal asc;
查询emp表信息按 sal 字段从大到小排序
select*from emp order by sal desc;
查询emp表工资大于3000的信息,并按 sal 字段从小到大排序
select*from emp where sal>3000 order by sal;
#3.8、分页查询
格式: 在SQL的最后添加limit 跳过的条数,请求的条数(每页的条数) 举例: 跳过的条数=(请求的页数-1)*每页条数 - 第一页的5条数据 limit 0,5 - 第2页的5条数据 limit 5,5 - 第5页的5条数据 limit 20,5 - 第8页的10条数据 limit 70,10 - 第9页的8条数据 limit 64,8
查询工资 sal 最高的前三个员工
select * from emp order by sal desc limit 0,3;
查询工资最三的员工
select * from emp order by sal desc limit 2,1;
查询员工表, 第2页的5条数据
select * from emp limit 5,5;
四、数值计数(求平均值,最大值,最小值,求和,计数)
#4.1、 平均值avg(字段名)
查询emp表的平均工资,工资(sal)
select avg(sal) from emp;
#4.2、 最大值max(字段名)
查询 emp 表的最高工资sal
select max(sal) from emp;
#4.3、 最小值min(字段名)
查询emp表的最低工资
select min(sal) from emp;
#4.4、 求和sum(字段名)
一个月所有员工的工资(sal)的总和
select sum(sal) from emp;
#4.5、 计数count(*)
查询emp表的信息条数
select count(*) from emp;
查询emp表工作(job)为程序员的人数
select count(*) from emp where job="程序员";
五、分组查询
#5.1、基础查询
将某个字段相同值的数据划分为一组,然后以组为单位进行统计查询 group by 字段名
查询emp表每种工作(job)的人数
select job,count(*) from emp group by job;
查询后
#5.2、聚合函数条件查询
here后面不能写聚合函数条件, 只能写普通字段条件 having后面写聚合函数条件, 而且要和group by分组查询结合使用
查询emp表每种工作(job)的人数 要求人数大于1
select job,count(*) from emp group by job having count(*)>1;
用c作为count(*)的别名
select job,count(*) c from emp group by job having c>1;
六、多表查询
格式: select * from A join B on 关联关系 where 条件;
select * from A ,B where 关系;
内连接:join
左外连接:left join
右外连接:right join
查询emp表的所有信息和dept表与之对应的信息,
两表有关联的字段emp表的dept_id字段和dept表的id字段
select*from emp,dept where emp.dept_id=dept.id;
或
select*from emp join left dept on emp.dept_id=dept.id;
或
select*from emp join dept on emp.dept_id=dept.id;
查询右表dept的所有信息和左表emp的对应信息
select*from emp right join dept on emp.dept_id=dept.id;
切割查询到的数据
#查询table表time字段左边4位字符=2023
select * from table where left(time, 4)=2023
#查询table表time字段右侧(数据后4位)4位字符=2023
select * from table where right(time, 4)=2023