MySQL指令大全

-- 创建数据库
create database name;

-- 创建数据表
create table name(
	id int not null primary key auto_increment,
	name varchar(20)
);

-- 查询所有
select * from tablename;

-- 不重复查询某一列 distinct
select DISTINCT name from student;

-- 插入用户
insert into student values(DEFAULT,'甄姬','女','1995-7-9');

-- 查询范围最简单的
select `name`,`score` from course where score > 4 and score < 6;

-- 查询范围用between ... and ...
select `name`, `score` from course where score between 4 and 6;

-- 表示或者的关系 in
select * from course where score in (3,4);

-- 表示或者的关系用or
select * from student where sex = '男' or name = '甄姬';

-- 升序排列 asc(默认就是升序)
select * from course order by score asc;

-- 降序排列 desc
select * from course order by score desc;

-- 以某一个字段升序,某一个字段降序
select * from course order by score asc, degree desc;

-- 查询某个列总人数count(字段)
select count(*) from student where sex = '女' ;

-- 查询某个字段的最大的数字max(字段)
select * from course where score=(select max(score) from course);

-- 查询某个字段最大数还可以先降序排列,然后limit 0,1 ,就把第一个值取出来了
-- limit 第一个数是起始位置,第二个数字是差几条,分页就是这样实现的
select * from course order by score desc limit 0,1;

-- 计算平均成绩avg(字段)
select avg(score) from course ;

-- 模糊匹配:x%,以x开头,%是通配符,匹配任意字符
-- 过滤分组后的数据having, 配合 group by使用
-- 例:查询score表中至少两名学生选修并且以3开头的课程
select cno, avg(degree),count(*) from score group by cno
having count(cno)>=2 and cno like '3%';

-- 多表查询
select sname, cno, degree from student,score where student.sno = score.sno;

-- 如果有date类型的字段,你想从1998-06-10中找到年份怎么办?year(date类型的字段)
select YEAR(born) from student where sex = '女';

-- 联合查询 union
select * from teacher where depart = '计算机系'
union
select * from teacher where depart = '软件工程系';

-- 任意一个 any 
select * from course where score > any(select score from course2);

-- 同上,所有是 all
select * from course where score > all(select score from course2);

-- as 取别名
select score as grade from course;

-- not like 模糊查询取反
select * from course where name not like '英%';

-- 求年龄使用now()算今年是几几年-year(字段)
select name,year(now())-year(born) as age from student;

-- 内联查询,其实就是两张表的数据,通过某个字段相等,查出相关记录数据
select * from person inner join card on person.cardId = card.id;

-- inner join = join
-- 左外连接会把左边表的所有数据取出来,如果右边的表中有相同数据则显示,没有则显示NULL
select * from person left join card on person.cardId = card.id;

-- left join = left outer join

-- 右外连接跟左外连接相反

-- start transaction 或者 begin;开启事务

-- 事务四大特征:
-- A:原子性 事务是最小单位,不可再分
-- C:一致性 事务要求同一SQL语句要么同时成功要么同时失败
-- I:隔离性 事务与事务之间是隔离的
-- D:持久性 事务一旦(commit),则不可返回

-- 事务开启的三种方式:
-- 1set autocommit = 0(默认是1)
-- 2、begin;
-- 3、start transaction;

-- 事务提交
-- 1、commit;

-- 事务回滚
-- 1、rollback;

-- 查询事务隔离级别 8.0以上
-- 系统级别
select @@global.transaction_isolation;
-- 会话级别
select @@transaction_isolation;

-- mysql 5.X
select @@global.tx_isolation;
select @@tx_isolation;

-- 修改事务级别
set global transaction isolation level read uncommitted;

-- 脏读
-- 如果一个事务没提交,但是被另一个事务读到了,就叫脏读,在实际开发中是不允许的

-- 不可重复读
-- 虽然读到了另一个事务提交的数据,但是读到同一个表的数据,发现前后不一致,就是不可重复读,read committed

-- 幻读
-- 事务a和事务b操作同一张表,事务a提交了但是
-- 事务b没更新,就可能出现幻读

-- 串行化
-- 当一个表被一个事务操作的时候,另外那个事务不可执行操作,这就叫串行化,除非前者提交了,后者才能执行操作,否则一直等待
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bean冷的心

你的鼓励将是我前进的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值