From c92d5627ed3cab8cedfb67905ef2eec649eebdd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B7=AF=E7=8E=B2?= <1516489926@qq.com> Date: Sun, 12 Mar 2023 17:16:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...9 \345\244\247\344\275\234\344\270\232.md" | 191 ++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 "29 \350\267\257\347\216\262/20230309 \345\244\247\344\275\234\344\270\232.md" diff --git "a/29 \350\267\257\347\216\262/20230309 \345\244\247\344\275\234\344\270\232.md" "b/29 \350\267\257\347\216\262/20230309 \345\244\247\344\275\234\344\270\232.md" new file mode 100644 index 0000000..17993cd --- /dev/null +++ "b/29 \350\267\257\347\216\262/20230309 \345\244\247\344\275\234\344\270\232.md" @@ -0,0 +1,191 @@ +# mysql + +### 笔记 + +```mysql +1、关联查询的结果一共有7种: +两个表的记录分为四种: +①A表中的记录能在B表中找到对应的记录 +②A表中的记录在B表中找不到对应的记录 +③B表中的记录可以在A表找到对应的记录 +④B表中的记录在A表中找不到对应的记录 + +(1)A∩B ①③ +(2)A ①② +(3)A-A∩B ② +(4)B ③④ +(5)B-A∩B ④ +(6)A∪B ①②③④ +(7)A∪B-A∩B ②④ + +2、两个表要一起查询,要有前提条件:有关联 +就是有相同逻辑意义和数据类型的字段。 + +3、如何实现7种查询结果 +(1)内连接 inner join +(2)外连接 outer join +左外连接 left outer join 或 left join +右外连接 right outer join 或 right join +全外连接 full outer join 或 full join -- union 代替 + +但是,mysql不支持全外连接,没有full join。 +mysql使用union关键字合并其他的查询结果实现全外连接的效果。 + +内连接 ==> A∩B +左连接 ==> A 或 A-A∩B +右连接 ==> B 或 B-A∩B +全外连接 ==> A∪B 或 A∪B - A∩B + 左连接的A union 右连接的B 得到 A∪B + 左连接的A-A∩B union 右连接B-A∩B 得到 A∪B - A∩B + +4、内连接 inner join +A表 inner join B表 on 关联条件 + +5、左连接 left join +(1)A表:A表 left join B表 on A表.关联字段 = B表.关联字段 +(2)A-A∩B: + A表 left join B表 on A表.关联字段 = B表.关联字段 + where 从表.关联字段 is null + 从表是A和B 看占从表位置的表。 +6、右连接 right join +(1)B表:A表 right join B表 on A表.关联字段 = B表.关联字段 +(2)B-A∩B: + A表 right join B表 on A表.关联字段 = B表.关联字段 + where 从表.关联字段 is null + 从表是A还是B 得看占从表位置的表。 +7、使用union实现全外连接的效果 +(1)A∪B +转换为 左连接的A union 右连接的B +(2)A∪B - A∩B +转换为 左连接的A-A∩B union 右连接的B-A∩B +*/ +``` + + + +### 作业 + +```mysql +create database work6 charset utf8; +use work6; + +create table author( +author_id int primary key,-- 作家编号 +author_name varchar(20) not null unique key, -- 作家姓名, +credits int,-- 积分 +vip_id varchar(20) not null -- 等级编号 +); +constraint one foreign key(vip_id) references vip(vip_id) + +create table vip( +vip_id varchar(20) primary key,-- 等级编号 +vip_name varchar(20) unique key not null +); + +create table story( +story_id int primary key auto_increment,-- 作品编号 +author_id int,-- 作家编号 +type_id varchar(20), +story_name varchar(20), +views_number int -- 浏览量 +); + +constraint two foreign key (author_id) references author(author_id), +type_id varchar(20), +constraint three foreign key(type_id) references type(type_id), +story_name varchar(50), + +create table type( +type_id varchar(20) primary key, -- 类型编号 +type_name varchar(20) not null unique key -- 类型名称 +); +desc author; +insert into author values +(1001,'朱逸群',600,'vip02'), +(1002,'范建',8510,'vip04'), +(1003,'史珍香',981,'vip02'), +(1004,'范统',2364,'vip02'), +(1005,'杜子腾',257,'vip01'), +(1006,'刘产',678,'vip02'), +(1007,'杜琦燕',438,'vip03'); + +desc vip; +insert into vip values +('vip01','青铜作家'), +('vip02','白银作家'), +('vip03','黄金作家'), +('vip04','钻石作家'); +desc story; +insert into story values +(1,1002,' L03',' 母猪产后与护理师的二三事',6541), +(2,1005,' L04',' 拖拉机大战蜘蛛侠',563), +(3,1003,' L01',' 这只小龙虾不正经',8754), +(4,1006,' L04',' 一个爹爹三个娃',36354), +(5,1006,' L01',' 皇上滚开本宫只劫财',3674), +(6,1005,' L05',' 给长城贴瓷砖的小太监',6541), +(7,1003,' L03',' 不科学御兽', 1257), +(8,1005,' L01',' 镜面管理局', 3216), +(9,1004,' L02',' 关于我成为灭魂师之后',1147), +(10,1004,' L05',' 公子别秀',2078); + +desc type; +insert into type values +('L01',' 玄幻'), +('L02',' 奇幻'), +('L03',' 武侠'), +('L04',' 仙侠'), +('L05',' 都市'); + +-- 题目 +-- 所有题目要求使用SQL语句完成 +-- 1. 根据前面提供的表结构和表数据,创建数据库并分别创建这张四张表;并插入相关数据。(提醒:外键 +-- 请注意建表顺序和插入数据的顺序) (30分) +-- 2.将story表中的字段类型改成 。(2分) +alter table story modify story_name varchar(40); +-- 3. 在author表中增加一个性别字段 字段名:,类型: ,要求默认值为'男'。 (3分) +alter table author add author_sex char(10) default '男'; +-- 4. 将作家编号为1005、1007的作家性别改为'女' 。(2分) +update author set author_sex='女' where author_id in (1005,1007); +-- 5. 作家杜子腾,写了一篇名为《拜登夸我很帅》的都市小说,有854个浏览量,请将这条信息插入到story +-- 表。(3分) +insert into story values (null,1005,'L05','拜登夸我很帅',854); +-- 6. 《拖拉机大战蜘蛛侠》这篇小说,浏览量涨了100,请更新story表中的相关数据。(2分) +update story set views_number = views_number+100 where story_name = '拖拉机大战蜘蛛侠'; +-- 7. 请删除story表的中《皇上滚开本宫只劫财》这篇小说相关数据。(2分) +delete from story where story_name='皇上滚开本宫只劫财'; +-- 8. 查询 浏览量大于 8000的小说的作者编号和小说作品名称。(2分) +select author_id,story_name from story where views_number >8000; +-- 9. 查询积分大于1000 并且会员等级高于vip03的作家所有信息。(3分) +select * from author where credits > 1000 and vip_id>'vip03'; +-- 10. 查询姓名以杜字开头的作家的姓名,积分和等级编号。(3分) +select author_name,credits,vip_id from author where author_name like'杜%'; +-- 11. 查询积分在100、1000之间的作家信息,以积分降序排列。(3分) +select * from author where credits between 100 and 1000 order by credits desc; +-- 12. 查询出小说的总浏览量,最高浏览量,最小浏览量,平均浏览量,给字段用上中文别名。(3分) +select sum(views_number) 总浏览量,max(views_number) 最高浏览量,min(views_number) 最小浏览量,avg(views_number) 平均浏览量 from story ; +-- 13. 查询各种等级的作家的平均积分和作家数量,并对查询结果使用中文别名。(3分) +select avg(credits) 平均积分,count(author_id) 作家数量 from author group by vip_id; +-- 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) +select type_id,count(type_id) from story group by type_id having count(type_id)>2; +-- 15. 查询所有小说中浏览量最少的书的作品编号、作品名称和类型编号、浏览量。(4分) + +select story_id,story_name,type_id,views_number from story where views_number =(select min(views_number) from story); +-- 16. 查询积分比刘产高的作者所有信息。(5分) +select * from author where credits > (select credits from author where author_name ='刘产'); +-- 17. 查询出哪些白银作家是没有写小说的,显示这些作家的姓名、等级名称。(8分) +select author_name,vip_name from author a +left join story t on a.author_id = t.author_id +left join vip v on a.vip_id = v. vip_id +where story_id is null and vip_name = '白银作家'; +-- 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) +select * from story where author_id = any (select author_id from story where views_number >5000) and views_number<1000; +-- 19. 查询所有小说的小说编号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果 +-- 字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +select story_id 小说编号, story_name 小说名称, views_number 浏览量, t.type_id 分类名称,author_name 作者姓名,credits 作者积分,vip_name 作者等级名称 +from story s +left join type p on s.type_id = p.type_id +left join author a on s.author_id = a.author_id +left join vip v on a.vip_id = v.vip_id +order by views_number desc,credits desc; +``` + -- Gitee