drop table if exists xzy1;
CREATE TABLE IF NOT EXISTS xzy1 (
id INT primary key auto_increment,
nick_name CHAR ( 32 ) ,
sex char(32),
age int,
money int
) DEFAULT CHARSET=utf8;
drop table if exists xzy2;
CREATE TABLE IF NOT EXISTS xzy2 (
id INT primary key auto_increment,
nick_name CHAR ( 32 ) ,
sex char(32),
age int,
money int
) DEFAULT CHARSET=utf8;
insert into xzy1 values
(1,"小明明","男",10,1000),
(2,"小花花","女",20,2000),
(3,"小明","男",30,3000),
(4,"小花","女",40,4000);
insert into xzy2 values
(1,"小灰灰","男",100,10000),
(2,"小红红","女",200,20000),
(3,"小灰","男",300,30000),
(4,"小红","女",400,40000);
1.多字段查询:
select nick_name,sex,money from xzy1
2.字段中的排序(仅在输出的时候排序)
select id,age from xzy1;#一般查询
select id,age from xzy1 order by age;#升序排序
select id,age from xzy1 order by age desc;#降序排序
3.分组查询
#表中存在的个数
select count(money) from xzy1;
#查询每个人钱数及相同钱数的个数
select money ,count(money) from xzy1 group by(money);
#加了个判断
select money ,count(money) from xzy1 group by(money) having count(money)>1;
4.方便查找的方法
select *from xzy1 where id=1 and age=10;
select *from xzy1 where id=1 or age=20;
select *from xzy1 where id in(1,2,3);
select *from xzy1 where id not in(1,2,3);
select *from xzy1 where age between 30 and 40;
select *from xzy1 where age not between 30 and 40;
5.子查询
把一个查询的结果当成另一个查询的条件进行使用
select * from xzy1 where id= (select id from xzy2 where age=200);
6.多表连接查询
#内连接(隐式连接)
select xzy1.id,xzy2.id,xzy1.money,xzy2.money from xzy2 , xzy1 where xzy1.id=xzy2.id
#外连接
select xzy1.id,xzy2.id,xzy1.money,xzy2.money from xzy2 left join xzy1 on xzy1.id=xzy2.id
#左外链接:left join
#理解为“有左显示”,比如on a.field=b.field,则显示a表中存在的全部数据及a、b中都有的数据,a中有、b中没有的数据以null显示
#右外链接:right join
#理解为“有右显示”,比如on a.field=b.field,则显示b表中存在的全部数据及a、b中都有的数据,b中有、a中没有的数据以null显示
#全连接:full join
#理解为“全连接”,两张表中所有数据都显示,实际就是inner +(left-inner)+(right-inner)
7.级联删除与级联更新:
在这里插入代码片
8.分隔符
默认的分割符为;
delimiter $$#分隔符修改为$$
9.函数:
系统函数
-- 查询mysql系统当前时间
SELECT NOW();
-- 查询当前mysql的版本
SELECT VERSION();
-- 查询当前所在的数据库
SELECT DATABASE();
-- 查询当前登录mysql的用户和主机
SELECT USER();
-- 获取一个字符串对应的md5值
SELECT MD5("HELLO");
聚合函数:
select count(age) from xzy1;#个数
select max(age) from xzy1;#最大
select min(age) from xzy1;#最小
select avg(age) from xzy1;#平均
自定义函数:
#格式:
create function myadd(a int,b,int) returns int
begin
declare i int;#变量的定义
set i= a+b;#给变量设置值
return i;#返回数据
end
#调用函数
select myadd(1,2);
#删除函数
drop function if exists myadd;
可能出现的错误:
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
原因是:
新版本中添加了
10.模糊查询
# % 匹配0-福讴歌任意字符
# _ 匹配任意一个字符
select * from xzy1 where nick_name like "%明%";
#找到了小明明和小明
select * from xzy1 where nick_name like "_明";
#只找到了小明
事务 !!!!:
事务是指:真个过程的没一个段远全部处理成功了,才会提交(commit)。
只要其中任意一点单元出现异常,就看看呀让事务回滚(rollback)。
特性: acid
atomicity(原子性):植物中的所有操作是一个整体,要不整体执行,要不全部失败。
consistency(一致性):指数执行前后的状态(数据)保持一致
isolation(隔离性):多个事务在执行的过程中互不影响。
durability(持久性):事物一旦被提交,既对数据库永久性的改变。即使在系统遇到故障之后,故障排除后这些数据也不会丢失。
事务的提交方式默认是自动提交,可以改成手动提交。
查询SHOW VARIABLES LIKE "%autocommit%"
start transaction; #开启事务
insert into xzy1 valuse(5,"小李","50",5000);
update xzy1 set age=10 where id=1;
rollback;#回滚事务
start transaction; #开启事务
insert into xzy1 valuse(5,"小李","50",5000);
update xzy1 set age=10 where id=1;
commit;#提交事务
隔离性:
事务隔离级别有以下几种:
Read Uncommitted(读未提交)
Read Committed(读已提交) ---->oracle中事务默认的隔离级别
Repeatable Read(重复读) —>mysql中的事务的隔离级别
Serializable(串行化)
#查询级别方式:
SELECT @@transaction_isolation
# 更改当前会话的隔离级别
set session transaction isolation level read uncommitted;
#设置当前会话隔离级别为读未提交
问题:
1、脏读:
脏读:在将事务级别设置为读未提交(read uncommitted),会读到其他事物没有提交的数据。
解决脏读:将事务级别更改成读已提交(Read Committed)
2、不可重复读问题
不可重复读:在一个事务中,前后两次读取的数据不一致
解决方法:将事务级别更改成重复读(Repeatable Read)
3、虚读、幻读
虚读:在设置前三种事务状态时,有可能读到其他事物新插入的行
解决方法:将事务级别更改成串行化(Serializable)
innodb锁机制:
两种锁机制:
乐观锁: 并不是硬编码的实现,而是通过version版本号来进行实现(并不能算是一种锁机制)
悲观锁:这种锁是表锁,又分为两种:
1.共享锁:(s锁也称读锁)
在设置共享模式的锁,这个共享锁允许你其他车的会话读取数据,但是不允许修改,如果其他的会话也需要修改数据,则要等待持有共享锁的会话结束锁的释放,才能修改数据。
可以在多个会话中加多个共享锁
添加多个共享锁容易出现相互等待释放的情况,造成死锁问题,使用多个共享锁的时候一定要谨慎
2.排它锁:(x锁也称写锁)
排它锁是不允许重复添加的
排它锁不存在死锁问题
加锁格式:
select * from xzy1 lock in share mode;#共享锁
select * from xzy1 for update;#排它锁