mysql学生表选课表课程表_mysql查询(学生表、课程表、选课表)
************************************************************
為sc表中的sno和cno創建外鍵
alter table sc add foreign key(sno) references student(sno);
alter table sc add foreign key(cno) references course(cno);
************************************************************
a) 插入如下學生記錄(學號:95030,姓名:李莉,年齡:18)
insert into student(sno,sname,sage) values('95030','李莉',18);
b) 插入如下選課記錄(95030,1)
insert int sc(sno,cno) values('95030','001');
c) 計算機系學生年齡改成20
update student set sage = 20 where sdept = 'cs';
d) 數學系所有學生成績改成0
update sc set grade = 0 where cno = '002';
e) 把低于總平均成績的女同學成績提高5分
f) 修改2號課程的成績,若成績小于75分提高5%,成績大于75時提高
4%(兩個語句實現,注意順序)
update sc set grade=grade+grade*0.05 where cno='002' and grade<75;
update sc set grade=grade+grade*0.04 where cno='002' and grade>75;
g) 刪除95030學生信息
delete from student where sno = '95030';
h) 刪除SC表中無成績的記錄
delete from sc where grade is null;
i) 刪除張娜的選課記錄
delete from sc where sno = (select sno from student where sname = '張娜');
j) 刪除數學系所有學生選課記錄
delete from sc where cno = '002';
k) 刪除不及格的學生選課記錄
delete from sc where grade < 60;
l) 查詢每一門課程成績都大于等于80分的學生學號、姓名和性別,把值送往另一個已經存在的基本表STU(SNO,SNAME,SSEX)中
insert into stu(sno,sname,ssex)
select student.sno,student.sname,student.ssex
from student,sc
where student.sno=sc.sno and sc.grade>=80;
m) 把所有學生學號和課程號連接追加到新表中
insert into STUsc(sno,cno)
select student.sno,sc.cno from student,sc
where student.sno = sc.sno;
n) 所有學生年齡增1
update student set sage = sage+1;
o) 統計3門以上課程不及格的學生把
相應的學生姓名、系別追加到另外一個表中
insert into stu(sname,sdept) --插入表中
select sname,sdept from student,sc --選擇出列名
where --條件
student.sno=(select sno from sc
where grade<60 group by sno having count(*)>3);
查詢每個學生及其選課情況;
select sno,cno from sc;
將STUDENT,SC進行右連接
查詢有不及格的學生姓名和所在系
select a.sname,a.sdept from student a right join sc b on a.sno = b.sno;
查詢所有成績為優秀(大于90分)的學生姓名
select a.grade,b.sname ,a.sno
from sc a right join student b on a.grade > 90 and a.sno = b.sno;
+-------+--------+-------+
| grade | sname | sno |
+-------+--------+-------+
| NULL | 麗麗 | NULL |
| NULL | 趙海 | NULL |
| 97 | 劉晨 | 08005 |
| 93 | 劉丹丹 | 08006 |
| NULL | 劉立 | NULL |
| NULL | 王江 | NULL |
| NULL | 高曉 | NULL |
| 100 | 張麗 | 08010 |
| NULL | NULL | NULL |
+-------+--------+-------+
select a.grade,b.sname ,a.sno from sc a
join student b on a.grade > 90 and a.sno = b.sno;
+-------+--------+-------+
| grade | sname | sno |
+-------+--------+-------+
| 97 | 劉晨 | 08005 |
| 93 | 劉丹丹 | 08006 |
| 100 | 張麗 | 08010 |
+-------+--------+-------+
查詢既選修了2號課程又選修了3號課程的
學生姓名、學號;
select sname,sno from student where sno in(
select sno from sc
where cno = '003'
and
sno in(select sno from sc where cno = '006'));
----或者
select a.sname,a.sno from student a join sc b where a.sno = b.sno and cno = '003' and b.sno in(select sno from sc where cno = '006');
查詢和劉晨同一年齡的學生
select sno ,sname from student where sage = (select sage from student where sname = '劉晨');
選修了課程名為“數據庫”的學生姓名和年齡
select a.sname,a.sage from student a join sc b on a.sno = b.sno and b.cno = '001
查詢其他系比IS系任一學生年齡小的學生名單
select sname,sage from student where sage < any
(select sage from student where sdept = 'is');
查詢其他系中比IS系所有學生年齡都小的學生名單
select sname,sage from student where sage < all
(select sage from student where sdept = 'is');
查詢選修了全部課程的學生姓名
select sname,sno
from student where sno in(select sno from sc);
查詢計算機系學生及其性別是男的學生
select *from student where sdept = 'cs' and ssex = '男';
查詢選修課程1的學生集合和選修2號課程學生集合的差集
select sno from sc where cno = '001' and sno not
in(select sno from sc where cno = '002');
查詢李麗同學不學的課程的課程號
select cno from course where cno not in(select a.cno from sc a join student b on a.sno = b.sno and b.sname = '李麗');
查詢選修了3號課程的學生平均年齡
select avg(sage) from student where sno in(select sno from sc where cno = '003');
求每門課程學生的平均成績
select avg(grade) from sc group by cno;
統計每門課程的學生選修人數(超過3人的才統計)。要求輸出課程號和選修人數,結果按人數降序排列,若人數相同,按課程號升序排列
select cno ,count(*) from sc group by cno having count(sno) > 3;
查詢學號比劉晨大,而年齡比他小的學生姓名。
select sname from student where
sno > (select sno from student where sname = '劉晨')
and sage < (select sage from student where sname = '劉晨');
求年齡大于女同學平均年齡的男同學姓名和年齡
select sname,sage from student where ssex = '男' and
sage > (select avg(sage) from student where ssex = '女');
求年齡大于所有女同學年齡的男同學姓名和年齡
select sname,sage from student where ssex = '男' and
sage > all (select sage from student where ssex = '女');
查詢95001和95002兩個學生都選修的課程的信息
select *from sc where sno in ('95001','95002');
-----------------------------------------------------
alter table student add test varchar(20); -
alter table student drop test; -
-----------------------------------------------------
為學生表按學號建立唯一索引
mysql> create UNIQUE INDEX stusno ON student(sno);
Query OK, 10 rows affected (0.61 sec)
Records: 10 Duplicates: 0 Warnings: 0
為course表按課程號升序(默認)建立唯一索引
mysql> create UNIQUE INDEX coucno ON course(cno);
Query OK, 7 rows affected (0.38 sec)
Records: 7 Duplicates: 0 Warnings: 0
為sc表按學號升序和課程號降序建立唯一索引
mysql> create UNIQUE INDEX scno ON sc(sno asc,cno desc);
Query OK, 11 rows affected (0.34 sec)
Records: 11 Duplicates: 0 Warnings: 0
//刪除索引
drop index scno on sc;
//插入學生信息
insert into student (sno,sname,sage) values('95030',';李莉',18);
a) 查詢全體學生的學號和姓名
select sno,sname from student;
b) 查詢全體學生的詳細記錄
select *from student;
c) 查詢所有選修過課程的學生學號
select distinct sno from sc ;
d) 查詢考試有不及格的學生學號
select sno from sc where grade < 60;
e) 查詢不是信息系(IS)、計算機系(CS)的學生性別、年齡、系別
select sname,ssex,sdept from student where sdept not in('is','cs');
f) 查詢選修了4號課的學生學號和成績,結果按成績降序排列
select sno,grade from sc where cno = '004' order by grade desc;
g) 查詢每個課程號和相應的選課人數
select cno,count(cno) as cnonumed from sc group by cno;
h) 查詢計算機系(CS)的學生姓名、年齡、系別
select sname,sage,sdept from student where sdept = 'cs';
i) 查詢年齡18-20歲的學生學號、姓名、系別、年齡;
select sname,sage,sdept from student where sage between 18 and 20;
j) 查詢姓劉的學生情況
select *from student where sname like '劉%';
k) 查詢既選修3號課程,又選修6號課程的學生學號和成績
select sno ,grade from sc
where cno = '003'
and
sno in(select sno from sc where cno = '006');
l) 查詢學生的姓名和出生年份(今年2003年)
select sname,sbirthday from student;
m) 查詢沒有成績的學生學號和課程號
select sno,cno from sc where grade is null;
n) 查詢總成績大于200分的學生學號
select sno,sum(grade) from sc
group by sno having sum(grade) > 200;
o) 查詢每門課程不及格學生人數
select cno,count(sno) from sc
where grade < 90
group by cno;
p) 查詢不及格課程超過3門的學生學號
select cno,count(sno) from sc
where grade < 60
group by cno
having count(sno) > 3;
q) 查詢年齡在10到19歲之間的學生信息
select *from student
where sage between 10 and 19;
r) 查詢全體學生情況,按所在系升序排列,同一個系的學生按年齡降序排列
select *from student order by sdept asc,sage desc;
s) 查詢選了1號課程的學生平均成績
select cno,avg(grade) from sc where cno = '001' group by cno;
+-----+------------+
| cno | avg(grade) |
+-----+------------+
| 001 | 92.6667 |
+-----+------------+
1 row in set (0.00 sec)
mysql> select cno,avg(grade) from sc group by cno having cno = '001';
+-----+------------+
| cno | avg(grade) |
+-----+------------+
| 001 | 92.6667 |
+-----+------------+
t) 查詢選了3號課程的學生的最高分
select cno, max(grade)
from sc where cno = '003' group by cno;
u) 查詢每個同學的總成績
select sno,sum(grade)
from sc
group by sno;
總結
以上是生活随笔為你收集整理的mysql学生表选课表课程表_mysql查询(学生表、课程表、选课表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AE特效 动态拼贴实现及分析
- 下一篇: 西电杨宗凯调研计算机学院,西安电子科技大