一、数据类型
1、整数类型:
int int后的宽度是显示宽度,不是存储宽度,所以不用加宽度。
2、日期类型:
year 年
date 日
time 时分秒
datetime 年月日时分秒,8个字节
timestamp 年月日时分秒,4个字节
now() 当前时间,根据字段约束填入表中
3、字符类型
char(5) 定长的字符
varchar(5) 可变长字符
字符类型的宽度指的是字符的长度;
char 不够5个,自动补全5个,但是在取数据或比较的时候会不要脸的把后面的空格去掉。
varchar 先用一个字节存字符的长度,后面存字符;比如'shi'先用一个字节存3,再存'shi'。
如果字符的长度超过255,那么就用两个字符表示长度,最长65535。
所以长的字符存在文件服务器,而数据库尽量的精简。
所以不能单纯的说 varchar 比 char 省空间
char 优点:存取速度快;缺点:占空间
varchar 优点:省空间;缺点:因为要先读取长度,所以存取速度慢。
大部分用 char,现在空间不是什么问题,更重要的是效率。
4、枚举类型和集合类型
枚举类型:enum('男','女')
集合类型:set('a','b','c','d')
二、约束条件
1、not null
2、default
create table t(
id int,
sex enum('male','female') not null default 'male'
);
3、unique
单列唯一
联合唯一 unique
create table t(
id int unique,
ip char(15),
port int,
unique(ip,port)
);
4、primary key
主键的作用:组织表的结构,提高查询速率,不仅仅是不为空且唯一。
存储引擎innodb:一张表必须要有一个主键,
你没指定会找一个不为空且唯一的字段为主键,如果再没有会自动设置一个隐藏的主键,隐藏的没有意义,所以你一定要指定。
单列主键:一般为每一张表设置一个ID字段为主键。
复合主键:
create table t(
ip char(15),
port int
primary key(ip,port)
);
5、auto_increment
自增:步长默认为1,起始偏移量为1。
create table t(
id primary key auto_increment,
name char(5)
);
清空表:
delete from t; 只清除记录,一般与where连用,删除指定的记录。
truncate t; 清除记录,并恢复起始偏移量为1
6、foreign key
create table t(
id int primary key,
name char(10),
dep_id int,
foreign key(dep_id) references dep(id)
on delete cascade # 联合删除
on update cascade # 联合更新
);
三、两张表之间的关系
1、多对一
在多的一张表建立外键
2、一对一
外键加唯一
3、多对多
创建第三张表
四、单表查询
1、简单查询
select * from t;
2、distinct去重
select distinct * from t;
3、将字段直接进行四则运算
select name, salary*12 as '年薪' from t;
4、定义显示格式
select concat('姓名:',name, '性别:',sex) as info from t;
select concat_ws(':', name, sex, age) from t;
5、where 约束
比较运算符 < > =
between and
in(1,2,3)
like %任意多个字符; _任意一个字符
逻辑运算符 not and or
6、group by 分组
group by 是在 where 之后运行
设置严格分组模式: set global sql_mode="ONLY_FULL_GROUP_BY";
聚合函数:count() sum() avg() max() min()
强调:
不要用unique的字段作为分组字段,没有意义;
如果不分组,那么整体就是一组;
分组之后,只能取分组的字段,以及每个组聚合的结果
group_concat 列举每个组的所有记录
select group_concat(name) from t group by post;
7、having 过滤
在分组之后进行,可以聚合函数作为条件,where 不可以用聚合函数作为条件。
8、order by 排序
在 distinct 之后运行
order by asc 升序,默认的
order by desc 降序
order by age asc id desc 先按照年龄升序,再按照id降序。
9、limit 限制条数
limit 3 从头开始,数三条记录
limit 10,10 从第10条开始,取10条记录
10、正则匹配
regexp '^shi' 匹配以'shi'开头的
总结:单表查询的语法以及执行顺序。
语法顺序:
select distinct 字段一,字段二,字段三 from 库.表名
where 条件
group by 分组条件
having 过滤
order by 排序字段
limit n;
执行顺序:
from 找表
where 条件过滤
group by 分组
having 过滤
distinct 去重
order by 排序
limit 分页
select 打印
五、多表查询
在笛卡尔积的基础上去筛选有关系的记录
1、内链接
select * from
tb11 inner join tb12
on tb11.part_id=tb12.department.id;
2、左链接
select * from
tb11 left join tb12
on tb11.part_id=tb12.department.id;
3、右链接
select * from
tb11 right join tb12
on tb11.part_id=tb12.department.id;
4、全外链接
select * from
tb11 left join tb12
on tb11.part_id=tb12.department.id
union
select * from
tb11 right join tb12
on tb11.part_id=tb12.department.id;
5、子查询
一张表的查询结果当作另一张表的条件。
六、完整 select 语句关键字的语法顺序和执行顺序
语法顺序:
select distinct 字段一,字段二,字段三
from t1 <join type> t2
on <join condition>
where 条件
group by 分组条件
having 过滤
order by 排序字段
limit n;
执行顺序:
from t1
on <join condition>
<join type> t2
where 条件
group by 分组条件
having 过滤
select
distinct
order by 排序字段
limit n;
mysql重点知识
最新推荐文章于 2023-06-06 10:27:30 发布