【实验目的】
    1、熟悉数据库的交互式SQL语言
    2、熟悉通过SQL语言对数据库进行操作
【实验平台】
    利用安装的SQL SERVER2008及其交互工具来操作SQL语言。

【实验内容】

    对于【实验1】的学生课程数据库的3个基本表

Student(Sno, sname, age, sex)

SC(sno, cno, grade)

Course(cno, cname, teacher)

给3个基本表insert输入记录,再用update、delete更新其中的数据(如果表中已有记录,该操作可忽略)。

建表及输入数据语句:
create table Student( Sno char(12) primary key, Sname char(8) unique, Sage
smallint, Ssex char(2),) create table course( Cno char(6) primary key, Cname
char(20) not null, Teacher char(8)) create table SC( Sno char(12), Cno char(6),
Grade smallint, primary key(sno,cno), foreign key(sno) references student(sno),
foreign key(cno) references course(cno)) insert into
Student(Sno,Sname,Ssex,Sage) values (201215121,'李勇','男','20'),
(201215122,'刘晨','女','19'), (201215123,'王敏','女','18'),
(201215125,'张立','男','19'); insert into Course(Cno,Cname,Teacher) values
(1,'数据库','LIU'), (2,'数学','LI'), (3,'信息系统','WANG'), (4,'操作系统','LIU'),
(5,'数据结构','ZHANG'), (6,'数据处理','LIU'), (7,'PASCAL语言','ZHU');
一、试用SQL的查询语句表达下列查询:

    1.检索LIU老师所授的课程号和课程名
select Cno,Cname from course where Teacher='LIU';
   2.检索年龄大于20岁的男学生的学号和姓名
select Sno,Sname from Student where Ssex='男' and Sage>=20;
3.检索学号为201215122学生所学课程的课程名与任课教师名(用in和exists引导的嵌套查询分别实现该题)
select Cname,Teacher /*检索号同学所选修的课程名和任课教师名*/ from course,SC where
Sno='201215122' and course.Cno=SC.Cno;
4.检索至少选修LIU老师所授课程中一门课程的女学生姓名(用连接查询、in和exists引导的嵌套查询分别实现该题)

方法一:
select sname /*检索至少选修liu老师所授课程中一门课程的女同学姓名*/ from Student,course,SC where
Student.Sno=SC.Sno and course.Cno=SC.Cno and Ssex='女' and Teacher='LIU';
 方法二: 
select sname /*检索至少选修liu老师所授课程中一门课程的女同学姓名*/ from Student where ssex='女' and
exists(select * from SC,course where SC.Sno=Student.Sno and course.Cno=SC.Cno
and Teacher='LIU')

5.检索李勇同学没有选修的课程的课程号(用not exists引导的嵌套查询、集合查询分别实现该题)

方法一:
select Cno /*检索李勇同学没有选修的课程号*/ from course where Cno<>all (select Cno from
Student,SC where SC.Sno=Student.Sno and Sname='李勇');
方法二: 
select cno /*检索李勇同学没有选修的课程号*/ from course where not exists(select * from
Student,SC where course.cno=sc.cno and Student.Sno=sc.sno and Sname='李勇')
6.检索至少选修两门课程的学生学号
select Sno /*检索至少选修了两门课的学生学号*/ from SC group by Sno having COUNT(*)>=2;
7.检索全部学生都选修的课程的课程号与课程名
select cno,Cname /*检索全部学生都选修的课程的课程名与课程号*/ from Course where Cno in(select Cno
from SC group by Cno having COUNT(*)=(select COUNT(*) from student))

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信