题目数据:
create database 学生课程系统;
use 学生课程系统;
create table Student
(Sno int primary key,
Sname nchar(10) ,
Ssex nchar(2),
Sage int,
Sdept nvarchar(30));
create table Course
(Cno int primary key,
Cname nvarchar(30),
Cpno int,
Ccredit int);
create table SC
(Sno int,
Cno int,
Grade int,
primary key(Sno,Cno));
insert into Student
values(201215121,‘李勇’,‘男’,20,‘CS’),
(201215122,‘刘晨’,‘女’,19,‘CS’),
(201215123,‘王敏’,‘女’,18,‘MA’),
(201215124,‘张立’,‘男’,19,‘IS’);
insert into Course
values(1,‘数据库’,5,4),(2,‘数学’,NULL,2),
(3,‘信息系统’,1,4),(4,‘操作系统’,6,3),
(5,‘数据结构’,7,4),(6,‘数据处理’,NULL,2),
(7,‘PASCAL’,6,4);
insert into SC
values(201215121,1,92),(201215121,2,85),
(201215121,3,88),(201215122,2,90),(201215122,3,80);
1.创建表my_Student,使该表具有Student表的相同结构。
create table my_student like student;
2.复制Student表中‘CS’系的学生信息,使这些数据进入到my_Student表。
insert into my_student select * from student where sdept=‘CS’;
3.向数据表my_Student表插入(201215122,‘李楠’,‘男’,19,‘MA’),同时避免冲突。①冲突更新;②冲突替换。
1:insert into my_student (sno,sname,ssex,sage,sdept)
values(201215122,‘李楠’,‘男’,19,’'MA)
on duplicate key
update sname=‘李楠’,ssex=‘男’,sage=‘19’,sdept=‘MA’;
2.replace into my_student(sno,sname,ssex,sage,sdept)
values(201215122,‘李楠’,‘男’,19,’'MA);
4.查询被选修的课程号。
select distinct cno from sc;
5.查询学生的成绩信息,按学号由小到大排序,学号相同时按成绩由高到低排序。
select sno,cno,grade from sc order by sno,grade desc;
6.查询1号课程的成绩的前3名。
select grade from sc where cno=1 order by grade desc limit 3;
7.更改2号课程的成绩第一名成绩为100分。
update sc set grade = 100 where cno =2 order by grade desc limit 1;
8.查询男生的人数。
select count(*) from student
where ssex=‘男’
group by ssex;
9.统计男女生的各自人数。
select ssex,count(*) from student group by ssex;
10.统计男女生的人数,加入回溯统计总人数。
select ssex,count(*) from student group by ssex with rollup;
11.统计各个系(Sdept)男女生的人数。
select sdept,ssex,count(*) from student group by sdept,ssex;
12.统计每个同学的学号、选课数、平均成绩和最高成绩。
select sno,count(*),avg(grade),max(grade) from sc group by sno ;
13.统计选课人数大于等于2人的课程号。
select cno from sc
group by cno
having count(*)>=2;
14.查询选修了2号课程的成绩最高分,平均分。
select max(grade),avg(grade) from sc
where cno=2;
15.查询选修了1号课程的’学生人数’(使用别名)。
select count(*) 学生人数 from sc
where cno=1;