mysql 基本增删改查SQL语句

CRUD

Insert语句

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

update语句

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

delete语句

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

select语句

基本语法

select [distinct] *|{column1,column2,....} from table_name;
  • 注意事项
    • 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;

where中经常使用的运算符

  • 比较运算符

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

    and -- 多个条件同时成立
    or -- 多个条件任一成立
    not -- 不成立,例如:where not (salary>100);
    
select * from student where (chinese+english+math) > 200 and math< chinese and `name` like '韩%';

-- 这个 韩% 表示是以韩开头的字符串,无论韩后面有多少个字符都可以。
-- 韩_ 表示是以韩开头的字符串,但是 韩后面只能有一个字符。
  • between …and…(闭区间)
select * from student where english between 80 and 90;

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

select column1,column2,... from tablename order by column asc|desc;
  • 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;
-- 对总分按照降序进行排列
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GaoJa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值