mysql学习总结

mysql是干什么的

mysql是一个轻量级数据库软件,主要用来存储数据,让内容拥有可持久性

SQL分类

DDL 数据库定义语句,用于数据库,表,列的管理

DML 数据库表中的数据,表数据的增删改

DQL 表数据的查询

DCL 权限控制

TCL 事务控制

CCL 指针控制

约束

非空约束 not null

唯一性约束 unique

默认值约束 defaut

主键约束 primary key

检查约束 check

自动增长 auto_increment

聚合函数

MAX 最大值

MIN 最小值

AVG 平均值

SUM 和

COUNT 计数

distinct 去重

分组与排序

group by 列名 (分组 )

order by 列名 (排序) desc 从大到小  asc 默认 从小到大

关联查询

左连接 select * from 表1 left join 表2 on 表1.列n=表2.列n

右连接 left 改成right

左外 select * from 表1 left join 表2 on 表1.列n=表2.列n where 表1.列n is null

右外

内连接 select * from 表1 inner join 表2 on 表1.列n=表2.列n

外连接 左外加右外

全连接 左外加右连接   左连接加右外连接

笛卡尔积 select * from 表1,表2 where 表1.列n=表2.列n

子查询

select 子查询

where 子查询

from 子查询

exists 子查询

-- 子查询
--select 子查询(外语句先执行,后执行子语句)
-- 查询课程表并显示课程老师的名称
select *,(select username from t_test1 t1 where t1.id=t2.id)name from t_test2 t2

--where 子查询(先执行子查询,再执行外查询)
-- 查询学了体育课程的学生
select st.* from student st where st.sid in(
	select sc.student_id from score sc
	left join course co on co.cid=sc.course_id
	where co.cname='体育'
)
--from 子查询(先执行子查询,再执行外查询)
-- 	列出三年二班学了体育的学生
	select a.* from (select * from score sc
	left join course co on co.cid=sc.course_id
	left join student st on st.sid=sc.student_id
	left join class cl on cl.cid=st.class_id
	where co.cname='体育') a where a.caption='三年二班'
	
--exists 子查询(可以用来代替in)(先执行外语句,再执行子查询,根据子查询是否)
--  查询学了课程id为1的学生的姓名
select * from student st where st.sid in (
select sc.student_id from score sc where sc.course_id='1'
)
	select * from student st where exists(
		select * from score sc where sc.course_id='1' and sc.student_id=st.sid
	)

--any
	-- 查询比'01'课程成绩高的其他课程
	select sc1.course_id from score sc1 where sc1.num <= any(select num from score sc where sc.course_id = '01')
	-- 查询比'01'课程任意成绩高的其他课程
	select sc1.course_id from score sc1 where sc1.num > all(select num from score sc where sc.course_id = '01')
	
	
			

数字函数

select round(1.5)
select rand() 

字符串函数

select concat('1','2')拼接字符串
select substring('helloword' from 6 for 5)截取字符串
select reverse('hello')字符串反转

日期和时间函数

select now() --取当前时间
select date_format(new(),'%y%m%d %H%i%s')--取年月日时分秒

条件判断函数

select st.sname,sc.course_id,ifnull(num,0) from student st
        left join score sc on sc.student_id = st.sid
        where st.class_id = '3'
        
        select if(year(now())=2012,'是2022','不是')

系统信息函数

-- 系统版本信息
        select VERSION()

加密函数

-- 加密函数
        select password('aaa')
        select MD5('123');--MD5加密是不可逆的

其他函数

-- 其他函数
        select format('1.2345',1)

limit关键字与分页查询

-- 分页函数limit
                -- n 第一页
                -- m  3 m=(n-1)*3
        select * from student limit 15,3

数据库事务

事务是指程序的某一个操作单元

事务的特性

原子性 即要么全部都执行要么都不执行

一致性 即最终结果不会改变

隔离性 事务与事务之前互补影响

持久性 一个事务一旦提交,他修改的数据会永久的保存在数据库中

开始事务 begin 

提交事务 commit

回滚事务(即回退上一步) rollback

事务的隔离级别

并发事务带来的问题

1.更新丢失Lost Update:事务A和事务B同时操作,事务B覆盖了事务A做的操作,导致事务A的更新丢失

2.脏读Dirty Reads:事务A读取到事务B还未提交的事务

3.不可重复读Non-Repetable Reads:事务A在同一事务的不同时间段内,读取同一数据,得到的值不一样(读取到其他事务修改的值)

4.幻读Phantom Reads:事务A在同一事务的不同时间段内,第n次读取的结果行与第n+次读取的结果行数不一样(读取到其他事务新增的内容)

事务的隔离级别

读未提交(READ UNCOMMITTED):能读取到其他事务未提交的数据

读已提交(READ COMMITTED):只能读取到其他事务已提交的数据

可重复读(默认隔离)(REPEATABLE READ):事务A在同一事务的不同时间段内,读取统一数据,得到的值一样(即使其他事务修改了值)

串行化(SERIALIZABLE):事务依次执行,不存在并发问题

数据库的三大范式

  1. 设计表时,应尽可能保证表中的属性不可再分割
  2. 非主属性必须完全依赖与主属性
  3. 属性不依赖与其他非主属性

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值