【MySQL】表的增删查改

作者主页:paper jie_博客

本文作者:大家好,我是paper jie,感谢你阅读本文,欢迎一建三连哦。

本文录入于《MySQL》专栏,本专栏是针对于大学生,编程小白精心打造的。笔者用重金(时间和精力)打造,将MySQL基础知识一网打尽,希望可以帮到读者们哦。

其他专栏:《算法详解》《C语言》《javaSE》《数据结构》等

内容分享:本期将会分享MySQL表的基础增删查改

目录

前言

新增(Create)

语法

全列插入单行数据

指定列插入多行

查询(Tetrieve)

语法

全列查询

指定列查询

查询字段为表达式

别名查询

去重查询(distinct )

排序(order by)

条件查询(where)

基本查询

and与or

between and

in

模糊查询: like

null查询: is(not)null

分页查询(limit)

修改(update)

语法

全列修改

指定修改

删除(delete)

语法

删除整个表内容

删除指定记录

总结


前言

在程序猿这个圈子里一直有一个常用词CRUD,也就是我们口中的增删查改. 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写.这里我们聊的就是MySQL的表的增删查改.

新增(Create)

语法

insert into 表名 values(参数.....);
insert into 表名(类型) values(参数....);

 举例准备:

create table student(id int, name varchar(20), sex varchar(20));

全列插入单行数据

insert into student values(1, '孙悟空', '石头人');
insert into student values(2, '八戒', '猪');

指定列插入多行

insert into student(id,name) values(3, '唐僧');
insert into student(name) values('金角大王');

查询(Tetrieve)

语法

//全列查询
select * from 表名; 
//指定列查询
select (类型,类型....) from 表名;
 

栗子前准备:

create table score(naem varchar(20), chinese decimal(4, 2), english decimal(4,2), math decimal(4,2));

全列查询

select * from score;

指定列查询

select name, math from score;

查询字段为表达式

select name, chinese+english+math from score;

别名查询

select name, chinese+math+english astotal from score;

去重查询(distinct )

select distinct math from score;

排序(order by)

//升序排序:
select * from score order by math;
//降序排序:
select * from score order by math desc;

使用表达式及别名排序:

//升序:
select name, math+chinese+english as total from score order by total;
//降序:
select name, math+chinese+english as total from score order by total desc;

多个字段进行排序:

//升序:
select * from score order by math, english, chinese;
//降序:
select * from score order by math, english, chinese desc;

条件查询(where)

比较运算符:

运算符

作用
> >= < <=

大于,大于等于,小于,小于等于

=等于,但是null不同,null = null 的结果是null
<=>等于,可以使用null,null<=>null的结果是true
!= <>不等于
between..and...范围表达  [a1, a2] 闭区间

in(a1, a2, a3....)

是括号里的任意一个,返回true
is null是null
is not null不是null
like模糊匹配 %表示任意多个字符  (字符%)

逻辑运算符:

运算符作用
and多个条件为true,才为true
or任意一个条件为true,结果就为true
not 条件结果为true,结果为false

这里需要注意:

where条件查询的时候不可以使用别名

and的优先级高于or

基本查询

select * from score where math < 60;

select * from score where Chinese > english;

select * from score where chinese+math+english < 150;

and与or

select * from score where chinese >60 and math > 60;

select * from score where chinese >60 or math > 60;

select * from score where chinese >60 or math > 60 and english < 80;

between and

select * from score where chinese berween 60 and 90;

in

select * from score where chinese in(10, 39, 60, 70, 80);

模糊查询: like

select * from score where name like '孙%';

select * from score where name like '%孙';

//只能匹配一个字符
select * from score where name like '孙_';

select * from score where name like '_孙';

null查询: is(not)null

select * from score where name is null;

select * from score where name is not null;

分页查询(limit)

//默认从第一行开始查询:
select * from score limit 3;
//从指定行开始查询:
select * from score limit 3 offset 0;

注意这里的0就是第一行,和数组的下标是一样的~

修改(update)

语法

update 表名 set 参数 = 预期值;

全列修改

update score set chinese = 80;

指定修改

update score set chinese = 70 where math > 60;

删除(delete)

语法

delete from 表名;
delete from 表名 where 限定条件;

删除整个表内容

delete from score;

删除指定记录

delete from score where english < 60;

总结

新增:

单行插入:

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


-- 多行插入
insert into 表(字段1, ..., 字段N) values
(value1, ...),
(value2, ...),
(value3, ...);

查询:

-- 全列查询
select * from 表


-- 指定列查询
select 字段1,字段2... from 表


-- 查询表达式字段
select 字段1+100,字段2+字段3 from 表


-- 别名
select 字段1 别名1, 字段2 别名2 from 表


-- 去重DISTINCT
select distinct 字段 from 表


-- 排序ORDER BY
select * from 表 order by 排序字段


-- 条件查询WHERE:
-- (1)比较运算符 (2)BETWEEN ... AND ... (3)IN (4)IS NULL (5)LIKE (6)AND (7)OR
(8)NOT
select * from 表 where 条件

修改:

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

删除:

delete from 表 where 条件

  • 75
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 72
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

paper jie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值