--(1)利用SQL语句进行简单查询和连接查询操作。
--1.列出所有course的课程号、课程名和学分。
select courseno,coursename,credithour
from course;
--2.查询所有蒙古族学生的学号、姓名和所在学院。
select studentNo,studentName,institute
from Student,Class
where student.classNo=Class.classNo;
--3.在score表中显示平均成绩都高于85分的学生学号、课程号和成绩。
select *
from Score
where studentNo in(
select studentno
from Score
group by studentNo
having AVG(score)>85);
--4. 查询选修课称号为001或005且平均成绩大于等于75分学生的学号、课程号和成绩。
select *
from Score
where courseNo in ('001','005')
and studentNo in(
select studentNo
from Score
group by studentNo
having AVG(score)>=75);
--5.查询信息管理学院年出生的所有男生的信息。
select *
from Student,Class
where institute='信息管理学院'
and sex='男' and YEAR(birthday)=1991;
--6.查询所有学生的年龄。
select studentno,YEAR(GETDATE())-YEAR(birthday) as age
from Student
--7.查询所有姓王或姓黄的学生的姓名、生日和所在班级名称。
select studentName,birthday,classname
from Student,Class
where Student.classNo=Class.classNo
and (studentName like '王%' or studentName like '黄%');
--8.查询先修课程为”数据结构”的所有课程。
select a.courseno,a.coursename
from Course a,Course b
where a.priorCourse=b.courseNo
and b.courseName='数据结构';
--9.查询信息管理学院非汉族同学的学号、姓名、性别及民族。
select studentNo,studentName,sex,nation
from Student,Class
where Student.classNo=Class.classNo
and institute='信息管理学院'
and nation !='汉族';
--10.查选修了“操作系统”的学生学号、成绩和姓名。
select student.studentNo,studentname,Score
from Student,course,Score
where Student.studentNo=score.studentNo
and Course.courseNo=score.courseNo
and courseName='操作系统';
--11.查至少选修了一门其直接先修课编号为004的课程的学生学号和姓名。
select score.studentno,studentname
from Student,Course,Score
where Student.studentNo=score.studentNo
and score.courseNo=Course.courseNo
and priorCourse='004';
--12.查至少选修了学号为0800001的学生所选课程的学生学号和姓名。
select score.studentno,studentname
from Score,student
where score.studentNo=Student.studentNo
and courseNo>=all(
select courseno
from Score
where studentNo='0800001')
--13.查询出生日期在年以后的学生的学号、姓名、籍贯和年龄。
select studentno,studentName,native,YEAR(GETDATE())-YEAR(birthday) as age
from Student
where YEAR(birthday)>1991;
--14.在student表中查询学生的学号、姓名和平均成绩,并按照平均成绩的降序排列。
select score.studentno,studentName,AVG(score) as avgscore
from Student,Score
where Student.studentNo=score.studentNo
group by score.studentNo,studentName
order by avgscore desc;
--15.查了选修了以“C语言程序设计”为其直接先修课的课程的学生学号、课程号和成绩。
select student.studentNo,score.courseNo,score
from student,score,Course
where student.studentNo=score.studentNo
and score.courseNo=Course.courseNo
and Course.courseNo=(
select a.courseNo
from Course a,Course b
where a.priorCourse=b.courseNo
and b.courseName='C语言程序设计');
--16.在score表中查询平均成绩大于80的学生的学号、课程号和成绩,并先按照课程号的升序、再按照成绩的降序排列。
select *
from Score
where studentNo in (
select studentNo
from Score
group by studentNo
having AVG(score)>80
)
order by courseNo,score desc;
--17.查信息管理学院学生选课情况,显示学生姓名、课程名和成绩。
select studentName,courseName,score
from Student,Course,Score,class
where Student.studentNo=score.studentNo
and Course.courseNo=score.courseNo
and Student.classNo=Class.classNo
and institute='信息管理学院';
--18.统计student表中的男女学生的人数。
select sex,count(*) as 人数
from Student
增加字段的sql语句
group by sex
--19.查询成绩最高分的学生的学号、课程号和相应成绩。
select studentNo,courseno,score
from Score
where score =(
select MAX(score)
from Score);
--20.查询选课少于门的学生的学号及其选课的门数。
select studentno,COUNT(*) as 门数
from Score
group by studentno
having count(*)<3
--(2) 利用SQL语句进行表的管理等操作。
--1.给class表中的classname字段添加唯一约束。
alter table class add constraint uniqueClassname unique(classname);
--2.给Student表中的sex字段添加检查约束。
alter table student add constraint chkSex check (sex in ('男','女'));
--3.建立学生会名单表StudentUnion,包含三个字段:编号SID(char(2)),姓名studnetName (varchar(20),职务post(char(15))。
create table studentUnion(
sid char(2) primary key,
studentname varchar(20),
post char(15));
--4.为StudentUnion表插入两条数据(,’李小勇’,’学生会主席’), (,’吴敏’,’文艺部长’)
insert into studentUnion
values(1,'李小勇','学生会主席');
insert into studentUnion
values(2,'吴敏','文艺部长');
--5.在上题中建立的表StudentUnion中增加一个字段“电话(char(8))”,为电话列添加检查约束,要求每个新加入或修改的电话号码为位数字。
alter table studentUnion add telephone char(8);
--6.为StudentUnion表增加一个字段“地址(varchar(50))”。
alter table studentUnion add address varchar(50);
--7.在表StudentUnion中给地址字段添加一个默认约束,默认值为“独墅湖高教区仁爱路号”。
alter table studentUnion add constraint defaultAdd default '独墅湖高教区仁爱路号' for address;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论