当前位置:网站首页>mysql约束与聚合查询
mysql约束与聚合查询
2022-07-19 21:37:00 【盛夏洋光】
文章目录
1. 约束
mysql对于数据的插入/修改进行一些限制和校验.
1.1 约束类型
NOT NULL | - 指示某列不能存储 NULL 值。 |
UNIQUE | - 保证某列的每行必须有唯一的值。 |
DEFAULT | - 规定没有给列赋值时的默认值。 |
PRIMARY KEY | - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。 |
FOREIGN KEY | - 保证一个表中的数据匹配另一个表中的值的参照完整性。 |
CHECK | - 保证列中的值符合指定的条件。对于MySQL数据库,对CHECK子句进行分析,但是忽略CHECK子句。 |
1.2 null 约束
创建表时,指定某列不为空
create table student(
id int not null,
name varchar(20),
qq_mail varchar(20)
);
1.3 unique 唯一约束
指定某列是唯一 不重复的
1.4 default 默认值约束
指定插入时, name默认值为 unkown :
create table student(
id int not null,
name varchar(20) default 'unkown',
qq_mail varchar(20)
);
1.5 primary key 主键约束
指定 id 列为主键
create table student(
id int not null primary key,
name varchar(20) default ‘unkown’,
qq_mail varchar(20)
);
对于整数类型的主键,常配搭自增长auto_increment来使用。插入数据对应字段不给值时,使用最大
值+1。
主键是 NOT NULL 和 UNIQUE 的结合,可以不用 NOT NULL
1.6 foreign key 外键约束
外键用于关联其他表的主键或唯一键
create table classes (
id nt primary key auto_increment,
name VARCHAR(20),
);
create table student (
id int primary key auto_increment,
name varchar(20) default 'unkown',
qq_mail varchar(20),
classes_id int,
foreign key (classes_id) references classes(id)
);
此处classes表负责约束 称为 父表
student表被约束 称为 子表
子表中的class_id 必须是父表中存在的数据
在父表为空的情况下 无法对子表进行插入
父表对子表的限制时不能随意插入/修改
子表对父表的限制时不能随意修改删除
2.聚合查询
2.1 聚合函数
常见的计算可以使用 mysql的聚合函数来实现 :
函数 | 说明 |
---|---|
count | 返回查询到的数据的 数量 |
sum | 返回查询到的数据的 总和,不是数字没有意义 |
avg | 返回查询到的数据的 平均值,不是数字没有意义 |
max | 返回查询到的数据的 最大值,不是数字没有意义 |
min | 返回查询到的数据的 最小值,不是数字没有意义 |
2.2.1 count
返回列所拥有的行数.(null不计入行数)
2.2.2 sum
其他用法类似.
2.3 group by
对指定列进行分组查询
select role,max(salary),min(salary),avg(salary) from emp group by role;
2.4 having
因为 group by 之后 无法使用where条件 固有having条件从句.
按角色查询平均工资<1500的信息
边栏推荐
- Dbeaver connect Oracle user display error and user does not exist?
- 4R analysis redis processing client requests
- 华为od 最大消费金额 js
- 散户炒股选哪个证券公司手续费低,手机上开户安全吗
- Deep decryption of go language context
- 分糖果 华为od js
- 主流定时任务解决方案全横评
- Debezium系列之:深入解读Debezium重要的jmx指标
- The most complete idea debug debugging skills in history (super detailed cases)
- Hill sort summary
猜你喜欢
[C# 调试]-使用 VS 2012 IDE 环境调试 C# 代码
RoleApp聚焦Web3,用“数据可持有”的钥匙开启Web3的大门
AI在测试中的应用
Deep decryption of go language context
史上最全的 IDEA Debug 调试技巧(超详细案例)
数据仓库开发 SQL 使用技巧总结
mmdeploy源码安装 (转换faster rcnn r50/yolox为tensorrt,并进行推理)
Module 7 of the construction camp
【Go语言】(一)环境搭建与了解VScode工具
The study of classic diagram of K-line diagram is conducive to correct
随机推荐
SQL question brushing: find out the current salary of all employees
[swoole Series 2.5] Asynchronous Tasks
Explore the secret behind the improvement of music and painting quality. A thousand yuan gift is waiting for you
[swoole series 2.5] asynchronous tasks
微生物与我们的生活息息相关
RoleApp聚焦Web3,用“数据可持有”的钥匙开启Web3的大门
Knowledge points of common interview questions: distributed lock
鸿蒙HarmonyOS 3官宣:7月27日正式发布;苹果2023年放缓部分团队招聘和支出;俄罗斯对谷歌重罚26亿元|极客头条
页面请求拦截
uniapp使用Hbuilder X方式, 安装uivew插件步骤
Interpretation of openmmlab series framework (based on pytorch)
【Go语言】(一)环境搭建与了解VScode工具
乐扣乐扣澄清欠税事件:不存在欠税,将一如既往合规经营,植根中国
Unity 3D人物的粒子消散
The first small sample detection benchmark and weak feature enhancement network under X-ray, and the new research of Beihang and iFLYTEK was selected into ACM mm 2022
See Zhihu salt for free
apply、call、bind的区别
查缺补漏C语言:字符串处理函数
spirng Security (八)多个过滤器链共存
Full horizontal evaluation of mainstream timed task solutions