MySQL表的增删改查

发布于:2024-05-23 ⋅ 阅读:(74) ⋅ 点赞:(0)

 博主主页: 码农派大星.

数据库专栏:MySQL数据库

关注博主带你了解更多MySQL数据库知识


1. CRUD

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

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

2. 新增(Create) 

create table stu_test(
    id int,name varchar(20) comment'姓名',
chinese int,math int,english int)

我们上篇创建了一张表

现在我们来插入数据:

2.1 单行数据 + 全列插入

 insert into stu_test values(1001,'蔡虚坤',44,55,76);

插入一条记录,stu_test 数量必须和定义表的列的数量及顺序一致 

2.2 多行数据 + 指定列插入 

insert into stu_test(id,name,chinese,math,english)values
    -> (1002,'蔡依林',34,56,78),
    -> (1003,'孙悟空',45,56,87),
    -> (1004,'唐三藏',56,78,98),
    -> (1005,'猪无能',78,80,89);

3. 查询(Retrieve) 

3.1 全列查询

 select * from stu_test;

3.2 指定列查询 

 select id,name,math from stu_test;

3.3 查询字段为表达式 

表达式包含一个字段:

select id,name,math+10 from stu_test;

表达式包含多个字段 :

 select id,name,math+chinese+english from stu_test;

3.4 别名 

为查询结果中的列指定别名,表示返回的结果集中,以别名作为该列的名称,语法:

SELECT id, name, chinese + math + english 总分 from stu_test;

3.5 去重:DISTINCT 

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

56分重复了

select distinct math from stu_test;

去重结果: 

3.6 排序:ORDER BY 

ASC 为升序(从小到大)

-- DESC 为降序(从大到小)

-- 默认为 ASC

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

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

select name,chinese from stu_test order by chinese;

 select name,chinese from stu_test order by chinese desc;

3. 使用表达式及别名排序

 select name,chinese+math+english from stu_test order by chinese+math+english;

select name,chinese+math+english 总分 from stu_test order by 总分 desc;

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

SELECT name, math, english, chinese FROM stu_test order by math desc ,english,chinese;
-- 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示

3.7 条件查询:WHERE 

比较运算符:

逻辑运算符:

注意:

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

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

基本查询: 

select name math from stu_test where math < 60;
-- 查询数学不及格的同学及数学成绩 ( < 60 )

 SELECT name, chinese, english FROM stu_test WHERE chinese < english;
-- 查询语文成绩不好于英语成绩的同学


SELECT name, chinese + math + english 总分 FROM stu_test
    -> where  chinese + math + english < 200;

-- 查询总分在 200 分以下的同学

 AND与OR:

SELECT * FROM stu_test WHERE chinese > 70 and english > 80;
-- 查询语文成绩大于70分,且英语成绩大于80分的同学

 SELECT * FROM stu_test WHERE chinese > 70 or english > 80;

-- 查询语文成绩大于70分,或英语成绩大于80分的同学

  观察AND 和 OR 的优先级:

SELECT * FROM stu_test WHERE chinese > 50 or math>70 and english > 70;

SELECT * FROM stu_test WHERE (chinese > 50 or math>70) and english > 70;

范围查询: 

1. BETWEEN ... AND
 SELECT name, chinese FROM stu_test WHERE chinese BETWEEN 60 AND 90;

 查询语文成绩在 [60, 90] 分的同学及语文成绩

SELECT name, chinese FROM stu_test WHERE chinese >= 60 AND chinese <= 90;
使用and也能实现

2. IN 
SELECT name, math FROM stu_test WHERE math IN (58, 59, 80, 78);
 查询数学成绩是 58 或者 59 或者 78 或者 80 分的同学及数学成绩

SELECT name, math FROM stu_test WHERE math = 58 OR math = 59 OR math = 80 or math = 78;

使用or也能查出

模糊查询:LIKE 

select name from stu_test where name like '蔡%'

-- % 匹配任意多个(包括 0 个)字符

 select name from stu_test where name like '%无%';

select name from stu_test where name like '蔡__';

_ 匹配严格的一个任意字符

NULL 的查询:IS [NOT] NULL 

 select name,english from stu_test where english is null;
查询 english 未知的同学姓名

 select name,english from stu_test where english is not null;
查询 english 已知的同学姓名

 3.8 分页查询:LIMIT

起始下标为 0

 select name from stu_test limit 3;
- 从 0 开始,筛选 3 条结果

select name from stu_test limit 3,2;

 从 3 开始,筛选 2条结果

select name from stu_test limit 3 offset 2;

从 2 开始,筛选 3 条结果,比第二种用法更明确,建议使用

按 id 进行分页,每页 3 条记录,分别显示 第 1、2、3 页

SELECT id, name, math, english, chinese FROM stu_test ORDER BY id LIMIT 3 offset 0;


 SELECT id, name, math, english, chinese FROM stu_test ORDER BY id LIMIT 3 offset 3;


SELECT id, name, math, english, chinese FROM stu_test ORDER BY id LIMIT 3 offset 5;

4. 修改(Update)

语法:

UPDATE table_name SET column = expr [, column = expr ...]
 [WHERE ...] [ORDER BY ...] [LIMIT ...]
 将孙悟空同学的数学成绩变更为 80 分

 update stu_test set math = 80 where name = '孙悟空';

 将孙悟空同学的数学成绩变更为 60 分,语文成绩变更为 70 分

 select math, chinese from stu_test where name = '孙悟空';

- 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分

update stu_test set math = math + 30 order by chinese+math+english limit 3;

将所有同学的语文成绩更新为原来的 2 倍
UPDATE exam_result SET chinese = chinese * 2;

5. 删除(Delete) 

语法:

DELETE FROM  table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
删除孙悟空同学的考试成绩
DELETE FROM exam_result WHERE name = '孙悟空';
- 删除整张表数据
delete  from stu_test;

删除操作比较危险,这里小编就不给大家演示了哈! 


网站公告

今日签到

点亮在社区的每一天
去签到