MySQL约束条件 以及查询关键字

发布时间 2023-09-08 22:00:59作者: Lubomierz

1. 约束条件

1. unsigned  去除符号
    create table t1(id int unsigned);

2. zerofill

3. not null非空

create table t2(id int, name varchar(16));
create table t3(id int, name varchar(16) not null);

4. unique 唯一 
# 单列唯一 : 某个字段唯一
create table t4(id int, name varchar(16) unique);

# 联合唯一  : 多个字段唯一
        create table t5(id int, 
                        ip varchar(16), 
                        port int,
                        unique(ip, port)
                       );


5.   default:默认值 在可以为空的情况下 不增加则是用默认值

    create table t6(id int, name varchar(16) default 'ly');
    
6. 主键
# 单纯的从约束条件上来看,主键就相当于是not null + unique,非空切唯一
# 如何添加主键

create table t7 (id int primary key, name varchar(32));

 # 以后我们一般给哪些字段添加主键:一般会给id字段添加主键,sid、aid、uid等都可以

7. auto_increment :# 让主键每次自动增加1

如何使用
create table t8(id int primary key auto_increment, name varchar(32));

    """得出结论:以后创建表的时候,主键的固定写法:"""
    id int primary key auto_increment


InnoDB存储引擎要求每一张表必须要有一个主键,但是你会发现,之前创建的很多张表,也都创建成功了,
为什呢,原因是:InnoDB存储引擎内部隐藏的有一个主键字段,但是这个隐藏的字段我们看不到,
这个主键的目的主要就是用来帮助我们把表常见成功,仅此而已.

 2. 关键字程序的使用

"""
注意:刚开始查询表的时候,按照步骤来即可,先确定是哪张表,在确定查询这张表有没有限制条件,然后在看是否需要分组,最后在确定需要哪些字段
"""

1. 模糊查询

show variables like '%mode%'
"""
模糊查询:没有明确的筛选条件
    关键字:like
    关键符号:
        %:匹配任意个数任意字符
        _:匹配单个个数任意字符
show variables like '%mode%se';
"""

2. 查询关键字 where 筛选
# 1.查询id大于等于3小于等于6的数据
select * from emp where id >=3 and <= 6;

select * from emp where id between 3 and 6;

3 .查询薪资是20000或者18000或者17000的数据

select * from emp where salary=18000 or salary=17000 or salary =15000;

select * from emp where salary in (20000, 18000, 17000);

4.查询员工姓名中包含o字母的员工姓名和薪资
select name, salary from emp where name like '%o%';
select name, salary from emp where name like 'o%';
select name, salary from emp where name like '%o';

5 .查询员工姓名是由四个字符组成的员工姓名与其薪资
select name, salary from emp where name like '____';
select name, salary from emp where char_length(name)=4;

 6.查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not  in (20000, 18000, 17000);

 7.查询岗位描述为空的员工名与岗位名  针对null不能用等号,只能用is
select name,post from emp where post_comment = NULL;  # 查询为空!
select name,post from emp where post_comment is NULL;
select name,post from emp where post_comment is not NULL;

'''在sql中,NULL和''不一样'''
# 假如字段想设置为空,该如何设置?
最好设置为'',不要使用NULL
尤其是两者混用

# 归档数据:使用SQL语句把要归档的数据筛选出来

 

查询关键字之group by分组

分组:
    把一个整体分成若赶个个体
关键字:group by

# 分组之后,得到的每一个分组中的第一条数据
"""分组之后只能得到分组的依据"""

# 分组之后默认只能够直接过去到分组的依据 其他数据都不能直接获取
    针对5.6需要自己设置sql_mode
        set global sql_mode = 'only_full_group_by,STRICT_TRANS_TABLES,PAD_CHAR_TO_FULL_LENGTH';
 
# 有哪些聚合函数
sum max min avg count

# 1.按部门分组 :# 分组有什么用:一般会配合聚合函数使用
select * from emp group by post;
select id,name,sex from emp group by post;  # 验证

# 2. 每个部门的平均工资
select post, avg(salary) from emp group by post;

# 3. 每个部门的工资总和
select post, sum(salary) from emp group by post;


# 4. # 每个部门的人数
select post, count(*) from emp group by post;
select post, count(id) from emp group by post;
select post, count(1) from emp group by post;


"""配合分组使用的其他函数,分组之后只能获取到分组的依据,如何获取分组之外的字段呢"""

group_concat: 用在分组之后

concat:分组之前使用
concat_ws

# concat  不分组使用
select concat(name,sex) from emp;
select concat(name,'|',sex) from emp;


# concat_ws()
select post,concat_ws('|', name, age, gender) from emp group by post;

补充:在显示的时候还可以给字段取别名
select post as '部门',max(salary) as '最高工资' from emp group by post;
as也可以省略 但是不推荐省 因为寓意不明确

 

关键字之having过滤

having也是用来筛选数据的
功能上跟where是一样的
where 用在分组之前,先筛选一遍
having用在分组之后再筛选,这个不能使用where,使用having



1.统计各部门年龄在30岁以上的员工平均薪资,并且保留平均薪资大于10000的部门.

1.1 select * from emp where age >30 
1.2 select avg(salary) from emp where age >30 group by post;
1.3 select avg(salary) from emp where age >30 group by post having avg(salary) > 10000;



关键字之distinct去重

# 把重复的数据去掉
# 对有重复的展示数据进行去重操作 一定要是重复的数据
select distinct id,age from emp;
select distinct post from emp;



关键字之order by排序

select * from emp order by salary; #默认升序排
select * from emp order by salary desc; #降序排

#先按照age降序排,在年轻相同的情况下再按照薪资升序排
select * from emp order by age desc,salary;

# 1. 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序

1. 先筛选出年龄在10岁以上的员工
select * from emp age > 10

2. 按照部门进行分组,然后求平均工资
select avg(salary) as avg_salary from emp age > 10 group by post


3. 在筛选出平均工资大于1000的部门
select avg(salary) as avg_salary from emp where age > 10 group by post having avg(salary) > 100


4. 然后对平均工资进行排序
select avg(salary) as avg_salary from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary) desc;


关键字之limit分页


# 限制展示条数
select * from emp limit 3;

# 查询工资最高的人的详细信息
select * from emp order by salary desc limit 1;

select * from emp limit 0,5;  # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置

select * from emp limit 5,5; # 第一个参数代表的是起始位置,第二个参数是限制 的条数

分页:
第一页:select *from emp limit 0, 10;
第二页:select *from emp limit 10, 10;
第三页:select *from emp limit 20, 10;
第四页:select *from emp limit 30, 10;


关键字之regexp正则


支持正则
select * from emp where name regexp '^j.*(n|y)$';

 

 表查询

select 查询的关键字

from 表名