当前位置:网站首页>MySQL筑基篇之增删改查
MySQL筑基篇之增删改查
2022-07-31 00:16:00 【web15286201346】
作者简介:C/C++领域新星创作者,为C++和java奋斗中
个人社区:社区
??系列专栏:MySql一点通
??推荐一款模拟面试、刷题神器??注册免费刷题
??前言
本文将承接前两篇MySQL专栏的博文,讲解数据库的增删改查操作,这里的查询确切的说应该是初级的查询,不涉及函数、分组等模块,当然更深层次的查询操作将放到后面的博文中,所以小伙伴们可以关注专栏,持续学习MySQL数据库。
文章目录
一、增加表中数据
1、无自增列时
- 指定字段添加数据
给表中的部分列添加数据:值的顺序必须跟指定列的顺序保持一致
语法:insert into 表名(列1,列2,...) values(值1,值2,...)
- 默认添加数据
向表中的所有列添加数据:值的顺序必须跟字段顺序保持一致
语法:insert into 表名 values(值1,值2,...)
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,原因:
- delete是一条一条删除,效率低,而truncate是直接在物理空间中将存放该表数据的空间截断舍弃,效率更快
- 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、简单查询
- 查询所有列
select * from 表名
- 查询部分列
select 列名1,列名2,... from 表名
- 可以通过列出所有字段名的方式查询所有列
- 弊端:书写繁琐
- 优势:可维护性更高、更灵活、执行效率更快
- 可以通过列出所有字段名的方式查询所有列
- 别名
select 列名1 as 别名1,列名2 as 别名2,... from 表名
- as关键字可省
select 列名1 别名1,列名2 别名2,... from 表名
- 表名也可以起别名
- as关键字可省
别名使用示例:
SELECT employee_id as 员工编号,salary as 工资 from employees
SELECT employee_id 员工编号,salary 工资,first_name,last_name from employees e
数学运算
select 列名+数字,列名-数字,列名*数字,列名/数字,列名%数字 from 表名
去重
select distinct 列名 from 表名
去重规则可以为多个列,只有当规则中的所有列的信息完全一致时才会去重 :select distinct 列名1,列名2,... from 表名
case when
select 列名1,列名2,
case
when 条件1 then 结果2
when 条件2 then 结果2
…
else 其他结果
end
from 表名
- 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
- 查询表详情
describe 表名
describe可以简写成desc:desc 表名
2、条件查询
语法:select 列名 from 表名 where 条件
单条件查询
查询工资>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'
多条件查询
多个条件之间须通过
and
或者or
进行拼接:
and:代表并且,多个条件同时满足,相当于java中的&&
or:代表或者,满足任意一个即可,相当于java中的||
区间查询
- 在区间内
between 最小值 and 最大值
- 不在范围内
not between 最小值 and 最大值
- 在区间内
枚举查询
- 在列举范围内
列名 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)
- 不在列举范围内
列名 not in(值1,值2,值3,...)
查询员工id不是100、105、110的员工信息select * from employees where employee_id not in(100,105,110)
- 在列举范围内
空值查询
- 为空时
列名 is null
- 不为空时
列名 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
- 为空时
模糊查询
语法: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
写在最后:查询操作前面目录前我加上了*表示其为重点内容,下一篇博客是关于查询操作的进阶,期待你的持续关注哦~
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
一款好用的接口测试工具——Postman
MySQL grant statements
H5跳转微信公众号解决方案
(五)fastai应用
How to solve the error of joiplay simulator
.NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
After writing business code for many years, I found these 11 doorways, which only experts know
如何在WordPress网站上添加导航菜单
transition transition && animation animation
asser利用蚁剑登录
随机推荐
what is jira
@requestmapping注解的作用及用法
Summary of the stock problem of state machine dynamic programming
消息队列存储消息数据的MySQL表设计
Installation considerations for pytorch
jira是什么
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
How to import game archives in joiplay emulator
Encapsulate and obtain system user information, roles and permission control
Android security optimization - APP reinforcement
二叉查找树的定义,查找,插入,删除
Regular expression password policy and regular backtracking mechanism bypass
Strict Mode for Databases
从编译的角度来学作用域!
MySQL中substring与substr区别
加密传输过程
状态机动态规划之股票问题总结
【深入浅出玩转FPGA学习14----------测试用例设计2】
pytorch的安装注意事项
ES6中 async 函数、await表达式 的基本用法