> 文档中心 > MySQL筑基篇之增删改查

MySQL筑基篇之增删改查


✅作者简介:C/C++领域新星创作者,为C++和java奋斗中
✨个人社区:微凉秋意社区
🔥系列专栏:MySql一点通
📃推荐一款模拟面试、刷题神器👉注册免费刷题

🔥前言

本文将承接前两篇MySQL专栏的博文,讲解数据库的增删改查操作,这里的查询确切的说应该是初级的查询,不涉及函数、分组等模块,当然更深层次的查询操作将放到后面的博文中,所以小伙伴们可以关注专栏,持续学习MySQL数据库。

文章目录

  • 一、增加表中数据
    • 1、无自增列时
    • 2、有自增列时
  • 二、删除表中数据
    • 1、使用delete
    • 2、使用truncate
  • 三、修改表中数据
  • 四、*查询操作
    • 1、简单查询
    • 2、条件查询
    • 3、排序

一、增加表中数据

1、无自增列时

  1. 指定字段添加数据
    给表中的部分列添加数据:值的顺序必须跟指定列的顺序保持一致
    语法:insert into 表名(列1,列2,...) values(值1,值2,...)
  2. 默认添加数据
    向表中的所有列添加数据:值的顺序必须跟字段顺序保持一致
    语法:insert into 表名 values(值1,值2,...)

2、有自增列时

  1. 对于指定字段的添加
    不用指定自增列,语法同无自增列一致
  2. 对于默认情况的添加
    必须手动为自增列赋值或者填上null
    例如:insert into t_person VALUES(null,'wangwu',32,'男',150.9')

自增列设置方法:

create table if not exists t_person(p_id int primary key auto_increment,-- 主键 自增...)

关键字:auto_increment

二、删除表中数据

1、使用delete

语法:delete from 表名 [where条件]

  • 删除所有数据,例如:delete from t_person
  • 删除指定数据,例如:delete from t_person where p_id=8

2、使用truncate

语法:truncate table 表名
通过表截断(truncate)的方式删除数据要优于使用delete,原因:

  1. delete是一条一条删除,效率低,而truncate是直接在物理空间中将存放该表数据的空间截断舍弃,效率更快
  2. delete主键会继续删除之前的自增,而truncate会重新开始自增

三、修改表中数据

语法:update 表名 set 列名1=新值,列名2=新值,... [where 条件]

  • 操作整张表,例如:update t_person set age=18
  • 操作部分数据,例如:update t_person set age=28 where p_id=1

第一个例子的含义是把t_person表中所有的age属性改为18,第二个含义是只把p_id为1对应的age改为28

四、*查询操作

查询是数据库基础的重点,拿小本本记上

1、简单查询

  1. 查询所有列
    select * from 表名
  2. 查询部分列
    select 列名1,列名2,... from 表名
    • 可以通过列出所有字段名的方式查询所有列
      • 弊端:书写繁琐
      • 优势:可维护性更高、更灵活、执行效率更快
  3. 别名
    select 列名1 as 别名1,列名2 as 别名2,... from 表名
    • as关键字可省
      select 列名1 别名1,列名2 别名2,... from 表名
    • 表名也可以起别名

别名使用示例:

SELECT employee_id as 员工编号,salary as 工资 from employees
SELECT employee_id 员工编号,salary 工资,first_name,last_name from employees e

  1. 数学运算
    select 列名+数字,列名-数字,列名*数字,列名/数字,列名%数字 from 表名
  2. 去重
    select distinct 列名 from 表名
    去重规则可以为多个列,只有当规则中的所有列的信息完全一致时才会去重 :
    select distinct 列名1,列名2,... from 表名
  3. case when
select 列名1,列名2,casewhen 条件1 then 结果2when 条件2 then 结果2...else 其他结果endfrom 表名
  • when从上至下判断
  • 每行数据只会执行一个when
  • 类似java中的多重if分支:case开启分支,end结束分支

使用示例:

查询员工id及其工资,并对工资进行评级:工资>10000 高薪,工资>8000 中级,工资>5000 低级,其他 底层

 select employee_id,salary,    case    when salary>10000 then '高薪'    when salary>8000 then '中级'    when salary>5000 then '低级'    else '底层'    end as '薪资等级'    from employees
  1. 查询表详情
    describe 表名
    describe可以简写成desc:
    desc 表名

2、条件查询

语法:
select 列名 from 表名 where 条件

  1. 单条件查询
    查询工资>10000的员工信息:
    SELECT * from employees where salary>10000

    • 比较的类型为字符串时,对比数据需要加上单引号
    • mysql默认不区分大小写,如有需求,则在对应位置添加binary关键字
      查询first_name为Steven的所有员工信息:
      select * from employees where first_name='STEVEN'
      区分大小写:
      select * from employees where binary first_name='STEVEN'
  2. 多条件查询

    多个条件之间须通过and或者or进行拼接:
    and:代表并且,多个条件同时满足,相当于java中的&&
    or:代表或者,满足任意一个即可,相当于java中的||

  3. 区间查询

    • 在区间内
      between 最小值 and 最大值
    1. 不在范围内
      not between 最小值 and 最大值
  4. 枚举查询

    1. 在列举范围内
      列名 in(值1,值2,值3,...)
      查询员工id为100、105、110的员工信息
      select * from employees where employee_id=100 or employee_id=105 or employee_id=110
      等价于:
      select * from employees where employee_id in(100,105,110)
    2. 不在列举范围内
      列名 not in(值1,值2,值3,...)
      查询员工id不是100、105、110的员工信息
      select * from employees where employee_id not in(100,105,110)
  5. 空值查询

    1. 为空时
      列名 is null
    2. 不为空时
      列名 is not null
      查询commission_pct为null的员工信息
      select * from employees where commission_pct is null
      查询commission_pct不为null的员工信息
      select * from employees where commission_pct is not null
  6. 模糊查询
    语法:
    where 列名 like '值'
    %:代表不固定长度,可以为0-n个字符
    _:代表一个长度

示例:

查询first_name中包含s的员工信息select * from employees where first_name like '%s%'查询firstname中以s开头的员工信息select * from employees where first_name like 's%'查询firstname中以s结尾的员工信息select * from employees where first_name like '%s'查询firstname中第二个字母为s的员工信息select * from employees where first_name like '_s%'

3、排序

对查询结果进行指定规则的排序显示

1、单列排序
select 列名 from 表名 order by 列名 asc(升序)|desc(降序)
示例:
根据工资从高到低显示员工信息
select * from employees order by salary desc

根据工资从低到高
select * from employees order by salary asc
select * from employees order by salary

tips: 默认为升序排列

2、多列排序
order by 列名1 asc|desc , 列名2 asc|desc,...
示例:
根据工资从低到高显示员工信息,如果工资相同,根据员工id从高到低显示
select * from employees order by salary asc,employee_id desc

3、where+order by

 select 列名 from 表名 where 筛选条件 order by 排序条件

示例:
查询工资>10000的员工信息,从高到低显示
select * from employees where salary>10000 order by salary desc

写在最后:
查询操作前面目录前我加上了*表示其为重点内容,下一篇博客是关于查询操作的进阶,期待你的持续关注哦~