MySQL 增删改查(基础 + 详解)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL 增删改查(基础 + 详解)

 🤞目录🤞

💖1. CRUD

💖2. 新增(Create)

2.1 单行数据 + 全列插入

2.2 多行数据 + 指定列插入

💖3. 查询(Retrieve)

3.1 全列查询

3.2 指定列查询

3.3 查询字段为表达式

3.4 别名

3.5 去重:DISTINCT

3.6 排序:ORDER BY

3.7 条件查询:WHERE

3.8 分页查询:LIMIT

💖4. 修改(Update)

💖5. 删除(Delete)


1. CRUD🚄

注释:在SQL中可以使用“--空格+描述来表示注释说明

-- "-- "表示注释说明

image.gif

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

2. 新增(Create)🚄

案例:

-- 创建一张学生表
DROP TABLE IF EXISTS student;
CREATE TABLE student (
    id INT,
    sn INT comment '学号',
    name VARCHAR(20) comment '姓名',
    qq_mail VARCHAR(20) comment 'QQ邮箱'
);

image.gif

image.gif编辑

2.1 单行数据 + 全列插入

(大小写都可)

-- values 中的值的顺序,必须严格按照建表时的顺序
INSERT INTO student VALUES (100, 10000, '张三', NULL);
INSERT INTO student VALUES (101, 10001, '李四', '11111');
insert into student values (102, 10002, '王五', '22222');

image.gif

image.gif编辑

2.2 多行数据 + 指定列插入

insert into student values (103, 10003, '宋江', null), 
(104, 10004, '卢俊义', '44444');
-- 没有出现的字段,填充默认值
insert into student (id,sn,name) values (105, 10005, '公孙胜'),
(106, 10006,'吴用');

image.gif

image.gif编辑

3. 查询(Retrieve)🚄

案例:

-- 创建考试成绩表
DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
  id INT,
  name VARCHAR(20),
  chinese DECIMAL(3,1),
  math DECIMAL(3,1),
  english DECIMAL(3,1)
);
-- 插入测试数据
insert into exam_result (id, name, chinese, math, english) values
    (1,'武松', 80, 92, 67), 
    (2,'石秀', 86, 72, 97),
    (3,'花容', 75, 98, 88),
    (4,'柴进', 97, 85, 78),
    (5,'李应', 87, 93, 87),
    (6,'鲁智深', 89, 90, 96);

image.gif

image.gif编辑

3.1 全列查询

-- 慎用
SELECT * FROM exam_result;

image.gif

image.gif编辑

3.2 指定列查询

select id, name from exam_result;

image.gif

image.gif编辑

3.3 查询字段为表达式

select id from exam_result;
select name, id from exam_result;
select 103 from exam_result;
select id, name, 103 from exam_result;
select 100 / 2 from exam_result;
select id * 10 from exam_result;
select id, name, english + math + chinese from exam_result;

image.gif

-- 和null比较值都为null, 不是 0 or 1
-- 有 null 参与的运算结果还是 null
-- 即使是 null,做bool值的时候,也看作 false
select * from student;
select id = 102 from student;
select id,id = 102 from student;
select id,id != 102 from student;
select id , name, qq_mail != null from student;
select id , name, qq_mail != 120  from student;
select id , name, qq_mail != null  from student;
select id , name, qq_mail + 10  from student;
select id = 102,id + 9901 from student;
select id = 102,id - 9901 from student;
select id , name, qq_mail + 10  from student;

image.gif

3.4 别名🚄

 as

select id, name, english + math + chinese as 总分 from exam_result;

image.gif

image.gif编辑

select id, name, english + math + chinese as total from exam_result;
select id,name,chinese as 语文,math as 数学,english as 英语 from exam_result;
select id,name,chinese as 语文,math as 数学,english as 英语 ,chinese + math + english as 总分 from exam_result;
-- as 可以省略
select id, name, english + math + chinese 总分 from exam_result;

image.gif

3.5 去重:DISTINCT

使用DISTINCT关键字对某列数据进行去重

select distinct math from exam_result;

image.gif

3.6 排序:ORDER BY

语法:

-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...] 
 ORDER BY column [ASC|DESC], [...];

image.gif

1. 没有 ORDER BY 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序

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

select * from student order by qq_mail;
select * from student order by qq_mail asc;
-- 结果相同,asc可以省略

image.gif

image.gif编辑

select * from student order by qq_mail desc;

image.gif

image.gif编辑

3. 使用表达式别名排序

select id,name,chinese + math + english from exam_result order by chinese + math + english desc;
select id, name, chinese + math + english as 总分 from exam_result order by 总分 desc;

image.gif

image.gif编辑

4. 可以对多个字段进行排序,排序优先级随书写顺序

-- 表示先以math 降序比较,若math相同则以chinese比较,若chinese也相同,则以english比较
select * from exam_result order by math desc ,chinese,english;

image.gif

image.gif编辑

3.7 条件查询:WHERE

指摘出较为重要的一部分:

-- 1. >= 
select id ,name ,math from exam_result where math >= 80;

image.gif

image.gif编辑

-- 2. <=>  也等于,NULL 安全('=',NULL 不安全),例如 NULL <=> NULL 的结果是 TRUE(1)
select * from exam_result where null <=> null;
-- 因为全为真,所有都查询

image.gif

image.gif编辑

-- 3. between .. and ..   [ .. ]
select id,name ,math from exam_result where math between 80 and 95;

image.gif

image.gif编辑

-- 4. in(option ..) 如果是option 里的就查询,指定查询
select * from exam_result where  name in ('武松', '鲁智深');

image.gif

image.gif编辑

-- 5. is null 
select * from exam_result where null is null ;
-- 因为全为真,所有都查询

image.gif

image.gif编辑

-- 6. like 模糊匹配。% 表示任意多个(包括 0 个)任意字符;
select * from exam_result where name like '鲁%';
select * from exam_result where name like '%智%';
select * from exam_result where name like '%深';
-- '_' 表示任意一个字符
select * from exam_result where name like '鲁__';
-- 结果相同

image.gif

image.gif编辑

-- 7. and or not 
select * from exam_result where chinese > 80 and english >80; 
select * from exam_result where chinese > 80 or english >80; 
select * from exam_result where not english >80;

image.gif

1. WHERE条件可以使用表达式,但不能使用别名。

2. AND的优先级高于OR,在同时使用时,需要使用小括号()包裹优先执行的部分

3.8 分页查询:LIMIT

-- 分页
-- limit : 限制  每次多少个结果
-- offset : 起始位置,从 0 开始计算
-- limit l offset o;
-- limit o, l;
-- limit l;   <-> limit l offset 0;
-- 起始位置:0,限制:2
select * from exam_result order by math, id limit 2;
-- 起始位置:3,限制:2
select * from exam_result order by math, id limit 2 offset 3;
-- 起始位置:3,限制:2
select * from exam_result order by math, id limit 3, 2;

image.gif

image.gif编辑

4. 修改(Update)🚄

语法:

UPDATE table_name SET column = expr [, column = expr ...]
 [WHERE ...] [ORDER BY ...] [LIMIT ...]

image.gif

-- 将武松同学的数学成绩变更为 80 分
UPDATE exam_result SET math = 80 WHERE name = '武松';
-- 将鲁智深同学的数学成绩变更为 60 分,语文成绩变更为 70 分
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '鲁智深';
-- 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT
3;

image.gif

5. 删除(Delete)🚄

语法:

DELETE FROM  table_name [WHERE ...] [ORDER BY ...] [LIMIT ..

image.gif

DELETE FROM exam_result WHERE name = '武松';
DROP TABLE IF EXISTS for_delete;
CREATE TABLE for_delete (
 id INT,
 name VARCHAR(20)
);
-- 插入测试数据
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
-- 删除整表数据
DELETE FROM for_delete;

image.gif


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
SQL 关系型数据库 MySQL
【MySQL】基本查询(表的增删改查)-- 详解(上)
【MySQL】基本查询(表的增删改查)-- 详解(上)
|
4月前
|
关系型数据库 MySQL
mysql连表查询基本增删改查
mysql连表查询基本增删改查
37 2
|
4月前
|
存储 关系型数据库 MySQL
【mysql】—— 表的增删改查
【mysql】—— 表的增删改查
|
2月前
|
JavaScript 关系型数据库 MySQL
创建nodejs项目并接入mysql,完成用户相关的增删改查的详细操作
创建nodejs项目并接入mysql,完成用户相关的增删改查的详细操作
44 0
|
17天前
|
JavaScript 关系型数据库 MySQL
node连接mysql,并实现增删改查功能
【8月更文挑战第26天】node连接mysql,并实现增删改查功能
32 3
|
14天前
|
关系型数据库 MySQL 数据库
MySQL数据库的增删改查
MySQL数据库的增删改查
11 0
|
1月前
|
关系型数据库 MySQL 大数据
C#使用SqlSugar操作MySQL数据库实现简单的增删改查
C#使用SqlSugar操作MySQL数据库实现简单的增删改查
137 2
|
22天前
|
SQL 关系型数据库 MySQL
"Python与MySQL的浪漫邂逅:一键掌握增删改查,开启你的数据库编程之旅!"
【8月更文挑战第21天】Python因其简洁的语法和强大的库支持,成为连接数据库的首选工具。本文介绍如何使用Python连接MySQL数据库并执行基本操作。首先需安装`mysql-connector-python`库。通过配置连接信息建立数据库连接后,可利用`cursor.execute()`执行SQL语句进行数据的增删改查,并通过`commit()`提交更改。查询时使用`fetchall()`或`fetchone()`获取结果。记得处理异常及关闭连接以释放资源。掌握这些基础,有助于高效进行数据库编程。
27 0
|
28天前
|
前端开发 Java 关系型数据库
通过HTML网页对mysql数据库进行增删改查(CRUD实例)
通过HTML网页对mysql数据库进行增删改查(CRUD实例)
137 0
|
3月前
|
SQL Java 数据库连接
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
54 2

热门文章

最新文章