(二)数据类型,日期时间函数,运算符操作,SQL查询,约束,索引

1.数据类型
    1.数值类型
    2.字符类型
    3.枚举类型
    4.日期时间类型
        1.date : 日期 "YYYY-MM-DD"
        2.time : 时间 "HH:MM:SS"
        3.datetime : 日期时间 "YYYY-MM-DD HH:MM:SS"
        4.timestamp : 日期时间 "YYYY-MM-DD HH:MM:SS"
        5.注意
            1. datetimp : 不给值默认返回NULL
            2. timestamp : 不给默认返回当前时间

2. 日期时间函数
    1.NOW()返回服务器当前时间
    2.CURDATE() 返回当前日期
    3.CURTIME() 返回当前时间
    4.日期时间运算
        1.语法格式
            select ... from 表名
            where 字段名 运算符 (时间 - interval 时间间隔单位);
            时间间隔单位: 1 day | 2 hour | 1 minute | 1 year | 3 month
            where meeting > (now() - interval 1 day);
                           现在的时间 - 1天的时间 = 1天前的时间点
        2.练习
            1.查询1天以内的记录
                select * from t1 where meeting > (now() - interval 1 day);
            2.查询1年以前的记录
                select * from t1 where meeting > (now() - interval 1 year);
            3.查询1天以前,3天以内的记录 # where id > 3 and id < 10
    5.练习
        1.创建库 studb2
            create database studb2;
        2.在库中创建表 stuinfo,字段有3个:
            name age phone char(11)
            create table stuinfo(
                                           name char(20),
                                          age tinyint unsigned,
                                          phone char(11));
        3.查看表结构
            desc stuinfo;
        4.在表中第一列添加一个id字段
            alter table stuinfo add id int first;
        5.把phone的数据类型改为bigint
            alter table stuinfo modify phone bigint;
        6.在表中最后一列添加一个字段:注册时间 register , 数据类型为: timestamp
            alter table stuinfo add register timestamp;
        7.在表中 id name age phone 四个字段插入1条记录
            insert into stuinfo values(1,"小羊",29,13456798212,CURTIME());
        8.查询5分钟内的注册信息
            select * from stuinfo where register > (now() - interval 5 minute);        

4. 表记录管理
    1.删除表记录(delete)
        1.delete from 表名 where 条件;
        2.注意
            delete语句后没有加where条件,表中所有记录全部清除
    2.更新表记录(update)
        1.update 表名 set 字段1=值1,字段2=值2,...where条件;
        2.注意
            必须加where条件

5.运算符操作(查询 修改 删除)
    1.数值比较&字符比较&逻辑比较
        数值:  =  !=  >  >=  <  <=
        字符:  =  !=
        逻辑:  and  or
    2.范围内比较
        1.运算符
            between 值1 and 值2
            in(值1,值2,...)
            not in(值1,值2,...)
    3.匹配空 非空
        1.空 : is null
        2.非空: is not null
        3.示例
            1.查找姓名为NULL的魏国女英雄信息
                select * from sanguo
                where name is null and country = "魏国" and sex = "女";
        
            注意
             1.NULL:空值,必须用is 或者 is not 去匹配
                2.空字符串 只能用 = 或者 != 去匹配
    4.模糊比较
        1.where 字段名 like 表达式
        2.表达式
            1. _ :匹配单个字符
            2. % : 匹配0到多个字符
        3.示例
            select * from sanguo where name like "_%_";   #至少两个
            select * from sanguo where name like "%";    # 0 到多个(空值除外)
            select * from sanguo where name like "___"; # 只有三个
            select * from sanguo where name like "赵%";  # 姓赵的

6.SQL查询
    1.总结
        3.select ...聚合函数 from 表名
        1.where ...
        2.group by ...
        4.having ...
        5.order by ...
        6.limit ...;
    2.order by
        1.给查询的结果进行排序
        2.order by 字段名 排序方式
        3.排序方式
            1.升序:ASC(默认)
            2.降序:DESC
        4.练习
            1.将蜀国的英雄按照攻击值从高到低排序
                select * from sanguo
                where country = "蜀国" order by gongji desc
            2.将魏蜀两国的男英雄中名字为三个字符的英雄按防御值升序排列
               select * from sanguo 
               where country in("魏国","蜀国") and sex="男" and name like "___" 
               order by fangyu ASC;
    3.limit(永远放在SQL语句的最后)
        1.作用: 限制显示查询记录的个数
        2.用法
            1.limit n --> 显示n条记录
            2.limit m,n
                m : 表示从 m + 1 条记录开始显示
                n : 表示显示 n 条
                limit 2,4 : 显示第三条开始 共四条(3,4,5,6)
                limit 0,2 : 显示1,2两条记录
        3.示例
            1.在蜀国英雄中查找攻击值前三名且名字不为NULL的英雄姓名,攻击值和国家
                select name,gongji,country from sanguo
                where country = "蜀国" and name is not NULL
                order by gongji DESC
                limit 3;
            2.在蜀国英雄中查找防域值倒数第二名至倒数第四名的英雄
                select * from sanguo 
                where 
                country = "蜀国" 
                order by fangyu limit 1,3;
        4.分页
            每页显示5(n)条记录,显示第4(m)页
            第1页: limit 0,5  # 1 2 3 4 5
            第2页: limit 5,5  # 6 7 8 9 10
            第3页: limit 10,5  # 11 12 13 14 15
            第4页: limit 15,5  # 16 17 18 19 20
            分页公式:limit (m-1) * n , n
    4.聚合函数
        1.分类
            avg(字段名) : 平均值
            max(字段名) : 最大值
            min(字段名) : 最小值
            sum(字段名) : 求和
            count(字段名) : 统计该字段记录的个数
        2.示例
            1.攻击力最强值
                select max(gongji) as zq from sanguo;
            2.统计一下表中id name字段分别有多少条记录
                select count(id),count(name) from sanguo;
                ## 空值NULL不会被记录
                    select count(*) from sanguo;
    5. group by
        1.作用 : 给查询的结果进行分组
        2.示例
            1.计算所有国家的平均攻击力,显示国家名和平均攻击力
                select country,avg(gongji) from sanguo
                group by country;
                先分组-再聚合-去重
                 蜀国
                 蜀国  400  蜀国
                 蜀国
                 魏国  300  魏国
                 魏国
                 吴国  200  吴国
            2.查找所有国家中,英雄数量最多的前两名,显示国家名称和英雄数量
                select country,count(name) as number from sanguo
                group by country
                order by number desc 
                limit 2;
            3.注意
                1.group by之后的字段名必须要为select之后的字段名,
                如果select后的字段名和group by之后的字段不一致,
                则必须对该字段进行聚合处理(聚合函数)
    6.having
        1.作用 : 查询结果进一步筛选
        2.示例
            1.找出平均攻击力大于105的国家的前2名,显示国家名称和平均攻击力
                select country,avg(gongji) from sanguo
                group by country
                having avg(gongji) > 105
                order by avg(gongji) desc limit 2;
        3.注意
            1.having语句通常和group by语句联合使用,过滤由group by语句返回的记录集
            2.where只能操作表中实际存在的字段,having操作由聚合函数生成的显示列
    7.distinct
        1.作用: 不显示字段的重复值
        2.示例
            1.sanguo表中有哪些国家
                select distinct country from sanguo;
            2.计算魏国一共有多少个英雄
                select count(distinct name) as number from sanguo
                where country = "魏国";
        3.注意
            1.处理distinct和from之间的所有字段,所有字段值必须全部完全相同才可以去重

7. 约束
    1.作用
        保证数据完整性 一致性 有效性的规则
    2.约束分类
        1.默认约束(default)
            1.插入记录时,如果不给该字段赋值,则使用默认值
            2.字段名 数据类型 default 默认值
        2.非空约束(not null)
            1.不允许该字段值有NULL记录
            2.字段名 数据类型 not null default 值
8.索引
    1.索引优缺点
        1.优点
            加快数据的检索速度
        2.缺点
            1.当对表中数据进行增加 修改 删除时,索引需要动态维护,降低了数据的维护速度
            2.索引需要占用物理存储空间

select user_id,count(user_id) from comment
group by user_id
order by count(user_id) DESC
limit 10;

    .练习
        1.找出攻击力高于150的蜀国英雄的名字和攻击值
            select name,gongji from sanguo where gongji > 150 and country = "蜀国";
        2.将赵云的攻击力改为666,防御力改为88
            update sanguo set gongji = 666,fangyu = 88 where name = "赵云666";
        3.查找蜀国和魏国的英雄信息
            select * from sanguo where country = "蜀国" or country = "魏国";
        4.将吴国英雄中攻击值为110的英雄的攻击值设置为100,防御力设置为60
            updata sanguo set gongji = 100,fangyu = 60 where gongji = 110 and country = "吴国";

猜你喜欢

转载自blog.csdn.net/zh__quan/article/details/81091718