From 4df51016c08a335bbf028bf0680868ab6133b2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=AE=8F=E8=BE=BE?= <2657224306@qq.com> Date: Mon, 13 Mar 2023 23:18:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=80=9C=E7=AC=AC=E5=85=AD=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\346\254\241\344\275\234\344\270\232.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "48 \351\251\254\345\256\217\350\276\276/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" diff --git "a/48 \351\251\254\345\256\217\350\276\276/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" "b/48 \351\251\254\345\256\217\350\276\276/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..2fb8f80 --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,37 @@ +create database xiaoshuo charset utf8; use xiaoshuo; create table author( author_id int primary key, -- 作家编号 author_name varchar(20) not null unique, -- 作家姓名 credits int, -- 积分 vip_id varchar(20), -- 等级编号 foreign key(vip_id) references vip(vip_id) ); + +create table vip( vip_id varchar(20) primary key, -- 等级编号 vip_name varchar(20) not null unique -- 等级名称 ); + +create table story( story_id int primary key auto_increment,-- 作品编号 author_id int, -- 作家编号,外键,关联作家信息表 type_id varchar(20), -- 类型编号,外键,关键作品类型表 story_name varchar(50), -- 作品名称 views_number int, -- 浏览量 foreign key(author_id) references author(author_id), foreign key(type_id) references type(type_id) ); + +create table type( type_id varchar(20) primary key, -- 类型编号,主键 type_name varchar(20) not null unique -- 类型名称,非空,不能重复 ); alter table author charset utf8; alter table vip charset utf8; alter table story charset utf8; alter table type charset utf8; + +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'); insert into vip values ('VIP01','青铜作家'), ('VIP02','白银作家'), ('VIP03','黄金作家'), ('VIP04','钻石作家'); 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','公子别秀6541',2078); insert into type values ('L01','玄幻'), ('L02','奇幻'), ('L03','武侠'), ('L04','仙侠'), ('L05','都市'); + +-- 请注意建表顺序和插入数据的顺序) (30分) -- 2. 将story 表中的story_name字段类型改成varchar(40) 。(2分) alter table story modify story_name varchar(40); + +-- 3. 在author表中增加一个性别字段 字段名:author_sex,类型:char(10),要求默认值为'男'。 (3分) + +alter table author add author_sex char(10) default '男'; -- 4. 将作家编号为1005、1007的作家性别改为'女' 。(2分) select * from author; update author set author_sex='女' where author_id='1005' or author_id='1007'; -- 5. 作家杜子腾,写了一篇名为《拜登夸我很帅》的都市小说,有854个浏览量,请将这条信息插入到story表。(3分) select * from story; select * from vip; select * from type; insert into story values(11,1005,'L05','拜登夸我很帅',854); -- 6. 《拖拉机大战蜘蛛侠》这篇小说,浏览量涨了100,请更新story表中的相关数据。(2分) update story set views_number=views_number+100 where story_name='拖拉机大战蜘蛛侠'; select * from story; -- 7. 请删除story表的中《皇上滚开本宫只劫财》这篇小说相关数据。(2分) delete from story where story_name='皇上滚开本宫只劫财'; + +-- 8. 查询 浏览量大于 8000的小说的作者编号和小说作品名称。(2分) select story_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 * from story; select sum(views_number) 总浏览量,max(views_number) 最高浏览量,min(views_number) 最小浏览量,avg(views_number) 平均浏览量 from story; + +-- 13. 查询各种等级的作家的平均积分和作家数量,并对查询结果使用中文别名。(3分) select vip_id 作家等级,avg(credits) 平均积分,count(author_name) 作家数量 from author group by vip_id; -- 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) + +-- 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 vip v left join author a on v.vip_id=a.vip_id left join story s on a.author_id=s.author_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 s.story_id 小说编号, s.story_name 小说名称, s.views_number 浏览量, t.type_id 分类名称, a.author_name 作者姓名, a.credits 作者积分, v.vip_name 作者等级 from story s left join type t on s.type_id=t.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 From 57bbfa006e50855483e5070f02950affcd47b595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=AE=8F=E8=BE=BE?= <2657224306@qq.com> Date: Mon, 13 Mar 2023 23:32:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=80=9C=E7=AC=AC=E4=B8=83=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...03\346\254\241\344\275\234\344\270\232.md" | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 "48 \351\251\254\345\256\217\350\276\276/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.md" diff --git "a/48 \351\251\254\345\256\217\350\276\276/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.md" "b/48 \351\251\254\345\256\217\350\276\276/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..522f0d6 --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,90 @@ +# + +# 作业 + +CREATE DATABASE m4 CHARSET utf8; +use m4 +CREATE table m2( +stuNo varchar(10), +stuName VARCHAR(10), +stuAge int, +stuAddress VARCHAR(10), +stuSeat int, +stuSex int); +desc m2; +INSERT into m2(stuNo,stuName,stuAge,stuAddress,stuSeat,stuSex) +VALUES('s2501','张秋丽',20,'美国硅谷',1,1), + ('s2502','李舒汶',88,'湖北武汉',2,0), + ('s2503','马文才',22,'湖南长沙',3,1), + ('s2504','欧阳俊雄',21,'湖北武汉',4,0), + ('s2505','梅超风',20,'湖北武汉',5,1), + ('s2506','陈璇风',19,'美国硅谷',6,1), + ('s2507','陈风',20,'美国硅谷',7,0); +SELECT *FROM m2 ; +use m4 +CREATE table m6( + examNo int, + stuNO varchar(10), + writeenExam int, + labExam int); + desc m6 + INSERT into m6 VALUES + (1,'s2501',50,70), + (2,'s2502',60,65), + (3,'s2503',86,85), + (4,'s2503',40,80), + (5,'s2505',70,90), + (6,'s2506',85,90); + SELECT * FROM m6 +-- 按图片所给的数据进行数据表的建立和数据插入,然后进行以下查询操作 +-- +-- 1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +SELECT stuno 学号,stuname 名字,stuage 年龄,stuaddress 地址,stuseat 座号,stusex 性别 from m2; +-- 2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +SELECT stuname,stuage,stuaddress from m2; +-- 3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +select stuno 学号,writeenExam 笔试,labExam 机试 from m6; +-- 5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 +select stuno 学号, writeenExam 笔试, labExam 机试,(writeenExam+labExam)总分 from m6; +-- 6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select DISTINCT stuaddress 来自 from m2; +-- 7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 +SELECT stuage 年龄 from m2; +-- 8.查询学生信息表(stuInfo)中前3行记录 +SELECT * from m2 LIMIT 3; +-- 9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +SELECT stuname 姓名, stuseat 座位号 from m2 LIMIT 4; +-- 11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +SELECT stuaddress +-- 12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from m6 where labExam between 60 and 80 order by labExam desc; +-- 13.查询来自湖北武汉或者湖南长沙的学生的所有信息 +select * from m2 where stuAddress='湖北武汉' or stuAddress='湖南长沙'; +-- 14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from m6 where writtenExam not between 70 and 90 order by writtenExam asc; +-- 15.查询年龄没有写的学生所有信息 +select * from m2 where stuAge is null; +-- 16.查询年龄写了的学生所有信息 +select * from m2 where stuage is not null; +-- 17.查询姓张的学生信息 +select * from m2 where stuName like '张%'; +-- 18.查询学生地址中有‘湖’字的信息 +select * from m2 where stuAddress like '%湖%'; +-- 19.查询姓张但名为一个字的学生信息 +select * from m2 where stuName like '张_'; +-- 20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from m2 where stuname like '__俊%'; +-- 21.按学生的年龄降序显示所有学生信息 +select * from m2 order by stuAge desc; +-- 22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from m2 order by stuAge desc,stuSeat asc; +-- 23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select examNO,a.stuNO,writtenExam,labExam from m2 a right join stuexam b on a.stuno=b.stuNO where writtenExam=(select max(writtenExam) from stuexam); +-- 24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select examNO,a.stuNO,writtenExam,labExam from m2 a right join stuexam b on a.stuno=b.stuNO where labExam=(select min(labExam) from m6); +-- 25.查询每个地方的学生的平均年龄 +select stuAddress,round(avg(stuAge),2) 平均年龄 from m2 group by stuAddress; +-- 26.查询男女生的分别的年龄总和 +select stuSex,sum(stuAge) 年龄总和 from m2 group by stuSex; +-- 27.查询每个地方的男女生的平均年龄和年龄的总和 +select stuSex,round(avg(stuAge),2) 平均年龄,sum(stuAge) 年龄总和 from m2 group by stuSex; -- Gitee