Mysql-day02

1.表字段的操作
1.语法:
alter table 表名 执行动作;
2.添加字段(add)
alter table 表名 add 字段名 数据类型;
alter table 表名 add 字段名 数据类型 first ;
alter table 表名 add 字段名 数据类型 after 字段名;
3.删除字段(drop)
alter table 表名 drop 字段名;
4.修改字段的数据类型(modify)
alter table 表名 modify 字段名 新数据类型;
5.字符类型宽度和数值类型的宽度区别
1.数值类型的宽度为显示宽度,只用于select 查询时显示,和存储无关
,可以用zerofill 查看效果
2.字符类型宽度超过之后无法存储

6.表的重命名:(rename)
    alter table 原表名 rename 新表名;

7.表字段重命名:(change)
    alter table 表名  change 原名 新名 数据类型;

练习:
    1.创建库studb2
    2.在studb2中创建表t1,字段有3个,name,age,phnumber char(11)
    3.查看表1的表结构
    4.在t1表中第一个字段添加id字段,要求显示宽度为3,位数不够用0填充
    5把phnumber数据类型改为bigint

表记录的管理:
1.删除表记录(delete)
1. delete from 表名 where 条件;
注意:不加where条件全部删除表记录
delete from hero where id=6;
delete from hero where name=“小乔” and country=“吴国”;

2.更改表记录(update)
    1.update 表名 set 字段1=值1,字段2=值2 where 条件;
        注意:不加where条件全部更改表记录
        update hero set name="司马懿",country="魏国" where id=5;


总结:
           表字段(alter table 表名)             表记录
    增加    add                                insert
    删除    drop                               delete
    修改    modify                             update
    查询    desc                               select


    练习:
    1. 查找所有蜀国英雄信息
        select * from hero where country="蜀国";
    
    2. 查找所有女英雄的姓名,性别,国家
        select name ,sex , country from hero where sex="女";

    3. 把id为2的记录改为典韦,性别男,国家改为魏国
         update hero set name="典韦",sex="男",country="魏国" where id =2;

    4. 删除所有蜀国的英雄
        delete from hero where country="蜀国";

    5. 把貂蝉的国籍改为魏国
        update hero set country="魏国" where id=4;

    6. 删除所有表记录
        delete from hero ;

6.运算符操作
1.数值比较&&字符比较&&逻辑比较
1.数值比较: = != > >= < <=
2.字符比较: = !=
3逻辑比较: and ,or ,between 值1 and 值2 (值1,值2必须为字符类型)
where id between 100 and 200;
4.示例:
1 找出攻击力高于200的蜀国英雄名字和攻击力
select name,gongji from sanguo where gongji>200 and country=“蜀国”;

    2.  将吴国英雄中攻击值为110的英雄攻击值改为100,防御改为60
        mysql> update sanguo set gongji=100 , fangyu=60 where country="吴国"and gongji=110;

    3.显示蜀国和吴国的英雄信息
        select * from sanguo where country="蜀国" or country="吴国";

2.范围内比较
    1.where 字段名 in (值1,值2,...)
    2.where 字段名 not in (值1,值2,...)

    3示例:
        1查找攻击值在100~200之间的蜀国英雄信息
            select * from sanguo where country="蜀国"and gongji between 100 and 200;
    2.找到蜀国和吴国以外的国家的女英雄信息
            select * from sanguo where sex="女" and country not in ("蜀国","吴国");
    3.找到id 为1,3,5的蜀国英雄和貂蝉的信息
            select * from sanguo where id in(1,3,5) and country="蜀国" or name="貂蝉";

3.匹配空,非空
    1 where name is Null
    2.where name is not Null
        查找姓名为空值的蜀国女英雄信息
        select * from sanguo where name is Null and country="蜀国" and sex ="男";

4注意:
    1.Null :空值,只能用is ,is not 去匹配
    2.""   :空字符串,用=,!=去匹配

5.模糊查询(like)
    where 字段名 like 表达式
        表达式:
            %: 匹配0~多个字符
            _:匹配一个字符
        示例:
            select name from sanguo where name like "_%_";   #匹配2个字符及以上
            select name from sanguo where name like "%";     #匹配所有,不包含Null
            select name from sanguo where name like "___";   #匹配三个字符
            select * from sanguo where name like "赵%";       # 匹配姓赵的

7.SQL查询
1.总结
3.select … 聚合函数 from 表名
1.where…
2.group by…
4.having …
5.order by…
6.limit…;

2. order by: 给查询的结果进行排序
    1  ...order by   字段名 ASC(升序默认)/DESC(降序) 
    2.示例
        1.将英雄按防御值降序排序
            select * from sanguo order by fangyu DESC;
        
        2.将蜀国英雄按攻击值降序排序
            select * from sanguo where country="蜀国" order by gongji DESC;
        
        3.将魏蜀两国英雄中名字为3个字的英雄按防御值升序排序
            select * from sanguo where country in ("蜀国","魏国") and name like "___" 
            order by fangyu ASC;

3.limit(永远放在SQL语句的最后写)
    1.用法:
        1.limit n: 显示n条记录
        2.limit m,n : 从第m+1条记录开始,显示n条
            如: limit 2,3 : 显示3,4,5三条记录
        3分页
            每页显示m条记录,显示第n页的记录
            limit (n-1)*m,m
    示例:
        1.在蜀国英雄中,查找防御值倒数第二名至倒数第四名的英雄记录
             select * from sanguo where country="蜀国" order by fangyu limit 1,3 ;

        2.在蜀国英雄中,查找攻击力值前三名且名字不为null的英雄的姓名,攻击值和国家
         select name,gongji,country from sanguo where country="蜀国" and name is not Null order by
         gongji DESC  limit 3;

4.聚合函数
    1.分类
        avg(字段): 平均值
        max(字段): 最大值
        min(字段): 最小值
        sum(字段): 求  和
        count(字段):统计该字段记录的条数
    2.示例
        1攻击力最强的值是多少
            select max(gongji) from sanguo;
        
        2.统计id ,name 两个字段分别有几条记录
            select count(id),count(name) from sanguo;
    练习:
        统计蜀国英雄中攻击值大于200的英雄数量
        select count(*) from sanguo where country="蜀国" and gongji>200;

5. group by :给查询的结果进行分组,先分组,再聚合
    1.示例
        1.计算每个国家的平均攻击力
        select country,avg(gongji) from sanguo group by country;
            分组       聚合函数          去重
    练习:
        查找所有国家的男英雄中,英雄数量最多的前两名的国家名称和英雄数量
            select country,count(*) from sanguo where sex="男" group by country order by count(*) DESC limit 2;

    注意:
        1.group by 后字段必须要为select后的字段
        2.如果查询字段和group by 后字段不一致,则必须对该字段进行聚合处理(聚合函数)
            select avg(gongji) from sanguo group by country;

6. having:对分组聚合后的结果进行进一步筛选
    示例:
        找出平均攻击力大于105的国家的前2名,显示国家名称和平均攻击力

        select country, avg(gongji) from sanguo group by country having 
        avg(gongji)>105 order by avg(gongji) DESC limit 2;

    1. having 语句通常与group by联合使用
    2. having语句存在弥补了where 关键字不能与聚合函数联合使用的不足,where只能操作表中实际存在的字段
        ,having操作的是聚合函数生成的显示列

作业:把练习再做一遍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值