当前位置:网站首页>MySQL constraint and aggregate query
MySQL constraint and aggregate query
2022-07-20 22:39:00 【Midsummer Yangguang】
List of articles
1. constraint
mysql For data insertion / Modify some restrictions and verifications .
1.1 Constraint type
NOT NULL | - Indicates that a column cannot store NULL value . |
UNIQUE | - Ensure that each row of a column must have a unique value . |
DEFAULT | - Specifies the default value when the column is not assigned a value . |
PRIMARY KEY | - NOT NULL and UNIQUE The combination of . Make sure that a column ( Or a combination of two or more columns ) There's a unique logo , Helps to find a specific record in a table more easily and quickly . |
FOREIGN KEY | - Ensure referential integrity of data in one table matching values in another table . |
CHECK | - Ensure that the values in the column meet the specified conditions . about MySQL database , Yes CHECK Clause to analyze , But ignore CHECK Clause . |
1.2 null constraint
Create table time , Specifies that a column is not empty
create table student(
id int not null,
name varchar(20),
qq_mail varchar(20)
);
1.3 unique Unique constraint
Specify that a column is unique Not repeated
1.4 default Default constraint
Specify when inserting , name The default value is unkown :
create table student(
id int not null,
name varchar(20) default 'unkown',
qq_mail varchar(20)
);
1.5 primary key Primary key constraint
Appoint id List as primary key
create table student(
id int not null primary key,
name varchar(20) default ‘unkown’,
qq_mail varchar(20)
);
For integer type primary keys , Often accompanied by self growth auto_increment To use . When the field corresponding to the inserted data does not give a value , Use the largest
value +1.
The primary key is NOT NULL and UNIQUE The combination of , Don't have to NOT NULL
1.6 foreign key Foreign key constraints
Foreign keys are used to associate with other tables Primary key or The only 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)
);
here classes Tables are responsible for constraining be called Parent table
student Table constrained be called Sub table
In the sub table class_id It must be the data existing in the parent table
If the parent table is empty Cannot insert sub table
When the parent table restricts the child table, it cannot be inserted at will / modify
The restriction of child table on parent table cannot be modified or deleted at will
2. Aggregate query
2.1 Aggregate functions
Common calculations can use mysql To implement :
function | explain |
---|---|
count | Returns the of the queried data Number |
sum | Returns the of the queried data The sum of the , It's not that numbers don't make sense |
avg | Returns the of the queried data Average , It's not that numbers don't make sense |
max | Returns the of the queried data Maximum , It's not that numbers don't make sense |
min | Returns the of the queried data minimum value , It's not that numbers don't make sense |
2.2.1 count
Returns the number of rows owned by the column .(null Not included in the number of rows )
2.2.2 sum
Other uses are similar .
2.3 group by
Group and query the specified columns
select role,max(salary),min(salary),avg(salary) from emp group by role;
2.4 having
because group by after Can't use where Conditions inherent having Conditional clause .
Query average salary by role <1500 Information about
边栏推荐
- MES管理系统解决方案的参照标准是什么
- 华为无线设备配置不同业务VLAN的AP间非快速漫游
- Easydss uses MySQL database and cannot be started. How to solve it?
- Particle dissipation of unity 3D characters
- 最受IT公司欢迎的 30 款开源软件
- 【论文阅读|浅读】RolX: Structural Role Extraction & Mining in Large Graphs
- 1054 The Dominant Color
- Build service records based on consumer and fastapi
- EMQ映云科技成功入选《中国企业家》2022年度「新锐100」榜单
- Oracle使用fy_recover_data恢复truncate删除的数据
猜你喜欢
随机推荐
d静态导入对象
13. 谈谈 Redis 的过期策略
免费看知乎盐选
【Go语言】(一)环境搭建与了解VScode工具
整流桥厂家ASEMI有哪些型号,整流桥厂家的电镀工艺怎么样?
创意:高仿矿质颜料AI油画作品呈现
明晚19点直播 | 深度剖析:数据湖中的对象存储
leetcode:632. 最小区间
Redis 官方可视化工具,高颜值,功能真心强大!
Google Earth engine -- how to get the information you want in the string through the return value of the function
CPDA | first learn data analysis tools or data analysis thinking?
1055 The World‘s Richest
Build service records based on consumer and fastapi
苹果等大厂Hold不住了!裁员和放缓招聘,「双管齐下」出方案
The training accuracy is comparable to alphafold2, and the speed is doubled. The helixfold training and reasoning code of the propeller is fully open source
RoleApp聚焦Web3,用“数据可持有”的钥匙开启Web3的大门
2022年下半年(软考高级)信息系统项目管理师认证招生简章
Compréhension approfondie du protocole TCP
[swoole series 2.5] asynchronous tasks
分糖果 华为od js