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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 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


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3天前
|
关系型数据库 MySQL
mysql连表查询基本增删改查
mysql连表查询基本增删改查
24 2
|
3天前
|
存储 关系型数据库 MySQL
【mysql】—— 表的增删改查
【mysql】—— 表的增删改查
|
3天前
|
存储 关系型数据库 MySQL
MySQL表的操作『增删改查』
MySQL表的操作『增删改查』
40 0
MySQL表的操作『增删改查』
|
3天前
|
存储 关系型数据库 MySQL
MySQL库的操作『增删改查 ‖ 编码问题 ‖ 备份与恢复』
MySQL库的操作『增删改查 ‖ 编码问题 ‖ 备份与恢复』
55 0
|
3天前
|
关系型数据库 MySQL 数据库
|
3天前
|
SQL 关系型数据库 MySQL
MySQL | 数据库的管理和操作【表的增删改查】(一)
MySQL | 数据库的管理和操作【表的增删改查】
|
3天前
|
SQL 关系型数据库 MySQL
MySQL | 数据库的管理和操作【表的增删改查】(二)
MySQL | 数据库的管理和操作【表的增删改查】(二)
|
3天前
|
SQL 关系型数据库 MySQL
MySQL 基本概念 基础用法 增删改查(特殊查询)语法 详细篇
MySQL 基本概念 基础用法 增删改查(特殊查询)语法 详细篇
|
3天前
|
存储 SQL 关系型数据库
MySQL表的增删改查---多表查询和联合查询
MySQL表的增删改查---多表查询和联合查询
|
3天前
|
JavaScript 前端开发 API
node+vue3+mysql前后分离开发范式——实现对数据库表的增删改查
node+vue3+mysql前后分离开发范式——实现对数据库表的增删改查
38 1

推荐镜像

更多