目录

数据库造神计划第七天-增删改查CRUD3

数据库造神计划第七天—增删改查(CRUD)(3)

https://i-blog.csdnimg.cn/direct/7d591b544f874d8a8495f2f641c7d7a5.jpeg

🔥个人主页:

🎬作者简介:Java研发方向学习者

📖个人专栏: 、《 ****

⭐️人生格言:没有人生来就会编程,但我生来倔强!!!


https://i-blog.csdnimg.cn/direct/7bc50643513c4841ab37a6a5e97eb4b5.gif


续接上一话:

一、Retrieve 检索

1、分页查询

        当我们在浏览器上搜索某个内容时,会有很多的搜索结果,这些结果都给用户也看不完,就不如用分页的模式一部分一部分的进行展示!!!

1.1语法

-- 起始下标为 0 
-- 从 0 开始,筛选 num 条结果 
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT num;
-- 从 start 开始,筛选 num 条结果 
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT start , num;
-- 从 start 开始,筛选 num 条结果,⽐第⼆种⽤法更明确,建议使⽤ 
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT num OFFSET start;

https://i-blog.csdnimg.cn/direct/00317d1de39049e3848e6a3b564d606f.png

1.2示例

# 查询第⼀⻚数据
select * from exam order by id asc limit 0, 3;

https://i-blog.csdnimg.cn/direct/0aaa03ca1b024deca14883d0633446a4.png

# 查询第⼆⻚数据
select * from exam order by id asc limit 3, 3;

https://i-blog.csdnimg.cn/direct/a63961cf248b4691885daf85f618b656.png

 # 查询第三⻚数据,没有达到limit的条数限制,也不会有任何影响,有多少条就显⽰多少条
select * from exam order by id asc limit 6, 3;

https://i-blog.csdnimg.cn/direct/ef2562cf92f14b7fa0933b527ad058c7.png

二、Update修改

        这是真正在修改数据库的原始数据!!!

1、语法

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
  SET assignment [, assignment] ...
  [WHERE where_condition]
  [ORDER BY ...]
  [LIMIT row_count]

#注:

(1)对符合条件的结果进行列值更新

(2)要先查找,找到之后再进行修改

2、示例

        将孙悟空同学的数学成绩变更为80分

# 查看原始数据
select * from exam where name = '孙悟空';

https://i-blog.csdnimg.cn/direct/b380dd6f55634c00bb329d3e931a5ab2.png

# 更新操作
update exam set math = 80 where name = '孙悟空';
# 查看结果,数学成绩更新成功
select * from exam where name = '孙悟空';

https://i-blog.csdnimg.cn/direct/9be5eca8e28a439fbc733388740c2218.png

        将曹孟德同学的数学成绩变更为60分,语文成绩变更为70分

 # 查看原始数据
select name, math, chinese from exam where name = '曹孟德';

https://i-blog.csdnimg.cn/direct/818b5f488a264ad7b17078abe312e26a.png

# 更新操作
update exam set math = 60, chinese = 70 where name = '曹孟德';
# 查看结果
select name, math, chinese from exam where name = '曹孟德';

https://i-blog.csdnimg.cn/direct/dcb7681d30814ffe868c2c2770398567.png

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

# 查看原始数据
select name, math,chinese + math + english as 总分 from exam where chinese + math + english is not null order by 总分 asc limit 3;

https://i-blog.csdnimg.cn/direct/028c1d72ed6d4996ab8fc7871041d3ea.png

# 更新操作
update exam set math = math +30 where chinese + math + english is not null order by chinese + math + english asc limit 3;
# 查看结果
select name, math, chinese + math + english as 总分 from exam where name in ('宋公明','刘⽞德','曹孟德');

https://i-blog.csdnimg.cn/direct/66aaf821056243d584ba90217bf6fd7f.png

# 修改后总成绩倒数前三的 3 位同学和数据成绩
select name, math,chinese + math + english as 总分 from exam where chinese + math + english is not null order by 总分 asc limit 3;

https://i-blog.csdnimg.cn/direct/c191114e18f54fd7baf288d70db9db4a.png

        将所有同学的语文成绩更新为原来的2倍

# 查看原始数据
select * from exam;

https://i-blog.csdnimg.cn/direct/1b4741e81cae40eb8d30268dfc556082.png

# 更新操作
update exam set chinese = chinese * 2;
# 查看结果
select * from exam;

https://i-blog.csdnimg.cn/direct/66c4f14fdc8e4095829adb4150cad598.png

#注:

(1)以原值的基础上做变更时,不能使用math+=30这样的语法

(2)不加where条件时,会导致全表数据被更新,谨慎操作

三、Delete 删除

        drop 删库,删表.
        delete 删记录/删行

1、语法

DELETE FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]

2、示例

        删除孙悟空同学的考试成绩

# 查看原始数据
select * from exam where name = '孙悟空';

https://i-blog.csdnimg.cn/direct/914050802ef5433597c02c887befd47a.png

# 删除操作
delete from exam where name = '孙悟空';
# 查看结果
select * from exam where name = '孙悟空';

https://i-blog.csdnimg.cn/direct/6cd8aeba1ac14206a8b2e81137e97d62.png

        删除整张表数据

# 准备测试表
CREATE TABLE t_delete (
  id INT,
  name VARCHAR(20)
);
# 插⼊测试数据
INSERT INTO t_delete (id, name) VALUES (1, 'A'), (2, 'B'), (3, 'C');
# 查看测试表
select * from t_delete;

https://i-blog.csdnimg.cn/direct/dd55232153614eeb90f35b064a490ef7.png

# 删除整张表中的数据
delete from t_delete;
# 查看结果
select * from t_delete;

https://i-blog.csdnimg.cn/direct/90c2d97b37fa48438274a93236c40e86.png

3、Delete注意事项

        执行Delete时不加条件会删除整张表的数据,谨慎操作

四、截断表

        截断表,更快速更高效的删除表操作,直接站在文件的角度,把表对应的文件内容清空了,只能删全表(不能指定条件,删除某个部分)

1、语法

TRUNCATE [TABLE] tbl_name

2、示例

# 准备测试表
CREATE TABLE t_truncate (
     id INT PRIMARY KEY AUTO_INCREMENT,
     name VARCHAR(20)
     );
# 插⼊测试数据
INSERT INTO t_truncate (name) VALUES ('A'), ('B'), ('C');
# 查看测试表
select * from t_truncate;

https://i-blog.csdnimg.cn/direct/2e2ef5cb04c44083806e89227183ea2a.png

# 查看建表结构,AUTO_INCREMENT= 4
show create table t_truncate;

https://i-blog.csdnimg.cn/direct/8daa5cdd5cf1456e97beaa51b3ee6835.png

# 截断表,注意受影响的⾏数是0
truncate table t_truncate;
# 查看表中的数据
select * from t_truncate;

https://i-blog.csdnimg.cn/direct/78cb262fb0ef4b2180bf095ce3ba05e3.png

# 查看表结构,AUTO_INCREMENT已被重置为0
show create table t_truncate\G

https://i-blog.csdnimg.cn/direct/b3fc2cc4bec6468b80149a51b9b05e24.png

# 继续写⼊数据
INSERT INTO t_truncate (name) VALUES ('D');
# ⾃增主键从1开如计数
select * from t_truncate;

https://i-blog.csdnimg.cn/direct/4be4635edeba4f058c07c5c00e246671.png

# 再次查看表结构,AUTO_INCREMENT=2
show create table t_truncate;

https://i-blog.csdnimg.cn/direct/4d6ab3b1aec741f7bb752d26f55bf020.png

3、Truncate注意事项

(1)只能对整表操作,不能像DELETE⼀样针对部分数据

(2)不对数据操作所以比DELETE更快,TRUNCATE在删除数据的时候,不经过真正的事物,所以无法回滚

(3)会重置AUTO_INCREMENT项

        由于内容较多,会分为多篇讲解,预知后续内容,请看后续博客!!!