From 39a67ec6ea3b34919fd5d2b7d2fe3ec3fae75207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=8E=B9=E8=8E=B9?= <3148816327@qq.com> Date: Wed, 8 Mar 2023 15:12:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=98=E8=8E=B9=E8=8E=B9=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\350\201\224\346\237\245\350\257\242 .md" | 210 ++++++++++++++++++ ...6 \345\255\220\346\237\245\350\257\242.md" | 156 +++++++++++++ 2 files changed, 366 insertions(+) create mode 100644 "36 \345\210\230\350\216\271\350\216\271/20230227 \345\205\263\350\201\224\346\237\245\350\257\242 .md" create mode 100644 "36 \345\210\230\350\216\271\350\216\271/20230306 \345\255\220\346\237\245\350\257\242.md" diff --git "a/36 \345\210\230\350\216\271\350\216\271/20230227 \345\205\263\350\201\224\346\237\245\350\257\242 .md" "b/36 \345\210\230\350\216\271\350\216\271/20230227 \345\205\263\350\201\224\346\237\245\350\257\242 .md" new file mode 100644 index 0000000..95aab45 --- /dev/null +++ "b/36 \345\210\230\350\216\271\350\216\271/20230227 \345\205\263\350\201\224\346\237\245\350\257\242 .md" @@ -0,0 +1,210 @@ +```sql +create database test charset utf8; +use test; +``` + +-- 1.设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 + +(一)Student(学生表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ------------ | +| Sno | varchar (20) | 否 | 学号(主码) | +| Sname | varchar (20) | 否 | 学生姓名 | +| Ssex | varchar (20) | 否 | 学生性别 | +| Sbirthday | datetime | 可 | 学生出生年月 | +| Class | varchar (20) | 可 | 学生所在班级 | + +(二)Course(课程表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| ------ | ------------ | -------- | ---------------- | +| Cno | varchar (20) | 否 | 课程号(主码) | +| Cname | varchar (20) | 否 | 课程名称 | +| Tno | varchar (20) | 否 | 教工编号(外码) | + +(三)Score(成绩表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| -------------- | ------------ | -------- | -------------- | +| Sno | varchar (20) | 否 | 学号(外码) | +| Cno | varchar (20) | 否 | 课程号(外码) | +| Degree | Decimal(4,1) | 可 | 成绩 | +| 主码:Sno+ Cno | | | | + +(四)Teacher(教师表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ---------------- | +| Tno | varchar (20) | 否 | 教工编号(主码) | +| Tname | varchar (20) | 否 | 教工姓名 | +| Tsex | varchar (20) | 否 | 教工性别 | +| Tbirthday | datetime | 可 | 教工出生年月 | +| Prof | varchar (20) | 可 | 职称 | +| Depart | varchar (20) | 否 | 教工所在部门 | + +```sql +drop table if exists Student; +create table Student( + Sno varchar (20) primary key, + Sname varchar (20) not null, + Ssex varchar (20) not null, + Sbirthday datetime, + Class varchar (20) +); +desc Student; +create table Course( + Cno varchar (20) primary key, + Cname varchar (20) not null, + Tno varchar (20) not null, + foreign key (Tno) references Teacher(Tno) +); +desc Course; +drop table if exists Score; +create table Score( + Sno varchar (20) not null, + Cno varchar (20) not null, + Degree Decimal(4,1), + foreign key(Sno) references Student(Sno), + foreign key(Cno) references Course(Cno) +); +desc Score; +create table Teacher( + Tno varchar (20) primary key, + Tname varchar (20) not null, + Tsex varchar (20) not null, + Tbirthday datetime, + Prof varchar (20), + Depart varchar (20) not null +); +desc Teacher; +``` + +-- 3. 数据库中的数据: +-- -- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ + +```sql +select * from Student,Score,Course where Student.Sno = Score.Sno and Score.Cno = Course.Cno; +``` + +-- -- 2,查询没有学生的教师的所有信息 + +```sql +select * from Teacher where Tno = (select Tno from Course left join Score on Course.Cno=Score.Cno where Sno is null); +``` + +#添加记录 + +表(一)Student + +| Sno | Sname | Ssex | Sbirthday | class | +| ---- | ----- | ---- | --------- | ----- | +| 108 | 曾华 | 男 | 1977-9-1 | 95033 | +| 105 | 匡明 | 男 | 1975-10-2 | 95031 | +| 107 | 王丽 | 女 | 1976-1-23 | 95033 | +| 101 | 李军 | 男 | 1976-2-20 | 95033 | +| 109 | 王芳 | 女 | 1975-2-10 | 95031 | +| 103 | 陆君 | 男 | 1974-6-3 | 95031 | + +表(二)Course + +| Cno | Cname | Tno | +| ----- | ---------- | ---- | +| 3-105 | 计算机导论 | 825 | +| 3-245 | 操作系统 | 804 | +| 6-166 | 数字电路 | 856 | +| 9-888 | 高等数学 | 831 | + +表(三)Score + +| Sno | Cno | Degree | +| ---- | ----- | ------ | +| 103 | 3-245 | 86 | +| 105 | 3-245 | 75 | +| 109 | 3-245 | 68 | +| 103 | 3-105 | 92 | +| 105 | 3-105 | 88 | +| 109 | 3-105 | 76 | +| 101 | 3-105 | 64 | +| 107 | 3-105 | 91 | +| 108 | 3-105 | 78 | +| 101 | 6-166 | 85 | +| 107 | 6-166 | 79 | +| 108 | 6-166 | 81 | + +表(四)Teacher + +| Tno | Tname | Tsex | Tbirthday | Prof | Depart | +| ---- | ----- | ---- | --------- | ------ | ---------- | +| 804 | 李诚 | 男 | 1958-12-2 | 副教授 | 计算机系 | +| 856 | 张旭 | 男 | 1969-3-12 | 讲师 | 电子工程系 | +| 825 | 王萍 | 女 | 1972-5-5 | 助教 | 计算机系 | +| 831 | 刘冰 | 女 | 1977-8-14 | 助教 | 电子工程系 | + +```sql +insert into Student (Sno,Sname,Ssex,Sbirthday,class)VALUES +(108,'曾华','男','1977-9-1',95033), +(105,'匡明','男','1975-10-2',95031), +(107,'王丽','女','1976-1-23',95033), +(101,'李军','男','1976-2-20',95033), +(109,'王芳','女','1975-2-10',95031), +(103,'陆君','男','1974-6-3',95031); +select * from Student; +insert into Course(Cno,Cname,Tno) VALUES +(3-105,'计算机导论',825), +(3-245,'操作系统',804), +(6-166,'数字电路',856), +(9-888,'高等数学',831); +select * from Course; +insert into Score(Sno,Cno,Degree) VALUES +(103,3-245,86), +(105,3-245,75), +(109,3-245,68), +(103,3-105,92), +(105,3-105,88), +(109,3-105,76), +(101,3-105,64), +(107,3-105,91), +(108,3-105,78), +(101,6-166,85), +(107,6-166,79), +(108,6-166,81); +select * from Score; + +insert into Teacher(Tno,Tname,Tsex,Tbirthday,Prof,Depart) VALUES +(804,'李诚','男','1958-12-2','副教授','计算机系'), +(856,'张旭','男','1969-3-12','讲师','电子工程系'), +(825,'王萍','女','1972-5-5','助教','计算机系'), +(831,'刘冰','女','1977-8-14','助教','电子工程系'); +select * from Teacher; +-- 查询Score表中的最高分的学生学号和课程号。 + +select max(Sno),max(Cno) from Score; +``` + +-- 查询所有学生的Sname、Cno和Degree列。 + +```sql +select Sname,Cno,Degree from Student left join Score on Student.Sno = Score.Sno; +UNION +select Sname,Cno,Degree from Student right join Score on Student.Sno = Score.Sno; +``` + +-- 查询所有学生的Sno、Cname和Degree列。 + +```sql +select Student.Sno,Cname,Degree from Student,Score,Course where Student.Sno=Score.Sno and Score.Cno=Course.Cno; +``` + +-- 查询所有学生的Sname、Cname和Degree列。 + +```sql +select Sname,Cname,Degree from Student,Score,Course where Student.Sno=Score.Sno and Score.Cno=Course.Cno; +``` + +-- 查询“95033”班学生的平均分。 + +```sql +select class,AVG(Degree) from Student left join Score on Student.Sno=Score.Sno where class=95033; +``` + diff --git "a/36 \345\210\230\350\216\271\350\216\271/20230306 \345\255\220\346\237\245\350\257\242.md" "b/36 \345\210\230\350\216\271\350\216\271/20230306 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..6be1a72 --- /dev/null +++ "b/36 \345\210\230\350\216\271\350\216\271/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,156 @@ +```sql +#建库建表 +create database test1 charset utf8; +use test1; +drop table if exists stuinfo; +create table stuinfo( +stuno varchar(255), +stuname varchar(4), +stusex char, +stuage varchar(255), +stuaddress varchar(255), +stuseal int +); +show tables; +desc stuinfo; +create table stuexam( +examno int , +stuno varchar(255), +writtenexam varchar(255), +labexam varchar(255) +); +desc stuexam; +drop table if exists stumarks; +create table stumarks( +examno varchar(3), +stuid varchar(6), +score varchar(3) +); +desc stumarks; +#添加记录 +insert into stuinfo VALUES ('s2501','张秋利','男','20','美国硅谷',1), +('s2502','李斯文','女','18','湖北武汉',2), +('s2503','马文才','男','18','湖南长沙',3), +('s2504','欧阳俊雄','女','21','湖北武汉',4), +('s2505','梅超风','男','20','湖北武汉',5), +('s2506','陈旋风','男','19','美国硅谷',6); +insert into stuexam VALUES (1,'s2501','50','70'), +(2,'s2502','60','65'), +(3,'s2503','86','70'), +(4,'s2504','40','80'), +(5,'s2505','70','85'), +(6,'s2506','85','90'); +insert into stumarks VALUES('1','s2501','88'), +('2','s2501','92'), +('3','s2501','53'), +('4','s2502','60'), +('5','s2502','99'), +('6','s2503','82'); +select * from stuinfo; +select * from stuexam; +select * from stumarks; +``` + +![](C:\Users\86157\Pictures\Saved Pictures\数据库表效果图(1).bmp) + +-- 1.查询出年龄比班上平均年龄大的学生的信息 + +```sql +select avg(stuage) from stuinfo; + +select * from stuinfo where stuage > (select avg(stuage) from stuinfo); +``` + +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) + +```sql +select max(score) from stumarks group by stuid; + +select stuno,stuname,stusex,sc from stuinfo left join (select stuid, max(score) sc from stumarks group by stuid) a on stuinfo.stuno = a.stuid; +``` + +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + +```sql +select (writtenexam + labexam)/2 from stuexam; + +select stuinfo.stuno ,stuname,stusex,(select (writtenexam + labexam)/2) from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno; +``` + +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) + +```sql +#普通查询 +select * from stuinfo where stusex = '男' && stuage >='20'; +#子查询 +select + +``` + +-- 5.查询出年龄比所有男生年龄都大的女生的信息 + +```sql +select stuage from stuinfo where stusex = '男'; + +select * from stuinfo where stusex = '女' && stuage > any(select stuage from stuinfo where stusex = '男'); +``` + +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) + +```sql +select * from stuinfo right join stumarks on stuinfo.stuno = stumarks.stuid +select * from stumarks where score >60; +``` + +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +```sql +#普通查询 +select * from stuinfo right join stumarks on stuinfo.stuno = stumarks.stuid where score > 90; +#子查询 + +select * from stumarks where score > 90; + +select * from stuinfo right join (select stuid from stumarks where score > 90) b on stuinfo.stuno = b.stuid; +``` + +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) + +```sql +select stuinfo.* from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where (writtenexam + labexam)/2 > 80; +``` + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + +```sql +select writtenexam,labexam from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where stuname = '张秋利'; + +select stuinfo.* from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where writtenexam > (select writtenexam from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where stuname = '张秋利') && labexam > (select labexam from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where stuname = '张秋利'); +``` + +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + +```sql +select stuinfo.* from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where writtenexam > (select writtenexam from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where stuname = '张秋利') or labexam > (select labexam from stuinfo left join stuexam on stuinfo.stuno = stuexam.stuno where stuname = '张秋利'); +``` + +-- 13.查询班上比所有男生年龄都要大的女生的信息 + +```sql +select stuage from stuinfo where stusex = '男'; + +select * from stuinfo where stusex = '女' && stuage > all(select stuage from stuinfo where stusex = '男'); +``` + +-- 14.查询出只是比某个男生年龄大的女生的信息 + +```sql +select stuage from stuinfo where stusex = '男'; + +select * from stuinfo where stusex = '女' && stuage > any(select stuage from stuinfo where stusex = '男'); +``` + -- Gitee From b0ac3ad11762d93d2e959c7eb14205b1009d71fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=8E=B9=E8=8E=B9?= <3148816327@qq.com> Date: Thu, 9 Mar 2023 07:43:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=98=E8=8E=B9=E8=8E=B9=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230308 \347\273\203\344\271\240.md" | 315 ++++++++++++++++++ 1 file changed, 315 insertions(+) create mode 100644 "36 \345\210\230\350\216\271\350\216\271/20230308 \347\273\203\344\271\240.md" diff --git "a/36 \345\210\230\350\216\271\350\216\271/20230308 \347\273\203\344\271\240.md" "b/36 \345\210\230\350\216\271\350\216\271/20230308 \347\273\203\344\271\240.md" new file mode 100644 index 0000000..c2fd2d8 --- /dev/null +++ "b/36 \345\210\230\350\216\271\350\216\271/20230308 \347\273\203\344\271\240.md" @@ -0,0 +1,315 @@ +# 作业一 + +![](C:\Users\86157\Pictures\Saved Pictures\作业二数据图片(1).bmp) + +```sql +create database test3 charset utf8; +use test3; +drop table if exists date; +create table date( +orderid char, +orderdate varchar(50) +); +show tables; +desc date; +create table item( +itemid int, +orderid char, +itemtype varchar(6), +itemname varchar(6), +thenumber varchar(3), +themoney DECIMAL +); +desc item; +insert into date values('1','2008-01-12 00:00:00.000'), +('2','2008-02-10 00:00:00.000'), +('3','2008-02-15 00:00:00.000'), +('4','2003-03-10 00:00:00.000'); +select * from date; +insert into item values (1,'1','文具','笔','72','2'), +(2,'1','文具','尺','10','1'), +(3,'1','体育用品','篮球','1','56'), +(4,'2','文具','笔','36','2'), +(5,'2','文具','固体胶','20','3'), +(6,'2','日常用品','透明胶','2','1'), +(7,'2','体育用品','羽毛球','20','3'), +(8,'3','文具','订书机','20','3'), +(9,'3','文具','订书针','10','3'), +(10,'3','文具','裁纸刀','5','5'), +(11,'4','文具','笔','20','2'), +(12,'4','文具','信纸','50','1'), +(13,'4','日常用品','毛巾','4','5'), +(14,'4','日常用品','透明胶','30','1'), +(15,'4','体育用品','羽毛球','20','3'); +select * from item; +``` + + +-- 1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +```sql +select date.orderid,orderdate,itemtype,itemname thenumber,themoney from date right join item on date.orderid = item.orderid; +``` + +-- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +```sql +select item.orderid,orderdate,itemtype,itemname from date right join item on date.orderid = item.orderid where thenumber > 50; +``` + +-- 3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +```sql +select thenumber * themoney from item; +select item.orderid,orderdate,itemtype,itemname,thenumber,themoney from date right join item on date.orderid = item.orderid left join (select orderid, thenumber * themoney from item) a on item.orderid = a.orderid; +``` + +-- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +```sql +select item.orderid,orderdate,itemtype,itemname,thenumber,themoney from date right join item on date.orderid = item.orderid left join (select orderid, thenumber * themoney from item) a on item.orderid = a.orderid where themoney >= 5 or thenumber >= 50; +``` + + +-- 5.查询每个订单分别订购了几个产品,例如: + -- 编号 订购产品数 + -- 1 3 + -- 2 4 + +```sql + select item.orderid 编号,thenumber 订购产品数 from date right join item on date.orderid = item.orderid; +``` + +-- 6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + + -- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +```sql + select orderid 订单编号,itemtype 产品类别,count(itemtype) 订购次数,sum(thenumber) 总数量 from item group by orderid,itemtype; +``` + + + +# 作业二 + +1. 作家信息表 ( author ) (4分) + +| 字段名称 | 数据类型 | 说明及要求 | +| ----------- | ----------- | --------------------------------- | +| author_id | int | 作家编号,主键 | +| author_name | varchar(20) | 作家姓名、非空、不能重复 | +| credits | int | 积分 | +| vip_id | varchar(20) | 等级编号,非空、外键关联等级信息表 | + +2. 作家等级信息表 ( vip ) (3分) + +| 字段名称 | 数据类型 | 说明及要求 | +| -------- | ----------- | ------------------------ | +| vip_id | varchar(20) | 等级编号,主键 | +| vip_name | varchar(20) | 等级名称,非空,不能重复 | + + +3. 小说作品信息表 ( story )(4分) + +| 字段名称 | 数据类型 | 说明及要求 | +| ------------ | ----------- | ----------------------------- | +| story_id | int | 作品编号,主键,自增 | +| author_id | int | 作家编号,外键,关联作家信息表 | +| type_id | varchar(20) | 类型编号,外键,关键作品类型表 | +| story_name | varchar(50) | 作品名称 | +| views_number | int | 浏览量 | + +4. 小说作品类型表 ( type )(2分) + + +| 字段名称 | 数据类型 | 说明及要求 | +| --------- | ----------- | ------------------------ | +| type_id | varchar(20) | 类型编号,主键 | +| type_name | varchar(20) | 类型名称,非空,不能重复 | + +2、对应的表数据 + +1. 作家信息表 (4分) + + +| 作家编号 | 作家名称 | 积分 | 等级编号 | +| :------: | :------: | :--: | :------: | +| 1001 | 朱逸群 | 600 | VIP01 | +| 1002 | 范建 | 8510 | VIP04 | +| 1003 | 史珍香 | 981 | VIP02 | +| 1004 | 范统 | 2364 | VIP02 | +| 1005 | 杜子腾 | 257 | VIP01 | +| 1006 | 刘产 | 678 | VIP02 | +| 1007 | 杜琦燕 | 438 | VIP03 | + +2. 等级信息表(2分) + +| 等级编号 | 等级名称 | +| :------: | :------: | +| VIP01 | 青铜作家 | +| VIP02 | 白银作家 | +| VIP03 | 黄金作家 | +| VIP04 | 钻石作家 | + +3. 小说作品信息表(5分) + +| 作品编号 | 作家编号 | 类型编号 | 作品名称 | 订阅数 | +| :------: | :------: | :------: | :----------------------: | :----: | +| 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 | + +4. 作品类型(3分) + +| 类型编号 | 类型名称 | +| :------: | :------: | +| L01 | 玄幻 | +| L02 | 奇幻 | +| L03 | 武侠 | +| L04 | 仙侠 | +| L05 | 都市 | + + +### + +```sql +create database xiaoshuo charset utf8; +use xiaoshuo; +-- 1.作家信息表 ( author ) +drop table if exists author; +create table author( +author_id int primary key, +author_name varchar(20) not null unique key, +credits int , +vip_id varchar(20) not null, +foreign key(vip_id) references vip(vip_id) +); +-- 2.作家等级信息表 ( vip ) +drop table if exists vip; +create table vip( +vip_id varchar(20) primary key, +vip_name varchar(20) not null unique key +); +-- 3.小说作品信息表 ( story ) +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) +); +-- 4.小说作品类型表 ( type ) +drop table if exists type; +create table type( +type_id varchar(20) primary key, +type_name varchar(20) not null unique key +); +#添加记录 +-- 作家信息表 +insert into author values +(1001,'朱逸群',600 ,'VIP01'), +(1002,'范建', 8510,'VIP04'), +(1003,'史珍香', 981,'VIP02'), +(1004 ,'范统' ,2364,'VIP02'), +(1005,'杜子腾',257,'VIP01'), +(1006,'刘产',678 ,'VIP02'), +(1007,'杜琦燕' ,438 ,'VIP03'); +-- 2.等级信息表 +insert into vip VALUES +('VIP01','青铜作家'), +('VIP02','白银作家'), +('VIP03','黄金作家'), +('VIP04','钻石作家'); +-- 3.小说作品信息表 +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); +-- 4.作品类型 +insert into type values +('L01','玄幻'), +('L02','奇幻'), +('L03','武侠'), +('L04','仙侠'), +('L05','都市'); +select * from author; +select * from vip; +select * from story; +select * from type; +-- 1. 根据前面提供的表结构和表数据,创建数据库并分别创建这张四张表;并插入相关数据。(提醒:外键请注意建表顺序和插入数据的顺序) (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 column author_sex char(10) default '男'; + + +-- 4. 将作家编号为1005、1007的作家性别改为'女' 。(2分) +update author set author_sex = '女' where author_id = '1005' or author_id = '1007'; + +-- 5. 作家杜子腾,写了一篇名为《拜登夸我很帅》的都市小说,有854个浏览量,请将这条信息插入到story表。(3分) +insert into story values(11,1005,'LO5','拜登夸我很帅',854); +-- 6. 《拖拉机大战蜘蛛侠》这篇小说,浏览量涨了100,请更新story表中的相关数据。(2分) + + +-- 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 left join vip on author.vip_id = vip.vip_id +where credits > 1000 && author.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_name) 作家数量 from author group by vip_id; + +-- 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) +select type_id 分类编号,count(story_id) 小说数量 from story group by type_id; + +-- 15. 查询所有小说中浏览量最少的书的作品编号、作品名称和类型编号、浏览量。(4分) +select min(views_number) from story; +select story_id,story_name,type_id,views_number from story where views_number = (select min(views_number) from story); +-- 16. 查询积分比刘产高的作者所有信息。(5分) +select credits from author where author_name = '刘产'; +select * from author where credits > (select credits from author where author_name = '刘产'); +-- 17. 查询出哪些白银作家是没有写小说的,显示这些作家的姓名、等级名称。(8分) +select author_name,vip_name from vip right join author on vip.vip_id = author.vip_id left join story on author.author_id = story.author_id +where story_id is null; +-- 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) + +-- 19. 查询所有小说的小说编号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +``` + -- Gitee