天河42期吕锡良 发表于 2022-6-6 15:28:23

MySQL学习笔记2

alter table ==>修改表,改变表格的格式结构的SQL语句

对表中数据的操作--增删改查---查
增--增加---对表格进行数据的插入--为了添加或者插入测试数据供我们进行测试某些测试不到的场景
改--修改--对表中原有的数据进行修改--为了修改测试数据供我们进行测试某些测试不到的场景
删--删除--对一些我们测试过程产生的脏数据(在测试过程一些不是程序正常生成的数据)进行清除处理
查--查询--对测试过程产生的数据进行检查核查

查--select
select * from 表; ==》查询所有字段内容来源于表

增--insert into
int、bigint、float、double只能插入数字,插入数据时不需要加引号把数据括起来
char、varchar插入字符串的,可以插入任意格式的数据,但是要加英文的成对引号
date插入日期格式的数据,也必须要加英文的成对引号

方式一:要求表的字段不多,对表的结构要熟悉
1,插入的值的数量必须要跟字段的数量对应上
2,值的格式必须要一一对应字段的要求
insert into 表名 values(字段1的值,字段2的值,字段3的值,。。。,字段7的值),(字段1的值,字段2的值,字段3的值,。。。,字段7的值);==>对一个有7个字段的表插入两行数据

方式二:对指定字段插入数据--常用的方式
insert into 表名(字段4,字段1,字段2) values(字段4的值,字段1的值,字段2的值),(字段4的值,字段1的值,字段2的值);==》对一张表中3个指定字段插入值,值的顺序按照指定字段的顺序进行插入

NULL和'   ':NULL是完全不占系统空间的,但是‘’空字符是占系统一个字节0的空间的

'NULL'==>这是一个字符串
NULL ==》这是表示 空值



改--update。。set。。
update 表 set 被改字段=值 where 条件字段=满足什么条件;

update test set sid=4 where name='xiaoming';
==》更新 test表 设置 sid的值为4 当 name字段等于'xiaoming'时


查--查询--select
select * from 表;==>查询 所有字段(*)的值 来自于 xx表;
select sid,name,age from emp; ==》

条件查询先接where(当满足。。。条件时)
select * from emp where dept2=101;==》查询所有字段内容来源于emp表,当dept2=101时

不等于 !=   <>(NULL值是不参与等于、不等于、大于小于这些条件匹配的)
select * from emp where dept2!=101;==》查询所有字段内容来源于emp表,当dept2不等于101时
select * from emp where dept2<>101;==》查询所有字段内容来源于emp表,当dept2不等于101时

多条件查询
and条件:要求两边同时满足
select *from emp where dept2=101 and incoming=3500;==》查询所有字段内容来源于emp表,当dept2字段等于101和incoming字段等于3500时
or条件:以or为分界,两边只要满足一个
select * from emp where dept2=101 or incoming=7500;==》查询所有字段内容来源于emp表,当dept2字段等于101或者incoming字段等于7500时

null的匹配 :is、is not
select *from test where age is null;==》查询所有字段内容来源于test表当age为null值时
select *from test where age is notnull;==》查询所有字段内容来源于test表当age不为null值时

大于小于的匹配:
select *from test where age>19 and age <=21;==》查询所有字段内容来源于test表,当age大于19且小于等于21
between 。。and 。。。:介于,是包含着等于的闭区间
select *from test where age between 19 and 21;

in与not in(与or条件类似)
select * from emp where age in (35,32,57);==》显示字段所有内容,来源于emp表,当age在35,32,57字段值;
select * from emp where age not in (35,32,57);==》显示字段所有内容,来源于emp表,当age不在35,32,57字段值;


在linux中模糊匹配符:‘*’,‘?’
但是在mysql中‘*’和‘?’有着其他作用
mysql中的模糊匹配符:‘%’,'_'
'%':匹配0个或者多个字符
‘_’:匹配一个字符
模糊匹配:like
select *from test where name ==》 '%m%';查询所有字段内容从test表当name字段包含m时。
select * from test where name like 'xiao____';==》查询所有字段内容从test表当name字段包含m时
select * from test where age like '2%';==》查询所有字段内容来源于test表,当age匹配2开头的字段内容
select * from test where time like '2022%';==》查询所有字段内容来源于test表,当time匹配2022开头的字段内容

查询限定的数据---分页查询 limit,只能放在SQL语句的末尾
select *from test limit 0,3;==》查询所有字段内容来源于test表,显示从0开始的3行数据
select *from test where time is not null limit 2,3;==》查询所有字段内容来源于test表,当time字段不为null,从第3行开始显示3行数据

select *from testlimit 0,3 where time is not null; ===》错误的写法==》

排序order by
如果结合limit使用,那么limit也是放在最后面
如果结合where条件查询,那么要先做条件查询,才可以做排序
asc表示升序、desc表示降序,默认是升序操作
select *from test order by age desc;
select *from test order by age asc;
select * from test where age is not null order by age asc limit 3; ==》先做where条件过滤,再做order by排序,最后做limit限定查询

select * from test order by phone asc,age asc; ==》对多个字段进行同时排序,谁在前谁的优先级更高(注意:order by排序最好不要同时对3个以上的字段进行排序)


分组查询:group by
注意:
1,分组查询要查询指定的字段,对什么字段进行分组就查询什么字段。因为分组之后,只有分组字段可以正常匹配,其他非分组字段(普通字段)是随机出现(一般是满足分组的第一条)
2,当使用了group by分组之后,要进行条件过滤,是不能使用where,where条件只能写在group by之前(先对表格的数据进行过滤,再进行分组)
3,如果希望在group by分组之后进条件过滤,就要使用having(作用有点类似where,但是只能写在group by分组之后)
4,group by分组一般是要结合 聚合函数使用,where条件是不允许接聚合函数,having才可以接聚合函数


select dept2 from emp group by dept2;==》查询dept2字段的值来源于emp表,对dept2进行分组过虑;
select dept2,count(*) from emp where dept2 !=101 group by dept2;==》查询dept2字段的值和统计行数,来源于emp表,当dept2不等于101时,并且对dept2进行分组过虑;
select dept2,count(*) from emp group by dept2 having dept2 != 101;==》查询dept2字段的值和统计行数,来源于emp表,对dept2进行分组过虑并且dept2不等于101
select dept2,count(*) from emp group by dept2 having count(*) >1;==》查询dept2字段的值和统计行数,来源于emp表,对dept2进行分组过虑并且dept2不等于101


聚合函数:(一般是结合分组查询使用,如果不使用分组查询,那么则是直接按照整个表格的情况进行处理)
count() 统计
select count(*) from 表; ==》统计表中的行数
select count(1) from 表; ==》统计表中的行数
select count(字段) from 表; ==》统计表中对应字段有多少条数据

sum()求和
select sum(incoming) from emp; ==》求emp表中的总收入==》对表中某个字段的值进行求和统计

avg()求平均
select avg(incoming) from emp; ==》求emp表中的平均收入 ==》对表中某个字段的值进行求平均

max() 求最大值
select max(incoming) from emp;==》求emp表中的最大收入 ==》对表中某个字段的值进行求最大值

min() 求最小值
select min(incoming) from emp;==》求emp表中的最小收入 ==》对表中某个字段的值进行求最小值

distinct()去重
select distinct(phone) from test;==》对test表中的phone字段的值进行去重复的操作,每种值只保留一个

SQL中进行四则运算:
select max(incoming),min(incoming),(max(incoming)-min(incoming))*100 from emp;==》查询incoming的最大值,incoming的最小值,incoming的最大值减去最小值的差乘以100,来源于emp表

as语句:取别名,可以对输入结果的字段名进行简化输出
select max(incoming),min(incoming),max(incoming)-min(incoming)*100 as '乱取的' from emp;



改--update。。set。。
update 表 set 被改字段=值 where 条件字段=满足什么条件;

update test set sid=4 where name='xiaoming';
==》更新 test表 设置 sid的值为4 当 name字段等于'xiaoming'时

update test set score =88.88;==》更新表test表
update test set name='laomai',age=28,score=100,time='1999-01-01' where sid=7;


删 --delete
delete from 表 where xx字段=满足什么条件;
delete from test where phone=17688889999 and time is null;==》删除表test表,当phone值是17688889999,并且 time 值 为null时
delete from 表;==>不限定条件删除表中数据,可以清空表中内容


面试题:
mysql中删除数据有多少种方式?
1,delete:可以根据条件删除指定的数据。如果不接条件删除时,可以清空表中数据,但是重新对表插入数据时,自增长字段还会在原来的最大值基础上进行累积递增(只删除数据,但没有释放空间)
2,truncate :truncate 表名; ==>直接清空表格中数据,不能接条件删除指定的数据。保留表结构,重新插入数据时,自增长会从1重新开始(清空表中数据并释放空间)
3,drop table +表名; ==》删除整张表,删除数据,表的定义也被删除





































页: [1]
查看完整版本: MySQL学习笔记2