MySQL4

DQL  

      排序查询

            语法   order by 字句

                        order by 排序字段1 排序方式1,排序方式2,排序字段2...;

                        select * from student  order by math;

                        排序方式

                                 ASC:升序,默认的

                                 DESC:降序

                        select * from student order by math asc,english asc;

                         如果有多个排序条件,当前面的条件值一样是,才会判断第二条件

          聚合函数: 将一列数据作为一个整体,进行纵向的计算

                              * 聚合函数的计算,排除null值

                                         解决方案: 选择不包含空的列进行计算

                                                             ifnull函数

                         count  计算个数

                                     一般选择非空的列:主键

                                     select count(name) from student;

                                     select count(ifnull(name,0)) from student;

                         max    计算最大值

                                    select max(math) from student;

                         min     计算最小值

                                    select min(math) from student;

                         sum    求和

                                     selcet sum(math) from student;

                         avg     计算平均值

                                      selcet avg(math) from student;

       分组查询

                语法:group by 分组字段;

                 注意:  分组之后查询的字段:分组字段、聚合函数

                              where和having的区别

                                     where在分组之前进行限定,如果不满足条件,则不参与分组

                                     having在分组之后进行限定,如果不满足结果,则不会被查询出来

                                     where后不可跟聚合函数,having可以

                        select sex,avg(math) from student group by sex;

                        selcet sex,avg(math),count(id) from student group by sex;

                        selcet sex,avg(math),count(id) from student where math>70 group by sex;

                        selcet sex,avg(math),count(id) 人数 from student where math>70 group by sex having 人数>2;

      分页查询

            语法:limit 开始的索引,每页查询的条数;

                    select * from student limit 0,3;--第一页

                    select * from student limit 3,3;--第二页

                公式:开始的索引=(当前的页码-1)*每页显示的条数

                     limit 是一个“方言”,只能在mysql里面用

        约束

             概念: 对表中的数据进行限定,保证数据的正确性,有效性和完整性

              分类:

                        1  主键约束: primary key

                                   注意:

                                           含义:非空且唯一

                                            一张表只能有一个字段为主键

                                            主键就是表中记录的唯一标识

                                  -- 在创建表时,添加主键约束

                                           create table stu{

                                                  id int primary key,--给id添加主键约束

                                                  name varchar(20)

                                                    };

                                  --  删除主键

                                       alter table stu drop primary key;

                                  --创建完表后,添加主键

                                     alter table stu modify id int primary key;

                                  --自动增长

                                        概念:如果某一列是数值类型的,使用auto_increment 可以来完成值的自动增长

                                        

                                  --在创建表时,添加主键约束,并且完成值的自动增长

                                           create table stu{

                                                  id int primary key auto_increment,

                                                  name varchar(20)

                                                    };

                                  --删除自动增长

                                            alter table stu modify id int;

                                  -- 添加自动增长

                                            alter table stu modify id int  auto_increment;

                        2  非空约束: not null  某一列的值不能为null

                                    1  在创建表时添加约束

 

                                             create table stu{

                                                      id int primary key,

                                                      name varchar(20) not null

                                             };

                                             alter table stu modify name varchar(20);--删除非空约束

                                    2  创建表之后添加非空约束

                                             alter table stu modify name varchar(20) not null;

                        3  唯一约束:  unique   某一列的值不能重复

                                       注意:  唯一约束可以有null值,但是只能有一条记录为null

                                --创建表时,条件唯一约束

                                    create table stu{

                                       id int,

                                       phone_number varchar(20) unique

                                            };

                                --表创建完后,添加唯一约束

                                     alter table stu modify phone_number varchar(20) unique;

                               --删除唯一约束

                                    alter table stu drop index phone_number;

                        4  外键约束:  foreign key  让表与表产生关系,从而保证数据的正确性

                                --在创建表时,可以添加外键

                                    语法:

                                                  create table 表名(

                                                         ...

                                                         外键列,

                                  constraint 外键名称 foreign key (外键列名称) references 主表名称(主表列名称)

                                                           )

                                -- 删除外键

                                        alter table 表的名称 drop foreign key 外键名称;

                                --创建表之后,添加外键

                                         alter table 表名 add constraint 外键名称 foreign key (外键字段名称) references 主表名称(主表列名称);

                        5  级联操作

                                 --添加外键,设置级联更新,设置级联删除

                                    alter table employee add constraint emp_dept foreign key (dep_id) references department (id) on update cascade on delete cascade;

 

 

 

      

posted @ 2021-10-06 20:12  Cuora  阅读(33)  评论(0)    收藏  举报