数据库上机实验学习记录最新Day1117

create table student
(
	Sno char(9) primary key,
	Sname char(20) unique,
	Ssex char(2),
	Sage smallint,
	Sdept char(20)

);

//查询
select Sno,Sname,Ssex,Sage,Sdept from student where Sname='wy';

//插入
insert into student values('3','dh','v','12','jg');
insert into student values('4','fg','n','45','sp');

insert into student values('5','hg','n','25','dp');
insert into student values('6','fg','v','55','ep');
insert into student values('7','rg','n','66','tp');
//删除
delete Student where Sname='hg';


//更新
update student set Sname='zl' where Sname='fg' and Ssex='n';

Day1028

题目要求:

一、在数据库pubs中,创建如下三张表:

1、学生表(student) (

 学号(sno)普通编码定长字符类型,长度7,主码,

 姓名(sname)普通编码定长字符类型,长度8,非空,

 性别(ssex)统一编码定长字符类型,长度1,只能是‘男’或‘女’,

 年龄(sage)微整型,1-150之间,

 所在系(sdept)统一编码可变长字符类型,长度10

create table student
(
	Sno char(7) primary key,
	Sname char(8) not null,
	Ssex nchar(1) check(Ssex='M' or Ssex='F'),
	Sage smallint check(Sage>=1 and Sage<=150),
	Sdept nchar(10)
);


 

 

2、课程表(course) (

 课程号(cno)普通编码定长字符类型,长度6,主码,

 课程名(cname)统一编码定长字符类型,长度10,非空,

 先修课程号(cpno)普通编码定长字符类型,长度6,可以空,外码(对应表course,

 学分(credit)微整型,1-10之间,

 学期(semester)微整型,1-10之间,

 

create table course
(
	Cno char(6) primary key,
	Cname nchar(10) not null,
	Cpno char(6) null ,
	credit smallint check(credit>=1 and credit<=10),
	semester smallint check(semester>=1 and semester<=10),
	foreign key(Cpno) references course(Cno)

);


 

 

3、修课表(sc(

 学号(sno)普通编码定长字符类型,长度7,主码,外码(对应表student,

 课程号(cno)普通编码定长字符类型,长度6,主码,外码(对应表course,

 成绩(grade)微整型,0-100之间,

 修课类别(xklb)普通编码定长字符类型,长度4

 

create table sc
(
	Sno char(7),
	Cno char(6),
	grade smallint check(grade>=0 and grade<=100),	
	xklb char(4),
	primary key (Sno,Cno),
	foreign key(Sno) references student(Sno),
	foreign key(Cno) references course(Cno)
);


 

 

二、

1、将下表数据输入到表student中;

Sno

sname

Ssex

Sage

Sdept

9512101

李勇

49

计算机系

9512102

刘晨

20

计算机系

9512103

王敏

20

计算机系

9521101

张立

22

信息系

9521102

吴宾

21

信息系

9521103

张海

20

信息系

9531101

钱小平

18

数学系

9531102

王大力

19

数学系

 

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9512101,'李勇' ,'M',49,'计算机系')

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9512102,'刘晨' ,'M',20,'计算机系')

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9512103,'王敏' ,'F',20,'计算机系')

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9521101,'张立' ,'M',22,'信息系')

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9521102,'吴宾' ,'F',21,'信息系')

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9521103,'张海' ,'M',20,'信息系')

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9531101,'钱小平' ,'F',18,'数学系')

insert into student(Sno,Sname,Ssex,Sage,Sdept)
values(9531102,'王大力' ,'M',19,'数学系')


 

 

2、将下表数据输入到表course中;

cno

cname

cpno

Credit

Semester

c01

计算机文化学

 

3

1

c02

VB

c01

2

3

c03

计算机网络

 

4

7

c04

数据库基础

c01

6

6

c05

高等数学

 

8

2

c06

数据结构

c05

5

4

c07

网络应用

c03

4

8

c08

Web数据库

c04

4

7

 

insert into course(Cno,Cname,Cpno,credit,semester)
values('c01','计算机文化学' ,null,3,1)

insert into course(Cno,Cname,Cpno,credit,semester)
values('c02','VB' ,'c01',2,3)

insert into course(Cno,Cname,Cpno,credit,semester)
values('c03','计算机网络' ,null,4,7)

insert into course(Cno,Cname,Cpno,credit,semester)
values('c04','数据库基础' ,'c01',6,6)

insert into course(Cno,Cname,Cpno,credit,semester)
values('c05','高等数学' ,null,8,2)

insert into course(Cno,Cname,Cpno,credit,semester)
values('c06','数据结构' ,'c05',5,4)

insert into course(Cno,Cname,Cpno,credit,semester)
values('c07','网络应用' ,'c03',4,8)

insert into course(Cno,Cname,Cpno,credit,semester)
values('c08','Web数据库' ,'c04',4,7)


3、将下表数据输入到表sc中;

.

cno

grade

XKLB

9512101

c01

90

必修

9512101

c02

86

选修

9512101

c06

 

必修

9512102

c02

78

选修

9512102

c04

66

必修

9521102

c01

82

选修

9521102

c02

75

选修

9521102

c04

92

必修

9521102

c05

50

必修

9521103

c02

68

选修

9521103

c06

 

必修

9531101

c01

80

选修

9531101

c05

95

必修

9531102

c05

85

必修


insert into sc(Sno,Cno,grade,xklb)
values(9512101,'c01',90,'必修')

insert into sc(Sno,Cno,grade,xklb)
values(9512101,'c02',86,'选修')

insert into sc(Sno,Cno,grade,xklb)
values(9512101,'c06',null,'必修')

insert into sc(Sno,Cno,grade,xklb)
values(9512102,'c02',78,'选修')

insert into sc(Sno,Cno,grade,xklb)
values(9512102,'c04',66,'必修')

insert into sc(Sno,Cno,grade,xklb)
values(9521102,'c01',82,'选修')

insert into sc(Sno,Cno,grade,xklb)
values(9521102,'c02',75,'选修')

insert into sc(Sno,Cno,grade,xklb)
values(9521102,'c04',92,'必修')

insert into sc(Sno,Cno,grade,xklb)
values(9521102,'c05',50,'必修')
insert into sc(Sno,Cno,grade,xklb)
values(9521103,'c02',68,'选修')

insert into sc(Sno,Cno,grade,xklb)
values(9521103,'c06',null,'必修')

insert into sc(Sno,Cno,grade,xklb)
values(9531101,'c01',80,'选修')

insert into sc(Sno,Cno,grade,xklb)
values(9531101,'c05',95,'必修')

insert into sc(Sno,Cno,grade,xklb)
values(9531102,'c05',85,'必修')

Day1104更新

五、仔细阅读,完成如下查询

计算机系哪些同学选了课程

1)、第一种思考方法

A、  计算机系同学的学号  select sno from student where sdept=计算机系

B、  这些学号中选了课的学号 select distinct sno from sc where sno in (A)

C、  整合起来的语句就是:select distinct sno from sc where sno in

               ( select sno from student where sdept=计算机系 )

2)、第二种思考方法

A、所有选了课同学的学号  select sno from sc

B、这些学号中是计算机系的同学 select sno from student where sdept=计算机系 and  sno in (A)

C、整合起来的语句就是:select sno from student where sdept=计算机系 and  sno in (select sno from sc )

 

1、  查询没有选课的同学;

2、  查询选修了‘VB’课程的同学;

3、  查询‘信息系’选修了‘VB’课程的同学;

4、  查询‘信息系’的同学选修了哪些课程;

5、  查询哪些系的同学选修了‘高等数学’;

6、  查询‘女’同学选修了哪些课程;

7、  查询男生哪些同学选修了‘VB’;

8、  查询选修了‘数据库基础’的学生的姓名、所在系;

9、  选修了第6学期开始的课程的学生的学号、姓名和所在系;

10、             查询男生所修的课程的课程名。


 

select distinct sno from sc where sno in 
               ( select sno from student where sdept='计算机系' )

select sno from student where sdept='计算机系' and  sno in (select sno from sc )

/*1、	查询没有选课的同学;*/
select Sno from student where Sno not in(select Sno from sc)

/*2、	查询选修了‘VB’课程的同学;*/
select Sno from student where Sno in (select Sno from sc where Cno in (select Cno from course where Cname='VB'))

/*3、	查询‘信息系’选修了‘VB’课程的同学;*/
select Sno from student where Sdept='信息系'
and Sno in(select Sno from student where Sno in (select Sno from sc where Cno in (select Cno from course where Cname='VB')))

/*4、	查询‘信息系’的同学选修了哪些课程;*/
select Cno from sc where Sno in (select Sno from student where Sdept='信息系')

/*5、	查询哪些系的同学选修了‘高等数学’;*/

select Sdept from student where Sno in (select Sno from  sc where Cno in(select Cno from course where Cname='高等数学') )

/*6、	查询‘女’同学选修了哪些课程;*/
select Cname from course where Cno in(Select Cno from sc where Sno in(select Sno from student where Ssex='F' ))

/*7、	查询男生哪些同学选修了‘VB’;*/
select Sname from student where Ssex='M' and Sno in (select Sno from sc where Cno in(select Cno from course where Cname='VB'))

/*8、	查询选修了‘数据库基础’的学生的姓名、所在系;*/
select Sname,Sdept from student where Sno in(select Sno from sc where Cno in(select Cno from course where Cname='数据库基础'))

/*9、	选修了第6学期开始的课程的学生的学号、姓名和所在系;*/
select Sno,Sname,Sdept from student where Sno in (select Sno from sc where Cno in(select Cno from course where semester=6))

/*10、	查询男生所修的课程的课程名。*/
select Cname from course where Cno in(Select Cno from sc where Sno in(select Sno from student where Ssex='M' ))


四、完成如下查询
(1) 查询计算机系没有选课的学生的姓名和年龄。
(2) 统计‘VB’课程的考试最高分、最低分和平均分。
(3) 统计‘数据库’课程的选课人数。
(4) 统计计算机系学生’vb’课程考试的最高分、最低分、平均分。 
(5) 统计每个系学生’vb’课程考试的最高分、最低分、平均分,列出系名和三个分数。

/*1.查询计算机系没有选课的学生的姓名和年龄。*/
select Sname,Sage from student
 where Sdept='计算机系' and Sno not in(select Sno from sc)

/*2.统计‘VB’课程的考试最高分、最低分和平均分。*/
select max(grade) as Max,min(grade)as Min,avg(grade)as average 
from sc where Cno in(select Cno from course where Cname='VB')

/*(3)	统计‘数据库’课程的选课人数。*/
select count(distinct Sno) from sc 
where Cno in(select Cno from course where Cname='VB') 

/*(4)	统计计算机系学生’vb’课程考试的最高分、最低分、平均分。  */
select max(grade) as Max,min(grade)as Min,avg(grade)as average
from sc where Cno in(select Cno from course where Cname='VB')
and Sno in(select Sno from student where Sdept='计算机系')

/*(5)	统计每个系学生’vb’课程考试的最高分、最低分、平均分,列出系名和三个分数。*/
select max(sc.grade) as Max,min(sc.grade)as Min,avg(sc.grade)as average,student.Sdept
from sc,student where Cno in(select Cno from course where Cname='VB')
group by student.Sdept


 

*/

/*
(6)	查询每个学生的修课总学分,并进行如下处理:如果总学分高于10分,则显示‘好学生’;
	如果总学分在6到10分间,则显示‘一般学生’;如果总学分低于6分,则显示‘不好学生’。
*/


select Sno,
case
when sum(credit)>10 then '好学生'
when sum(credit) between 6 and 10 then '一般学生'
else '不好学生'
end 
级别
from sc 
join course on
sc.Cno=course.Cno
Group by Sno;

/*
7)	统计每个系每个学生的修课门数和平均成绩,如果修课门数大于3并且平均成绩高于90的显示‘优秀’;
	如果修课门数大于3并且平均成绩在80~90,则显示‘较好’;
	对于修课门数小于等于3的并且平均成绩高于90的显示‘较好’;
	如果修课门数小于等于3并且平均成绩在80~90,则显示‘一般’;
	其他情况显示为‘应努力’。
	列出系名、学号、原修课门数和平均成绩以及处理后的显示结果。
*/

select Sno,
case 
when count(Cno)>3 and avg(grade)>90 then '优秀'
when count(Cno)>3 and avg(grade) between 80 and 90 then '较好'
when count(Cno)<=3 and avg(grade)>90 then '较好'
when count(Cno)<=3 and avg(grade)between 80 and 90 then '一般'
else '应努力'
end
级别
from sc
group by Sno


SQL查询一 实验学时:2   实验类型:验证 实验要求:必修 一、实验目的 通过本实验使学生掌握单表查询并初步掌握多表查询的技能。 二、实验内容 使用实验一建立的银行贷款数据库和表,完成以下查询。 1-10题为单表查询,11-25为多表查询。 查询所有法人的法人代码、法人名称、经济性质和注册资金。 查询“B1100”银行的的银行名称和电话。 查询贷款金额在2000至4000万元之间的法人代码、银行代码、贷款日期和贷款金额。 查询2009年1月1日以后贷款且贷款期限是10年的法人代码。 查询贷款期限为5年、10年或15年的贷款信息。 查询经济性质为“私营“的所有法人的最高注册资金、最低注册资金和平均注册资金。 查询每种经济性质的法人的经济性质、最高注册资金、最低注册资金和平均注册资金。 统计每个法人的法人代码和贷款总次数,要求查询结果按贷款总次数的升序排列。 查询贷款次数超过3次的法人的平均贷款金额和贷款次数。  统计每种经济性质贷款的法人的总数和其平均贷款金额,列出平均贷款金额前三名的经济性质、法人总数和平均贷款金额。  查询贷款期限为5年、10年或15年的法人名称、银行名称、贷款日期、贷款金额和贷款期限。  查询经济性质为“国营”的法人在“上海”的银行贷款的信息,列出法人名称、银行名称和贷款日期。  查询与“B1100”银行在同一城市(假设银行名称的第5和第6个字符为城市名称)的其他的银行的名称。  查询哪些银行没有贷过款,列出银行号和银行名称。分别用多表连接和子查询两种方式实现。  查询贷过款的所有法人的名称,贷款银行名称,贷款日期,贷款金额,要求将查询结果放在一张新的永久表New_LoanT中,新表中的列名分别为:法人名称、银行名称、贷款日期和贷款金额。  分别查询经济性质“国营”和“私营”的法人名称,贷款银行名称,贷款日期,贷款金额,要求将这两个查询结果合并成一个结果集,并以法人名称、银行名称、贷款日期和贷款金额作为显示列名,结果按贷款日期的升序和贷款金额的降序显示。 使用实验一建立的学生数据库和表,完成以下查询 查询计算机系没有选课的学生的姓名和年龄。 统计‘VB’课程的考试最高分、最低分和平均分。 统计‘数据库’课程的选课人数。 统计计算机系学生’vb’课程考试的最高分、最低分、平均分。  统计每个系学生’vb’课程考试的最高分、最低分、平均分,列出系名和三个分数。 查询每个学生的修课总学分,并进行如下处理:如果总学分高于10分,则显示‘好学生’;如果总学分在6到10分间,则显示‘一般学生’;如果总学分低于6分,则显示‘不好学生’。 统计每个系每个学生的修课门数和平均成绩,如果修课门数大于3并且平均成绩高于90的显示‘优秀’;如果修课门数大于3并且平均成绩在80~90,则显示‘较好’;对于修课门数小于等于3的并且平均成绩高于90的显示‘较好’;如果修课门数小于等于3并且平均成绩在80~90,则显示‘一般’;其他情况显示为‘应努力’。列出系名、学号、原修课门数和平均成绩以及处理后的显示结果。 查询计算机系学生考试成绩最低的两个成绩所对应的学生的姓名、课程名和成绩。 列出没有选课的学生的学号、姓名和所在系。 三、思考题 1、创建表: 教师表(   tid 普通编码定长字符型,长度为10,主关键字   tname 普通编码定长字符型,长度为10,非空 zc 普通编码定长字符型,长度为6) 设此表数据如下: tid Tname zc T1 A 教授 T2 B 副教授 T3 C 教授 编写SQL语句,使查询的结果显示为如下形式: 教师号 教师名 教授 副教授 T1 A 教授 T2 B 副教授 T3 C 教授 2、创建表: 教师表(   tid 普通编码定长字符型,长度为10,主码   tname 普通编码定长字符型,长度为10,非空 zc 普通编码定长字符型,长度为6 Salary 工作,整型) 设此表数据如下: tid Tname zc Salary T1 A 教授 5000 T2 B 副教授 4000 T3 C 教授 5000 编写SQL语句,使查询的结果显示为如下形式: 教师号 教师名 教授工资 副教授工资 T1 A 5000 Null T2 B Null 4000 T3 C 5000 Null 四、实验报告 将实验结果反映在实验报告中,并对实验中遇到的问题及解决方案进行整理、分析总结,提出实验结论或自己的看法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值