HBU-数据库第五周作业

第五周数据库作业

注意

MySQL的数据库名、表名、列名、别名大小写规则是这样的:

1、数据库名与表名是严格区分大小写的;

2、表的别名是严格区分大小写的;

3、列名与列的别名在所有的情况下均是忽略大小写的;

4、字段内容默认情况下是大小写不敏感的。

先建表,这是我们查询的预备工作

Student表

创建表

create table student(
Sno int,
Sname varchar(30),
Ssex varchar(10),
Sage int,
Sdept varchar(20));

插入数据

insert into student values("2012151121","李勇","男",20,"CS");

insert into student values("2012151122","刘晨","女",19,"CS");

insert into student values("2012151123","王敏","女",18,"MA");

insert into student values("2012151125","张立","男",19,"IS");

Course表

创建表

create table course(
cno int,
cname  varchar(30),
cpno int,
ccredit int));

插入数据

insert into Course values("1","数据库",5,4);

insert into Course values("2","数学",NULL,2);

insert into Course values("3","信息系统",1,4);

insert into Course values("4","操作系统",6,3);

insert into Course values("5","数据结构",7,4);

insert into Course values("6","数据处理",NULL,2);

insert into Course values("7","PASCAL语言",6,4);

SC表

建表

Create table Sc(
sno int,
cno int,
grade int);

插入数据

insert into SC values(201215121,1,92);

insert into SC values(201215121,2,85);

insert into SC values(201215121,3,88);

insert into SC values(201215122,2,90);

insert into SC values(201215122,3,80);

查询语句

1.查询全体学生的详细记录

select * from student;

2.查询全体学生的学号和姓名

select Sno,Sname from student;

3.查询全体学生的学号和姓名,使用列别名改变查询结果的列标题,把列名改为汉字“学生编号”“学生姓名”。(选做)

select Sno as "学生编号",Sname as "学生姓名" from student;

4.查询选修了课程的学生学号。(不去掉重复学号的和去重的都试一下)

select distinct Sno from SC;
select Sno from SC;

5.查询‘CS’系全体学生的名单

select * from student where Sdept="CS";

6.查询‘1’号课的选课情况

select * from SC where Cno=1;

7.查询男同学的学号和姓名

select Sno,Sname from student where Ssex="男";

8.查询考试成绩有不及格的课程的课程号。(不去掉重复学号的和去重的都试一下)

select Cno from sc where grade<60;
select distinct Cno from sc where grade<60;

9.查询成绩在95~99分(包括95分和99分)之间的选课记录的学号、课程号和成绩.

select * from SC where grade>=95 and grade<=99;
select * from SC where grade in (95,99);

10.查询成绩不在95~99分之间的学号、课程号和成绩。

select * from SC where grade not in (95,99);

11.查询年龄是18岁、20岁或24岁的学生的姓名和性别。(几种写法?)

select Sname,Ssex from student where Sage=18 or Sage=20 or Sage=24;
select Sname,Ssex from student where Sage in(18,20,24);

12.查询年龄既不是18岁、20岁,也不是24岁的学生的姓名和性别。

select Sname,Ssex from student where Sage not in(18,20,24);

13.查询课程名中第2个字为 "据" 字的课程的课程号、课程名和学分。

select Cno,Cname,Ccredit from course where substr(Cname,2,1)="据";

14.查询课程名为“A_ C”课程的课程号和学分

select Cno,Ccredit,Cname from course where Cname like "%A%C%";

15.查询没有先行课的课程号和课程名

select Cno,Cname from course where cpno is NULL;

16.查询缺少了成绩的学生的学号和课程号

select Sno,Cno from SC where grade is null;

17.查询男同学的学号、姓名、年龄和所在系,将查询结果按所在系的系号降序排列,同一系中的学生按年龄升序排列。

select Sno,Sname,Sage,Sdept from Student where Ssex="男" order by Sdept desc ,Sage asc;

18.查询开设的课程总门数.

select count(*) from Course;

19.查询有学生选的课程的门数

select count(distinct Cno) from SC;

20.查询全体同学的最小年龄。

select min(Sage) from student;

21.查询男同学的最小年龄。

select min(Sage) from student where Ssex="男";

22.查询‘CS’系男同学的最小年龄。

select min(Sage) from student where Sdept="CS" and Ssex="男";

23.查询‘95001’同学的选课平均成绩。

select avg(grade) from SC where Sno="95001";

24.查询‘95001’同学的选课最高成绩

select max(grade) from SC where Sno="95001";

25.查询有选课记录的同学的学号和他相应的选课门数。

select Sno,count(Sno) from sc group by Sno;

26查询‘CS’系或‘MA’系姓刘的学生的信息。

select * from student where substr(Sname,1,1)="刘" and Sdept in("CS","MA");

猜你喜欢

转载自www.cnblogs.com/h3zh1/p/12571765.html