Skr-Eric的Mysql课堂(三)——Mysql的表内的增删改查

表字段操作

  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、修改表名(rename)

    alter table 表名 rename 新表名;

  6、修改字段名(change)

    alter table 表名 change 原字段名 新字段名 数据类型;

    alter table new_t3 change name username char(15);

  7、练习

    1、在 db2 库中创建表 stutab ,字段有3个:

      name、age、phnumber

      use db2;

      create table stutab(

      name char(20),

      age tinyint,

      phnumber bigint

      );

    2、在表中第一列添加一个 id 字段

      alter table studab add id int first;

    3、把 phnumber 的数据类型改为 char(11)

      alter table studab modify phnumber char(11);

    4、在表中最后一列添加一个字段 address

      alter table studab add address varchar(30);

    5、删除表中的 age 字段

      alter table stutab drop age;

 

表记录操作

  1、删除表记录(delete)

    1、delete from 表名 where 条件;

    2、注意

      一定要加where条件,不加where条件全部删除表记录

  2、更新表记录(update)

    1、update 表名 set 字段1=值1,字段2=值2 where 条件;

    2、注意

      一定要加where条件,不加where条件全部更新表记录

6、运算符操作

  1、数值比较&&字符比较&&逻辑比较

    1、数值比较 := != > >= < <=

    2、字符比较 := !=

    3、逻辑比较 :

      1、and :两个或者多个条件同时成立

      2、or  :有1个条件满足即可

        where country="蜀国" or country="魏国";

    4、练习

      1、查找攻击力高于150的英雄的名字和攻击值

        select name,gongji from sanguo

 where gongji>150;

      2、将赵云的攻击力设置为360,防御力设置为88,名字改为赵子龙

        update sanguo set gongji=360,fangyu=88,name="赵子龙" where name="赵云";

      3、将吴国英雄中攻击值为110的英雄的攻击值改为100,防御力改为60

        update sanguo set gongji=100,fangyu=60

 where country="吴国" and gongji=110;

      4、查找蜀国和魏国的英雄信息

        select * from sanguo where

 country="蜀国" or country="魏国";

      5、找出攻击力高于200的蜀国英雄的名字、攻击值和国家

        select name,gongji,country from sanguo

 where

        gongji>200 and country="蜀国";

  2、范围内比较

    1、between 值1 and 值2

    2、in(值1,值2)

    3、not in(值1,值2)

    4、练习

      1、查找攻击值100-200之间的蜀国英雄信息

        select * from sanguo

        where

        (gongji between 100 and 200) and country="蜀国";

      2、查找蜀国和吴国以外的国家的女英雄信息

        select * from sanguo

        where country not in("蜀国","吴国") and sex="女";

      3、查找id为1、3或5的蜀国英雄 和 貂蝉的信息

        select * from sanguo

        where

        (id in(1,3,5) and country="蜀国") or name="貂蝉";

  3、匹配空 和 非空

    1、空 :is null

    2、非空 :is not null

    3、练习

      1、查找姓名为 NULL 的蜀国男英雄信息

        select * from sanguo

 where

        name is NULL and country="蜀国" and sex="男";

      2、查找姓名为 "" 的英雄信息

        select * from sanguo where name="";

      3、在所有蜀国英雄中查找攻击力大于150的并且名字不为NULL的英雄的姓名、攻击值和国家

        select name,gongji,country from sanguo

 where

        country="蜀国" and gongji>150 and name is not NULL;

      4、查找魏蜀两国英雄中攻击力小于200并且防御力小于80的英雄信息

        select * from sanguo

 where

        country in("魏国","蜀国") and gongji<200 and fangyu<80;

    4、注意

      1、NULL :空值,只能用is、is not去匹配

      2、""   :空字符串,只能用 =、!= 去匹配

  4、模糊查询(like)

    1、where 字段名 like 表达式

    2、表达式

      1、_ :匹配单个字符

      2、% :匹配0到多个字符

    3、练习

      # name中有2个字符以上的

      select name from sanguo where name like "_%_";

      # 匹配所有,但不包括NULL

      select name from sanguo where name like "%";

      # 匹配名字为3个字符

      select name from sanguo where name like "___";

      # 匹配姓赵的英雄

 

 

 

想要看更多的课程请微信关注SkrEric的编程课堂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值