2019年3月20日星期三
1 统计男生的人数,显示性别 ,人数(写三种形式)
2 统计学号40和学号2的补考(sc.score)数量,显示学号(student.sid
sc.sid),姓名(student.sname)
3 找出需要补考(sc)的人,显示学号(sc.sid, student.sid),姓名(student),补考课程名称(course)
4 演示数据库第三范式
5 统计分数小于60的人数和人次,显示人数,人次
实体 Entity 关系Relation
|
实体1 |
实体2 |
备注 |
|
1 |
1 |
某个实体信息很多,拆分成两张表,比如用户信息、用户登录信息 |
|
1 |
N |
在多的实体中,定义引用实体1主键字段做为外键 |
|
M |
N |
将两个实体中主键字段提取出来,至少再生成一张表 |
分析外卖系统的如下实体关系
|
实体 |
实体 |
关系 |
如何建表 |
|
卖家 |
商品 |
|
|
|
买家 |
商品 |
|
|
|
买家 |
地址 |
|
|
数据库管理系统DBMS也称之为关系数据库管理系统 RDBMS,关系就是数据库中的表
针对外卖系统表:
统计某天消费人数
统计某天消费人次
查询杨涛订的外卖,显示姓名,菜名,数量,金额
杨涛 米饭 1 20
杨涛 面条 1 15
统计潘涛2月花费总额,显示姓名,总额
统计每个人2月消费总额,并按总额降序,显示姓名,总额
查询杨皓文2月分卖出的商品总额,显示姓名,总额
类别实体(自身一对多,设计表时增加一个字段表示引用主键字段值)
|
类别id |
类别名称 |
父类id |
|
1 |
水果 |
-1 |
|
2 |
蔬菜 |
-1 |
|
3 |
苹果 |
1 |
|
4 |
青菜 |
2 |
|
5 |
肉类 |
-1 |
|
6 |
羊肉 |
5 |
|
|
|
|
流水号:
订单信息表(id 流水号 PK , oid mid 。。。。
序列/流水号/自动增长字段
SQL>create table testseq(a int primary key ,b char(100));
SQL>create sequence ord_seq;
SQL>insert into testseq(a,b) values(ord_seq.nextval,’aaa’);
SQL>insert into testseq(a,b) values(ord_seq.nextval,’bbb’);
SQL>select * from testseq;
mysql>create table testseq(a int auto_increment primary key,
b char(100));
mysql>insert into testseq(b) values(‘aaa’);
mysql>insert into testseq(b) values(‘bbb’);
mysql>select * from testseq;
数据集合运算:并集union 交集intersect 差集(补集)minus
SQL>create table seta(a int ,b int);
SQL> 1 1
2 3
SQL>create table setb(a int ,b int);
2 3
3 4
SQL>select * from seta
union/intersect/minus
select * from setb;
统计每个人B等级【>=70 <90】和C等级【>=60 <70】的数量,显示学号,等级,数量
|
学号 |
等级 |
数量 |
|
1 |
B |
2 |
|
2 |
B |
2 |
|
1 |
C |
2 |
|
。。。。。。。 |
|
|
统计每个人B等级【>=70 <90】和C等级【>=60 <70】的数量,显示学号,姓名,等级,数量
|
学号 |
姓名 |
等级 |
数量 |
|
1 |
Zs |
B |
2 |
|
2 |
Ls |
B |
2 |
|
1 |
Zs |
C |
2 |
|
。。。。。。。 |
|
|
|
做好的,根据实体关系将外卖系统的表进行调整
1 统计男生的人数,显示性别 ,人数(写三种形式)
2 找出同名的人显示学号,姓名,性别,年龄
3 统计平均成绩80以上的课程,显示课程号,课程名称
4 有如下表any
|
学号 |
姓名 |
课程名 |
成绩 |
|
1 |
Zs |
数学 |
30 |
|
1 |
Zs |
语文 |
60 |
|
2 |
Ls |
英语 |
79 |
|
1 |
Zs |
物理 |
80 |
|
2 |
Ls |
物理 |
90 |
|
。。。。。 |
|
|
|
找出数学比英语多考20分以上的人,显示学号,姓名
|
A |
|
B | ||||||
|
学号 |
姓名 |
课程名 |
成绩 |
|
学号 |
姓名 |
课程名 |
成绩 |
|
1 |
Zs |
数学 |
90 |
1 |
Zs |
数学 |
90 | |
|
1 |
Zs |
语文 |
60 |
1 |
Zs |
语文 |
60 | |
|
2 |
Ls |
英语 |
79 |
2 |
Ls |
英语 |
79 | |
|
1 |
Zs |
英语 |
60 |
1 |
Zs |
英语 |
60 | |
|
2 |
Ls |
物理 |
90 |
2 |
Ls |
物理 |
90 | |
|
。。。。。 |
|
|
|
。。。。。 |
|
|
| |

from any a,any b where a.sid=b.sid and a.kcm=’Math’ and b.kcm=’En’ and a.cj-b.cj>=20
2019年3月22日星期五
演讲:
说说操作系统(挑和系统相关的实践,从大到小阐述)
说说SQL(目前阐述数据库设计和查询)
ps -ef|grep sshd
子查询:
查询的结果,单列数据或多列数据
统计总分最高的人,显示学号
select sid,sum(score) from sc group by sid;
select sid,max(sum(score)) from sc group by sid;
select max(sum(score))
from (
select sid,sum(score) from sc group by sid );
第一范式:
select max(zf) 最大总分
from (
select sid,sum(score) zf from sc group by sid );
select sid,sum(score) sid from sc group by sid;
select *
from (
select sid,sum(score) sid from sc group by sid );
方法一
select sid,sum(score) from sc group by sid
having sum(score)
>=(
select max(zf) from
( select sum(score) zf from sc group by sid )
);
方法二:all
select sid,sum(score) from sc group by sid
having sum(score)>=all(
select sum(score) from sc group by sid
);
查询同名的人,显示学号,姓名,性别,年龄
select distinct a.* from student a, student b
where ( 连接条件 )
子查询演示
方法一
select * from student where sname in
(
select sname from student group by sname having count(*)>1
);
方法二:
select a.* from student a ,
(
select sname from student group by sname having count(*)>1
) b where a.sname=b.sname;
统计平均分最高的人,显示学号,姓名
select a.sid, sname
from student a, sc where a.sid=sc.sid
group by a.sid,sname having avg(score)>=all(
select avg(score) from sc group by sid
);
找出选修了所有课程,且总分最高的人,显示学号,姓名
having 总分>=all( 每个人的总分 )
and 选课数量= ( 课程总数 )
找出学过潘涛老师(teacher)所带的课程(sc.cid course.cid)的学生,
显示学号sc.sid student.sid,姓名student.sname
a.sid,sname,d.tid,d,tname
from student a, sc b,course c ,teacher d
where a.sid=sc.sid and sc.cid=c.cid and c.tid=d.tid and d.tname=’aaaa’;
找出吃过编号为10001商家所有菜的人,显示编号、姓名(外卖系统)
提示:找出每个人吃过的10001商家的不同菜的数量,和
算出的10001商家的菜的数量比较
select c.sid,a.cname from customers a, corder b,sellers c,goodss d
where a.cid=b.ocid and c.sid=d.gsid and d.gid=b.ogid
group by c.sid,a.cname
having count(*)=(
select count(*) from sellers where
sid='10001'
) ; ( 问题? )
找出选修了潘涛老师所带的所有课程的学生,显示学号,姓名
select sname, a.sid from student a, sc b, course c, teacher d
where a.sid=b.sid and b.cid=c.cid and c.tid=d.tid and tname='潘涛'
group by a.sid,sname
having count(*) 【in >=all】 (
select count(*) from teacher,course
where teacher.tid=course.tid and tname='潘涛' group by course.tid
);
上述语句错在什么地方举例说明?
select a.sid,sname,count(*),d.tid,d.tname
from student a, sc b, course c, teacher d
where a.sid=b.sid and b.cid=c.cid
and c.tid=d.tid and d.tname='潘涛'
group by a.sid, d.tid,sname,d.tname
为上述语句出一个题目( )
select a.sid,sname
from student a, sc b, course c, teacher d
where a.sid=b.sid and b.cid=c.cid
and c.tid=d.tid and d.tname='潘涛'
group by a.sid, d.tid,sname
having count(*)=(2 ) and ( d.tid=1 or d.tid=3)
为上述语句出一个题目( )
参考一
select a.sid,sname
from student a, sc b, course c, teacher d
where a.sid=b.sid and b.cid=c.cid
and c.tid=d.tid and d.tname='潘涛'
group by a.sid, d.tid,sname
having count(*)=(
select count(*) from course where tid=d.tid );
参考二
select sid,sname from
(
select sid,b.tid,count(*) num
from sc a, course b,teacher c
where a.cid=b.cid and b.tid=c.tid and tname='潘涛'
group by sid,b.tid;
)x,
student y,
( select a.tid, count(cid) num from course a,teacher b
where tname='潘涛' and a.tid=b.tid group by a.tid )z
where x.sid=y.sid and x.tid=z.tid and x.num=z.num;
参考三
select a.sid ,a.sname,b.tid ,ms, count(c.cid)
from student a,teacher b,course c , sc d ,
( select count(cid) ms,a.tid from teacher a,course b
where a.tid=b.tid and tname='潘涛' group by a.tid
) e
where a.sid=d.sid and b.tid=c.tid and c.cid=d.cid and b.tid=e.tid
and c.tid=e.tid
group by a.sid,a.sname,b.tid ,ms
having count(c.cid)=ms ;
找出吃遍所有商家所有菜的人,显示编号,姓名(外卖系统)
左连接和右连接
SQL>create table joina(a int ,b int);
SQL>create table joinb(a int ,b int);
SQL>insert into joina values(1,1);
SQL>insert into joinb values(2,2);
SQL>select * from joina left join joinb on joina.a=joinb.a;
SQL> select * from joina right join joinb on joina.a=joinb.a;
SQL>insert into joina values(2,2);
SQL>insert into joinb values(3,3);
SQL>select * from joina left join joinb on joina.a=joinb.a;
SQL> select * from joina right join joinb on joina.a=joinb.a;
统计所有(没有选过的也要统计)同学的总分,显示学号,姓名、总分
select * from student left join sc on student.sid=sc.sid;
select * from sc right join student on student.sid=sc.sid;
方法一
统计有选修课的
union
没有选修课;
方法二
左或右连接;
2万+

被折叠的 条评论
为什么被折叠?



