MySQL复习
文章目录
- MySQL
- 如何保证完整性和一致性?
- 约束:
- 1.数据类型约束
- 1.1整数类型
- 1.2小数类型
- 1.3字符串
- 1.4日期类型
- 1.5其他类型
- 2.自增长约束
- 3.主键约束 primary key
- 4.非空约束
- 5.默认值约束
- 6.检查约束
- 7.外键约束
- 索引:
- 视图
- 函数
- 存储过程
- 触发器:
- SQL
- DML 数据操纵语言
- DQL 数据查询语言
- 一、基础查询
- 二.多列排序
- 三.子查询(嵌套查询)
- 四.表连接 将两张表的数据显示到一个结果集中
- 五。联合查询
- DBL
- DCL
MySQL
表:存储结构化数据,格式化数据
非结构化数据:音频,视频。。。
SQL擅长使用表来存储结构化数据
如何保证完整性和一致性?
约束:
1.数据类型约束
1.1整数类型
tinyint 1个字节
mediumint 3个字节
int 4个字节 -21亿 ~ 21亿 无符号:0 ~ 42亿
bigint 8个字节
1.2小数类型
float 4个字节
double 8个字节
decimal定点小数
1.3字符串
char 定长字符串
定长,如下定长20,不够20自己补齐20位,性能高,找的时候好定位位置,比如性别,手机号
varchar 不定长字符串
1.4日期类型
data 日期
time 时间
datetime 日期和时间
timestamp 以整数记录时间 最节省空间,不易读,从1970年1月1号到现在的毫秒数
1.5其他类型
bit位 1个bit经常用于布尔值
enum 枚举 用的很少
json很鸡肋
text 大文本 几GB都可以存,字符串存不合适
blob 存大的二进制,大文件 ,比如大的视频,但现在很少往数据库存视频,还会严重影响数据库性能
2.自增长约束
只能整数类型支持
3.主键约束 primary key
用来唯一标识一个实体的作为主键,比如学号,id
主键不能为空
一个表 两个主键标识,联合主键,也就是把选中的两列合起来作为主键
4.非空约束
可选,主键一定不为空
5.默认值约束
6.检查约束
不建议使用(在dba的角度要用,在程序员角度不爱用 --不够灵活,难以调试)
7.外键约束
不建议使用(在dba的角度要用,在程序员角度不爱用 --不够灵活,难以调试)
至少两个表。
比如一个班级删除的时候,学生表里的是这个班级的学生的学生信息一并删除
不做操作:
提示错误信息
/////////////////////////////////////////////////////////////////////////
索引:
unique唯一索引
normal普通索引
索引方式:b+树
视图
函数
存储过程
触发器:
////////////////////////////////////////////////////////////////////////
SQL
结构化查询语言。
DML 数据操纵语言
数据操纵语言。写操作,增insert,删delete,改update
DQL 数据查询语言
数据查询 select
一、基础查询
- 查询所有数据
SELECT * from t_student;
- 限定列查询
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student;
- 列起别名,表起别名 AS 可以省略
SELECT id AS 编号,stu_id AS 学号,`name` 姓名,pinyin 拼音,sex 性别,birthday 出生日期
FROM t_student AS t;
- 列运算
SELECT id,stu_id,`name`,pinyin,height+10 修正身高,weight-10 修正体重 FROM t_student;
- 限定行查询 可用于分页
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student LIMIT 10;
-- 第一个参数表示起始行(从0开始),第二个参数表示查询行数
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student LIMIT 10,10;
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student LIMIT 10 OFFSET 10;
- 指定查询条件where。where后指定查询表达式。支持算术运算符、比较运算符、逻辑运算 符、其它
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE id =5;
-- 不等于。<> !=
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE id<>5 LIMIT 10;
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE id!=5 LIMIT 10;
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE id<=5 LIMIT 10;
-- 仅限mysql,日期也可以比较大小
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE birthday > '2000-01-01';
- 算术运算符 也可以用在查询条件中(支持小括号提升优先级)
SELECT id,stu_id,`name`,pinyin,sex,birthday ,weight FROM t_student WHERE weight/((height/100)*(height/100))>24;
- 逻辑运算符 and or not
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE height >=175 AND sex = '女';
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE Not sex = '女';
- 空判断
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE class_id is NULL;
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE class_id is NOT NULL;
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE NOT class_id is NULL;
- 通配符 %代表0或多个字符
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE`name` LIKE '张%';
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE`name` LIKE '%晓%';
-- 下划线代表一个字符
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE`name` LIKE '张__';
- 正则查询,正则表达式是用于字符串匹配模式用 REGEX或者RLIKE 名字是3个以上字符的
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE `name` REGEXP '\\w{4,}';
SELECT id,stu_id,`name`,pinyin,sex,height,weight FROM t_student WHERE `name` RLIKE '\\w{4,}';
SELECT id,stu_id,`name`,pinyin,sex,height,weight,wechat FROM t_student WHERE `wechat` NOT RLIKE '\\d{11}';
- 结果集去重 DISTINCT 去重慎用,影响性能
SELECT sex FROM t_student LIMIT 20;SELECT DISTINCT sex FROM t_student LIMIT 20;
- 查询结果排序 mysql默认按照数据插入的顺序进行排序
SELECT id FROM t_student
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student limit 20;
SELECT id,stu_id,`name`,pinyin,sex,height,birthday FROM t_student ORDER BY height ASC limit 200 ;
SELECT id,stu_id,`name`,pinyin,sex,height,weight,birthday FROM t_student ORDER BY weight DESC limit 200 ;
二.多列排序
- seclect后只能是分组列和聚合函数列
聚合函数 avg,max,min,count,对数字进行聚合运算
SELECT sex,AVG(height)FROM t_student GROUP BY sex;
mysql 5不报错,可以写name
:
SELECT sex, `name` FROM t_student GROUP BY sex;
SELECT sex,MAX(weight),MIN(weight),COUNT(0) FROM t_student GROUP BY sex;
SELECT class_id,MAX(height),MAX(weight) FROM t_student GROUP BY class_id;
- 多次分组
SELECT class_id,sex,MAX(height),MAX(weight) FROM t_student GROUP BY class_id,sex ORDER BY class_id,sex;
- 无分组或单分组。
- count中的参数,表示根据哪一列来统计行数。
SELECT count(0) From t_student ; -- 常数列
SELECT count(*) From t_student ;
SELECT count(id) From t_student ;
-- 注意count不统计null值
SELECT count(sex) From t_student ;
SELECT count(DISTINCT class_id) From t_student ;
SELECT sex,AVG(DISTINCT height) From t_student GROUP BY sex;
4.分组之后的条件筛选
where是分组之前筛选 having是分组之后筛选
SELECT class_id,sex,MAX(height) mh,MAX(weight) mwFROM t_student GROUP BY class_id,sex HAVING mh >183 AND mw>95ORDER BY class_id,sex ;
三.子查询(嵌套查询)
- 列子查询
SELECT id,stu_id,`name`,(SELECT `class_name`FROM t_class WHERE id = class_id ) class_name,class_id,sex,birthday FROM t_student LIMIT 10;
- 表子查询
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM (SELECT *FROM t_student WHERE sex = '女')t1;
- 在where中比较运算符子查询
-- 等号(不等号)子查询 要求子查询结果必须是一行一列SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE class_id = (SELECT id FROM t_class WHERE class_name = '080503-JAVA'); SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE class_id != (SELECT id FROM t_class WHERE class_name = '080503-JAVA'); -- 大于号子查询,小于号子查询SELECT id,stu_id,`name`,pinyin,sex,birthday,class_id FROM t_student WHERE class_id > ALL(SELECT id FROM t_class WHERE class_name LIKE '%JAVA%'); SELECT id,stu_id,`name`,pinyin,sex,birthday,class_id FROM t_student WHERE class_id > ANY(SELECT id FROM t_class WHERE class_name = '080503-JAVA');-- 4.in 和not in 子查询SELECT id,stu_id,`name`,pinyin,sex,birthday,class_id FROM t_student WHERE id in(1,2,3);SELECT id,stu_id,`name`,pinyin,sex,birthday,class_id FROM t_student WHERE id in(SELECT id FROM t_class WHERE class_name LIKE '%JAVA%');
- exists/not exists 子查询 唯一的断言类查询
SELECT id,stu_id,`name`,pinyin,sex,birthday,class_id FROM t_student WHERE EXISTS(SELECT id From t_class WHERE class_name LIKE '%JAVA%')
- 相关子查询
-- 查询出比所在班平均身高还要高的学生SELECT id,stu_id,`name`,pinyin,sex,birthday,class_id FROM t_student t1WHERE height>(SELECT AVG(height) FROM t_student t2 WHERE t2.class_id = t1.class_id);
四.表连接 将两张表的数据显示到一个结果集中
- 内连接:连接条件列,在左右两侧都必须存在
SELECT t1.id,stu_id,`name`,pinyin,sex,birthday,class_id,t2.class_name,t2.begin_time
FROM t_student t1
INNER JOIN t_class t2 ON t1.class_id =t2.id ORDER BY t1.id;
- 左外连接
-- 左表数据一定显示,右表连接不上时显示为NULL;
SELECT t1.id,stu_id,`name`,pinyin,sex,birthday,class_id,t2.class_name,t2.begin_time
FROM t_student t1
LEFT JOIN t_class t2 ON t1.class_id =t2.id ORDER BY t1.id;
3.右外连接
-- 右表数据一定显示,左表连接不上时显示为NULL;
SELECT t1.id,stu_id,`name`,pinyin,sex,birthday,class_id,t2.class_name,t2.begin_time
FROM t_student t1
RIGHT JOIN t_class t2 ON t1.class_id =t2.id ORDER BY t1.id;
- 全外连接 mysql不支持
-- 左表右边数据全部显示,左表右表连接不上时显示为NULL;
SELECT * FROM(
SELECT t1.id,stu_id,`name`,pinyin,sex,birthday,class_id,t2.class_name,t2.begin_time
FROM t_student t1
LEFT JOIN t_class t2 ON t1.class_id =t2.id ORDER BY t1.id) t1
UNION
SELECT *FROM(
SELECT t1.id,stu_id,`name`,pinyin,sex,birthday,class_id,t2.class_name,t2.begin_time
FROM t_student t1
RIGHT JOIN t_class t2 ON t1.class_id =t2.id ORDER BY t1.id) t2;
五。联合查询
-- 注意:1不需要是同一张表 2.列数必须一致 3,数据类型基本匹配 4,union会去重,union all不去重
-- union关键字
-- 1.
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE sex = '男'
UNION
SELECT id,stu_id,`name`,pinyin,sex,birthday FROM t_student WHERE sex = '女';
DBL
数据定义语言。create alter drop创建库创建表创建存储过程
DCL
数据控制语言。用户,角色,权限。
注意事项:
1.sql中不区分大小写
2.表与表之间有关系
3.dba:database adminstrator