MySQL学习笔记
常用数据类型
- 整数类型
int:常规整数类型tinyint:取值范围为-128到127
- 小数类型
decimal(5,2):总长度为5位,其中小数位占2位
- 字符串类型
varchar(3):存储长度为3的可变长度字符串
- 日期时间类型
datetime:用于存储日期和时间
- 文本类型
text:用于存储大量文本数据
表操作
创建表
create table a(id int , name varchar(10) );
上述语句创建表a,包含两个字段:id(整数类型)和name(长度为10的字符串类型)
插入数据
insert into a values (1 , 'wangwaiwai') , (2 , 'waiwaiwang');
此语句向表a中插入两行数据
查询操作
select id,name from b;
从表b中查询id和name两列数据
更新操作
update a set name = 'newname' where id = 1;
将表a中id为1的记录的name字段更新为newname
删除操作
delete from a where id = 4;
从表a中删除id为4的记录
表结构修改
alter table b add column sex varchar(10);
向表b中添加一个名为sex,类型为长度10的字符串的新列
删除操作的区别
| 操作 | 特点 |
|---|---|
delete | 可带条件,逐条删除数据,会记录日志 |
truncate | 删除表中所有数据,不记录日志,速度比delete快,无法回滚 |
drop | 删除整个表结构及数据 |
字段约束
create table c (
id int primary key auto_increment ,
name varchar(20) not null ,
age tinyint unsigned default 18
);
primary key:主键约束,确保字段值唯一且非空auto_increment:自动递增,常用于id字段not null:非空约束,字段必须有值default:默认值,当插入数据未指定该字段值时使用默认值
条件查询
比较运算符
select * from b where id < 1;
支持<、>、<=、>=、!=等比较运算符
逻辑运算符
select * from b where score > 60 and sex = '女';
使用and、or、not进行逻辑判断,not也可用!=替代
模糊查询
-- 查找第四个字是'歪'的数据
select * from b where col like '___歪';
-- 查找以'歪'结尾的数据
select * from b where col like '%歪';
-- 查找包含'歪'的数据
select * from b where col like '%歪%';
使用like关键字,_代表一个字符,%代表任意字符
范围查找
-- 使用between and
SELECT * FROM b where id BETWEEN 1 AND 3;
-- 使用in
select * from b where sex in ('man' , 'women');
between and指定范围,in指定多个值
null判断
select * from b where age is null;
使用is null判断字段值是否为null
别名使用
select * from student s where s.id = 1;
使用as(可省略)给表或字段起别名,方便多表查询和区分重复字段名
排序操作
-- 升序(默认)
select * from b where sex = 'man' order by age;
-- 降序
select * from b where sex = 'man' order by age desc;
使用order by进行排序,asc表示升序(默认),desc表示降序
聚合函数
-- 统计不重复的id数量
select count(distinct id) from b;
-- 查询最大分数
select max(score) from b;
-- 查询最小年龄
select min(age) from b;
-- 求和与求平均
select sum(score), avg(age) from b;
常用聚合函数包括count、max、min、sum、avg,不能直接用于where条件
数据分组
select sex, count(*) from b where age <19 group by sex order by id desc;
先使用where过滤数据,再通过group by进行分组,最后用order by排序
having与where的区别
select class from b group by class having count(*) > 3;
where:在分组前过滤数据,不能使用聚合函数having:在分组后过滤数据,可以使用聚合函数
limit分页
-- 查询前两条数据
select * from b limit 2;
-- 从第三条开始查询两条数据
select * from b limit 2, 2;
limit用于限制查询结果的数量,limit offset, count中offset为起始偏移量,count为查询数量
联表查询
内连接
select * from table1 t1 inner join table2 t2 on t1.id = t2.id where t1.id = 13;
只返回两个表中满足连接条件的匹配行
左连接和右连接
left join:返回左表的所有行,以及右表中匹配的行,右表无匹配数据时显示为nullright join:返回右表的所有行,以及左表中匹配的行,左表无匹配数据时显示为null
子查询
SELECT * FROM student s WHERE (SELECT AVG(age) FROM student) < s.age AND sex = '男';
子查询嵌套在where条件中,用于更复杂的查询逻辑,但效率相对较低
存储过程
创建存储过程
create procedure a()
begin
declare i int default 1;
while i <10000
do
insert into user values (i,concat ('abc' , i),13000000+i);
set i = i +1;
end while;
end
上述存储过程向user表中批量插入9999条数据
调用存储过程
call a();
使用call语句调用存储过程,在变量前加@可使变量在当前会话中保持状态,实现多次调用的连续操作
Redis相关
Redis是一种非关系型数据库,基于内存存储,适用于高并发场景。若出现大量请求绕过Redis直接访问MySQL导致数据库崩溃,可能是Redis内存不足,可通过升级服务器内存或在后端使用队列(如栈、堆实现的队列)来控制请求流量,防止内存过载
MySQL删除方式
物理删除
使用delete语句直接删除数据,会记录日志,可回滚
逻辑删除
通过更新状态码实现(如0表示下架,1表示上架),保留数据记录,符合数据留痕的业务需求
2万+

被折叠的 条评论
为什么被折叠?



