MySQL数据库(表的CRUD基础操作(最常用))

ced485cbb11e458d81a746890b32cf3f.gif

 作者:渴望力量的土狗

博客主页:渴望力量的土狗的博客主页

专栏:MySQL数据库

工欲善其事必先利其器,给大家介绍一款超牛的斩获大厂offer利器——牛客网

点击免费注册和我一起刷题吧

目录

理解CRUD:

新增(Create) :

单行数据+全列插入:

多行数据+指定列插入: 

查询(Retrieve):

全列查询: 

指定列查询:

查询字段为表达式: 

表达式不包含字段: 

表达式包含1个字段:

表达式包含多个字段:

去重操作: 

排序:

条件查询:  (where)(很重要)

条件查询:

范围查询: 

​模糊查询: 

NULL 的查询:

分页查询:LIMIT: 

修改(Update): 

删除(Delete): 


理解CRUD:

CRUD 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写

新增(Create) :

单行数据+全列插入:

注意:只可以就行单行插入,并且必须是全列的,否则就失败了。

单行数据,全列插入语法形式:

insert into 表(字段1, ..., 字段N) values (value1, ..., value N);

创建一个学生表:

create table student (id int, name varchar(20),
                      chinese dicimal(3,1),
                      math dicimal(3,1),
                      english dicimal(3,1));

进行单行插入:

insert into student values(1,'张三',88,99,67);

多行数据+指定列插入: 

这个就是说我们可以一次插入多行,每一行的列不做要求,可以随便插入。

语法形式:

insert into 表(字段1, ..., 字段N) values
(value1, ...),
(value2, ...),
(value3, ...);

例如: 

 insert into student (id, name, chinese) values (2,'李四',86),(3,'王五',67);

 或者:

 insert into student (id,name,chinese,math,english) values (4,'张飞',82,64,97);

 这些都是可以的,但是需要注意的是,values前面括号里面的数据必须和后面的数据相匹配,否则就会出现错误。

查询(Retrieve):

全列查询: 

语法形式:

select * from 表

 上述例子中就用到了全列查询:

 select * from student;

全列查询是把整个表中的所有数据都遍历了一边,这种用法在数据量比较大的时候不建议使用,MySQL数据库是基于客户端-服务器类型的,当我们发送指令的时候,服务器来接收指令,并给予服务。我们所需要的数据是存储在硬盘中的,所以服务器需要在硬盘中读取我们需要的数据,并加以解析,当全部都查询的时候,会增加数据传输时间和网络开销 。

所以一般我们查询的时候要指定查询的列等内容,尽量避免全列查询(当然,我们这里数据量非常非常小,就影响不是很大)。

指定列查询:

语法形式:

select 字段1,字段2... from 表
 select id,name,chinese from student;

查询字段为表达式: 

语法格式:

select 字段1+100,字段2+字段3 from 表

表达式不包含字段: 

select name,chinese,10 from student;

 

表达式包含1个字段:

select name,chinese,chinese+10 from student;

 

表达式包含多个字段:

select name,chinese+math+english from student;

方便起见,可以加个别名:

select name,chinese+math+english as total from student;

 

去重操作: 

顾名思义,就是去除字段中重复的数据

语法形式:

select distinct 字段 from 表

比如:我们看一下这个学生表里面的数据:

select *from student;

可以看到李四和孙六的语文成绩都是86,我们这里进行一下去重:

select distinct chinese from student;

可以看到只有一个86了,这就达到了去重的目的。需要我们注意的是,所有的查询操作仅仅是查询出一个临时的表供你查看,并没有改变存在服务器硬盘中的数据。

排序:

语法形式:

select 字段....from 表 order by 排序字段

 注意:NULL 数据排序,视为比任何值都小,升序出现在最上面,降序出现在最下面

-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC

 比如说我们按照语文从小到大排序:

select name,chinese from student order by chinese;

或者我们想降序:

 select name,chinese from student order by chinese desc;

 

我们也可以按照表达式别名等等进行排序,无非就是在查询的基础上加以改动即可。

  select name,chinese+math+english as total from student order by total;

也可以对多个字段进行一个排序:

如语文升序,数学降序

select *from student order by chinese,math desc;

 

排序是有优先级的,排序优先级随书写顺序。

条件查询:  (where)(很重要)

一些常用的比较运算符:

运算符 说明
>, >=, <, <= 大于,大于等于,小于,小于等于
= 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
<=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!=, <> 不等于
BETWEEN a0 AND
a1
范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1)
IN (option, ...) 如果是 option 中的任意一个,返回 TRUE(1)
IS NULL 是 NULL
IS NOT NULL 不是 NULL
LIKE 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字

常用的逻辑运算符:

运算符 说明
AND 多个条件必须都为 TRUE(1),结果才是 TRUE(1)
OR 任意一个条件为 TRUE(1), 结果为 TRUE(1)
NOT 条件为 TRUE(1),结果为 FALSE(0)

注:
1. WHERE条件可以使用表达式,但不能使用别名。
2. AND的优先级高于OR,在同时使用时,需要使用小括号()包裹优先执行的部分

条件查询:

比如查询英语不及格的同学及英语成绩 ( < 60 )

 select name,english from student where english<60;

又或者语文成绩比英语好的:

select name,chinese,english from student where chinese>english;

 又或者总分在200以下的:

select name,chinese+math+english from student where chinese+math+english>200;

或者查询语文和英语都在80以上的:

select name,chinese,english from student where chinese>80 and english>80;

或者查询语文或英语在80以上的:

select name,chinese,english from student where chinese>80 or english>80;

范围查询: 

 或者找语文成绩在80到90之间的:

select name,chinese from student where chinese between 80 and 90;

and也可以实现:

 select name,chinese from student where chinese>=80 and chinese <=90;

 然后我们可以查询语文成绩是88,82的学生:

select name,chinese from student where chinese in (88,82);

 用or也可以实现:

select name,chinese from student where chinese=88 or chinese=82;

模糊查询: 

模糊查询用like,当加%的时候表示%位置可以是任意个字符,用_代表只能是一个字符,并且要严格遵守位置要求:

select name from student where name like '张%';

如果只是想查询一个张?这样的名字:

select name from student where name like '张_';

NULL 的查询:

查询数学成绩为null的同学:

select name from student where math is null;

同理,不为空的数学成绩的学生:

 select name from student where math is not null;

分页查询:LIMIT: 

语法定义:

select ....from table_name [where....] [order by....] limit n offset s;

意思是从 s行开始,筛选 n 条结果。

如果去掉s,就是从0开始,筛选n条结果。

如以id为序查看前三个:

select id,name,chinese,math,english from student order by id limit 3 offset 0;

修改(Update): 

语法形式:

update 表 set 字段1=value1, 字段2=value2... where 条件

例如把王五的数学成绩改为66:

 update student set math=66 where name='王五';

 又或者:将总成绩倒数前三的 3 位同学的数学成绩加上 5 分

update student set math=math+10 order by chinese+math+english limit 3;

删除(Delete): 

注意:我们这里删除的是表的数据,实际的表依然存在,如果需要删除表,需要使用drop

语法形式:

delete from 表 where 条件

 如:删除张三的信息:

delete from student where name='张三'; 

 删除整个表的数据:

delete from student; 

只是删除了数据,表还在!!!

好的,以上就是MySQL数据库(表的CRUD基础操作(最常用))的全部内容了,希望对大家能有所帮助,留下你的点赞和收藏吧!!!

猜你喜欢

转载自blog.csdn.net/m0_67995737/article/details/127713289