MySQL表的增删改查(基础)
目录
一.操作表中的数据(DML)
1.新增数据
1.1 单行数据+全列插入
1.2 多行数据+指定列插入
2.更新数据
2.1 将所有性别改为女(慎用)
2.2 将id为2的学生性别改为男
2.3 将id为2的学生的年龄改为(20),地址改为天宫
3.删除数据
3.1 删除id为1的数据
3.2 删除所有数据
二.查询表中的数据(DQL)
1.简单查询
1.1 全列查询
1.2 指定列查询
1.3 查询字段为表达式
1.4 别名查询
1.5 去重查询
1.6 order by 排序
2.条件查询(where)
2.1 基本查询
2.2 and与or
2.3 范围查询 in,between...and...
2.4 模糊查询 Like
2.5 NULL查询 is null(is not null)
3.分页查询(limit)
一.操作表中的数据(DML)
数据准备
--创建一个student表
表中字段:
学号, id int
姓名, name varchar(20)
年龄, age int
性别, sex char(1)
地址, address varchar(40)
# 创建学生表 CREATE TABLE student(id INT,name VARCHAR(20),age INT,sex CHAR(1),address VARCHAR(40) );
1.新增数据
1.1 单行数据+全列插入
语法格式:insert to 表名 values(对应的数据库字段);
#插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致
insert to student values(1,'孙悟空',20,'男','花果山');
insert to student values(2,'猪八戒',18,'男','云栈洞');
1.2 多行数据+指定列插入
语法格式:insert to 表名 (指定的数据库字段列)values(与指定的数据库字段列相对应)
#插入两条记录,value_list 数量必须和指定列数量及顺序一致
insert to student (id,name,age,sex,address) values
(3,'沙悟净',18,'男','流沙河'),
(4,'白龙马',16,'男','东海龙宫'),
(5,'哪吒',17,'女','天宫');
注意:
① 值与字段必须要对应,个数相同&数据类型相同
②值的数据大小,必须在字段指定的长度范围内 insert into 表名 (字段名1,字段名2...) values(字段值1,字段值2...); 。
③varchar char date类型的值必须使用单引号,或者双引号 包裹。
④如果要插入空值,可以忽略不写,或者插入null 。
⑤如果插入指定字段的值,必须要上写列名。
2.更新数据
认识一个关键字where ,这个关键字在mysql语句中称为条件查询语句(在查询的时候会细讲)
语法格式:不带条件的修改
update 表名 set 列名 = 值;(慎用)
语法格式:带条件的修改
update 表名 set 列名 = 值 where 字段名 = 值;(字段名为定义的字段,值是保存的字段下的数据)
2.1 将所有性别改为女(慎用)
update student set sex = '女'
2.2 将id为2的学生性别改为男
update student set sex = '男' where id = 2;
2.3 将id为2的学生的年龄改为(20),地址改为天宫
update student set age = 20,address = '天宫' where id = 2;
3.删除数据
语法格式1: 删除所有数据
delete from 表名;
语法格式2: 指定条件删除数据
delete from 表名 where 字段名 = 值;
3.1 删除id为1的数据
delete from student where id = 1;
3.2 删除所有数据
delete from student;
二.查询表中的数据(DQL)
数据准备
创建考试成绩表
创建考试成绩表
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,'唐三藏', 67, 98, 56),
(2,'孙悟空', 87.5, 78, 77),
(3,'猪悟能', 88, 98.5, 90),
(4,'曹孟德', 82, 84, 67),
(5,'刘玄德', 55.5, 85, 45),
(6,'孙权', 70, 73, 78.5),
(7,'宋公明', 75, 65, 30);
查询操作不会对数据库中的内容进行修改,只是显示数据库的内容
1.简单查询
1.1 全列查询
查询成绩表的所有信息
语法格式 :select 列名 from 表名;
# *代表所有字段
select * from exam_result;
1.2 指定列查询
查询id,name的学生
语法格式:select 字段1,字段2 from 表名;
#字段不用按照指定顺序书写
select id,name from exam_result;
1.3 查询字段为表达式
计算每个学生的总成绩,且只显示 id,name,和总分数
select id,name, chinese + math + english from exam_result;
1.4 别名查询
将所有学生的信息查询出来,并将列名改为中文
别名查询就是给字段起外号,比如 表中的字段 id 我要让他查询显示为 学号,这样就叫别名查询,别名查询需要用到关键字as(也可以省略)
语法格式 :select id as '起的别名' from exam_result;
select id as '学号',name '姓名',chinese '语文',math '数学',english '英语' from exam_result;
1.5 去重查询
对math这列进行去重处理
对某列数据去重
语法格式:select distinct 列名 from 表名;
select distinct math from exam_result;
1.6 order by 排序
asc(升序排序)从小到大
desc (降序排序) 从大到小
语法格式:select 列名 from 表名 order by 列名 desc;
单列排序 对数学成绩降序排序
select name,math from exam_result order by math desc;
对别名排序 对总分升序排序
select name,chinese + math + english total from exam_result order total asc;
多个字段排序 对数学成绩,以及id排序
select id,name,math from exam_result order by id desc,math desc;
2.条件查询(where)
如果查询语句中没有设置条件,就会查询所有的行信息,在实际应用中,一定要指定查询条件,对记录进行过滤
语法格式:select 列名 from 表名 where 条件表达式;
#先取出表中的每条数据,满足条件的数据就返回,不满足的就过滤掉
比较运算符:
运算符 | 名称 | 说明 | 示例 |
---|---|---|---|
> | 大于 | 比较左侧值是否大于右侧值 | salary > 5000 |
>= | 大于等于 | 比较左侧值是否大于或等于右侧值 | age >= 18 |
< | 小于 | 比较左侧值是否小于右侧值 | score < 60 |
<= | 小于等于 | 比较左侧值是否小于或等于右侧值 | quantity <= 100 |
= | 等于(NULL 不安全) | 比较两侧值是否相等NULL = NULL 返回 NULL | status = 'active' NULL = NULL → NULL |
<=> | 等于(NULL 安全) | 比较两侧值是否相等(包括 NULL )NULL <=> NULL 返回 TRUE(1) | phone <=> NULL NULL <=> NULL → 1 |
!= 或 <> | 不等于 | 比较两侧值是否不相等 | department != 'HR' status <> 'expired' |
BETWEEN a0 AND a1 | 范围匹配 | 检查值是否在 [a0, a1] 闭区间内a0 <= value <= a1 返回 TRUE(1) | age BETWEEN 18 AND 30 date BETWEEN '2023-01-01' AND '2023-12-31' |
IN (option, ...) | 集合匹配 | 检查值是否在指定集合中 | id IN (101, 205, 307) status IN ('active', 'pending') |
IS NULL | 是否为 NULL | 检查值是否为 NULL | email IS NULL |
IS NOT NULL | 是否不为 NULL | 检查值是否不为 NULL | phone IS NOT NULL |
LIKE | 模糊匹配 | 使用通配符匹配文本:% - 任意多个字符(含0个)_ - 单个任意字符 |
逻辑运算符:
运算符 | 说明 |
AND | 多个条件必须都为 TRUE(1),结果才是 TRUE(1) |
or | 任意一个条件为 TRUE(1), 结果为 TRUE(1) |
not | 条件为 TRUE(1),结果为 FALSE(0) |
注意:
1. WHERE条件可以使用表达式,但不能使用别名。
2. AND的优先级高于OR,在同时使用时,需要使用小括号()包裹优先执行的部分
2.1 基本查询
查询英语不及格的同学及英语成绩 ( < 60 )
select name,english from exam_result where english < 60;
查询语文成绩好于英语成绩的同学
select name ,chinese,english from exam_result where chinese > english;
查询总分在 200 分以下的同学
select name,chinese + english + math 总分 from exam_result where chinese + english + math <200;
2.2 and与or
查询语文成绩大于80分,且英语成绩大于80分的同学
select name,chinese,english from exam_result where chinese > 80 and english > 80;
查询语文成绩大于80分,或英语成绩大于80分的同学
select name,chinese,english from exam_result where chinese > 80 or english > 80;
观察AND 和 OR 的优先级
select name,chinese,english from exam_result where chinese > 80 or english > 80 and english > 70;
select name,chinese,english from exam_result where (chinese > 80 or english) > 80 and english > 70;
2.3 范围查询 in,between...and...
查询语文成绩在 [80, 90] 分的同学及语文成绩
select name,chinese from exam_result where chinese between 80 and 90;
使用 AND 也可以实现
select name,chinese from exam_result where chinese >= 80 and chinese <= 90;
2.4 模糊查询 Like
通过模糊查询姓孙的同学信息
select name from exam_result where name like '%孙%';
通过模糊查询孙姓为两字的名字
select name from exam_result where name like '孙_';
2.5 NULL查询 is null(is not null)
查询 有英语成绩的同学姓名
select name from exam_result where english is not null;
查询 没有英语成绩的同学姓名
select name from exam_rseult where exam_result is null;
3.分页查询(limit)
limit 关键字的作用
1. limit是限制的意思,用于 限制返回的查询结果的行数 (可以通过limit指定查询多少行数据)
2. limit 语法是 MySql的方言,用来完成分页
语法格式:SELECT 字段1,字段2... FROM 表名 LIMIT offset , length;
limit offset , length; 关键字可以接受一个 或者两个 为0 或者正整数的参数
offset 起始行数, 从0开始记数, 如果省略 则默认为 0.
length 返回的行数
查询成绩表中的前5条数据
select * from exam_result limit 5;
select * from exam_result limit 0,5;
查询成绩表中从第四条开始,查询2条
select * from exam_result limit 3,2;
分页操作,每页显示2条数据
select * from exam_result 0,2;
select * from exam_result 2,2;
select * from exam_result 4,2;
本内容有错误的地方请大家在评论区指出!我们一起学习进步 !!!下一个内容我们将学习多表查询以及事务管理.....!!!
完结撒花!!!