• mysql 基本增删改查SQL语句


    CRUD

    Insert语句

    insert into `good` (id,good_name,price) values(11,'苹果手机',5000);
    --           表名    列名 列名     列名          值
    
    • 1
    • 2
    • insert语句注意事项
      • 插入的数据应与字段的数据类型相同
      • 数据的长度应列的规定范围内,例如:不能将一个长度80的字符串加入到长度为40的列中
      • 在values中列出的数据位置必须与被加入的列的排列位置相对应
      • 字符和日期类型数据应包含在单引号中
      • 列可以插入空值【前提是该字段允许为空】
      • insert into tab_name (列名…) values(),(),()…;
      • 如果是给表中的所有字段添加数据的时候,可以不写前面的字段名字
      • 默认值的使用,当不给某个字段值时,如果有默认值就会添加,否则报错。

    update语句

    update good set price = price + 1000 where good_name = '苹果手机';
    
    • 1
    • 使用细节
      • update语法可以用新值更新原有表行中的各列。
      • set子句指示要修改哪些列和要赋给予哪些值。
      • where子句指定应更新哪些行,如没有where子句,则更新所有的行(记录)
      • 如果需要修改多个字段,可以通过set 字段1 = 值1,字段2 = 值2…

    delete语句

    delete from good where good_name = '苹果手机';
    
    • 1
    • 使用细节
      • 如果不适用where子句,将删除表中的所有数据
      • Delete语句不能删除某一列的值(可使用update设为null或者‘’)
      • 使用delete语句仅删除记录,不删除表本身。如果要删除表,使用drop table 语句。

    select语句

    基本语法

    select [distinct] *|{column1,column2,....} from table_name;
    
    • 1
    • 注意事项
      • select指定查询哪些列的数据
      • column指定列名
      • *号代表查询所有列
      • from指定查询哪张表
      • distinct可选,指查询结果的时候,是否去掉重复的数据
    -- 统计每个学生的总分
    select `name`,(chinese+english+math) from student;
    -- 给每个学生的总成绩加10分
    select `name`,(chinese+english+math+10) from student; 
    -- 使用别名表示学生的总成绩
    select `name` as '名字',(chinese+english+math) as total_score from student;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    where中经常使用的运算符

    • 比较运算符

      > < <= >= = <> !=  -- 大于,小于,大于(小于)等于,不等于
      
      • 1
      between ... and ... -- 显示在某一区域的值
      
      • 1
      in(set) -- 显示在in列表中的值,例如,in(100,200)  这个set是个集合,不是表示区间
      
      • 1
      like '张pattern'
      not like ''  -- 模糊查询
      
      • 1
      • 2
      is null -- 判断是否为空
      
      • 1
    • 逻辑运算符

      and -- 多个条件同时成立
      or -- 多个条件任一成立
      not -- 不成立,例如:where not (salary>100);
      
      • 1
      • 2
      • 3
    select * from student where (chinese+english+math) > 200 and math< chinese and `name` like '韩%';
    
    -- 这个 韩% 表示是以韩开头的字符串,无论韩后面有多少个字符都可以。
    -- 韩_ 表示是以韩开头的字符串,但是 韩后面只能有一个字符。
    
    • 1
    • 2
    • 3
    • 4
    • between …and…(闭区间)
    select * from student where english between 80 and 90;
    
    • 1

    使用order by 子句排序查询结果

    select column1,column2,... from tablename order by column asc|desc;
    
    • 1
    • order by 指定排序的列,排序的列既可以是表中的列名,也可以是select语句后指定的列名
    • asc升序【默认】,desc降序
    • order by 子句应位于select语句的结尾
    select * from student order by math;-- 默认是升序
    select * from student order by math desc -- 按照math成绩降序排列
    select `name`,(chinese+math+english) as total_score from student order by total_score;
    -- 对总分按照升序进行排列
    select `name`,(chinese+math+english) as total_score from student order by total_score desc;
    -- 对总分按照降序进行排列
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    斐波那契数列|||川(马蹄集)
    基于JAVA人事管理系统计算机毕业设计源码+数据库+lw文档+系统+部署
    数组中的第K大元素[堆排 + 建堆的实际时间复杂度]
    2023.9.26 IO 文件操作详解
    TCP协议面试灵魂10问,建议收藏~
    根据NVeloDocx Word模板引擎生成Word(六-结束)
    ASM字节码插桩
    链表的注意事项
    Arm发布 Neoverse V2 和 E2:下一代 Arm 服务器 CPU 内核
    Java调用操作系统命令的输出乱码问题解决
  • 原文地址:https://blog.csdn.net/m0_60996938/article/details/125631819