当前位置:网站首页>mysql 基本增删改查SQL语句
mysql 基本增删改查SQL语句
2022-07-06 09:32:00 【萌新菜狗】
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;
-- 对总分按照降序进行排列
边栏推荐
- Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
- JVM之垃圾回收器下篇
- DS18B20數字溫度計系統設計
- Instructions for Redux
- 字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
- Shell_ 04_ Shell script
- 唯有学C不负众望 TOP2 p1变量
- "One year after graduation, I won ACL best paper"
- Ruoyi-Cloud 踩坑的BUG
- Flink 解析(七):时间窗口
猜你喜欢

Alibaba cloud server builds SVN version Library

Instructions for Redux

Shell_ 05_ operator

Idea breakpoint debugging skills, multiple dynamic diagram package teaching package meeting.

Some feelings of brushing leetcode 300+ questions

Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance

Log4j2 major vulnerabilities and Solutions

Activit fragmented deadly pit

MySQL日期函数

Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
随机推荐
唯有学C不负众望 TOP3 Demo练习
Shell_ 06_ Judgment and circulation
Resume of a microservice architecture teacher with 10 years of work experience
Activiti目录(五)驳回、重新发起、取消流程
JVM运行时数据区之程序计数器
JVM之垃圾回收器上篇
MySQL digital function
ByteDance technical Interviewer: what kind of candidate do I want to pick most
The 116 students spent three days reproducing the ByteDance internal real technology project
TCP's three handshakes and four waves
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
MySQL optimization notes
8086 分段技术
手把手带你做强化学习实验--敲级详细
Prototype chain inheritance
汇编语言基础知识
Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
Shell_ 07_ Functions and regular expressions
Eureka single machine construction
逻辑运算指令