--求出所教课程成绩中平均成绩大于分的教师的教师号、教师姓名、性别、职称及电话号码
select t.TeaNo,t.TeaName,t.Sex,t.Title,t.Telephone from Teacher t where t.TeaNo in(
select SelectCourse.TeaNo from SelectCourse
group by SelectCourse.TeaNo
having AVG(SelectCourse.Score)>70 )
--求出每个教师所教课程的平均成绩、最高成绩、最低成绩
select t.TeaNo,t.TeaName, AVG(sc.Score) as 平均成绩, MAX(sc.Score) as 最高成绩, MIN(sc.Score) as 最低成绩 from SelectCourse sc join Teacher t on sc.TeaNo=t.TeaNo
group by sc.CourseNo,t.TeaNo,t.TeaName
--求出不同职称的教师人数
select t.Title as 职称,count(t.Title) as 人数 from Teacher t group by t.Title
--求选修””课的学生学号、姓名、性别和成绩,这些学生的成绩比选修“”号课程的最低成绩高。 /*
select Student.StuNo,Score
from selectCourse inner join student on selectcourse.StuNo=student.Stuno where CourseNo='0001' and Score>any( select score
from selectCourse where CourseNo='0004' ) */
select s.StuNo,s.StuName,s.Sex,sc.Score
from Student s join SelectCourse sc on s.StuNo=sc.StuNo
where sc.CourseNo='0001' and sc.Score>(
select MIN(Score) from SelectCourse
where CourseNo='0004' )
--*列出每个老师所教课程情况,并统计出每个老师所教课程的平均分(注意:每个老师可能教多门课程,必须分别显示每门课程的平均分) /*
[ COMPUTE
{ { AVG | COUNT | MAX | MIN | STDEV | STDEVP | VAR | VARP | SUM }
( expression ) } [ ,...n ] [ BY expression [ ,...n ] ] ] */
select * from
selectcourse order by teano,courseNo
compute avg(score) by teano,CourseNo
--查询出年出生的教师与年出生的学生的姓名、性别、出生日期和电话号码。 select s.StuName,s.Sex,s.Birthday,s.Telephone from Student s
where s.Birthday like '83%' union
select t.TeaName,t.Sex,t.Birthday,t.Telephone from Teacher t
where t.Birthday like '66%'
--求选修了??号课程且成绩小于“王宏远”同学的学号及成绩 select StuNo,Score from SelectCourse where CourseNo='0003' and Score<(
Select sc.Score
from SelectCourse sc join Student s on sc.StuNo=s.StuNo
where s.stuName='王宏远'
and sc.CourseNo='0003' )
--给Department表中插入新开设系记录,系名:艺术系,系主任:刘颖,电话号码:-38256744,地址:教学楼
insert into
Department(DepartNo,DepartName,Dean,Telephone,Address) values('06','艺术系','刘颖','020-38256744','教学楼')
--修改Department表中计算机系的系主任为“李开明” update Department set Dean='李开明'
where DepartName='计算机系'
--求出所教课程成绩中平均成绩大于分的教师的教师号、教师姓名、性别、职称及电话号码。
select t.TeaNo,t.TeaName,t.Sex,t.Title,t.Telephone from Teacher t where t.TeaNo in( select TeaNo
from SelectCourse
group by TeaNo,CourseNo having AVG(Score)>80 )
--求选修””课的学生学号、姓名、性别和成绩,要求这些学生的成绩比选修“”号课程的最低成绩高。 /*
select Student.StuNo,Student.StuName,Student.Sex,Score from selectCourse inner join student on selectcourse.StuNo=student.Stuno where CourseNo='0001' and Score>any( select score
from selectCourse where CourseNo='0004' ) */
select s.StuNo,s.StuName,s.Sex,sc.Score from Student s join SelectCourse sc on s.StuNo=sc.StuNo
where sc.CourseNo='0001' and sc.Score>(
Select MIN(Score) from SelectCourse where CourseNo='0004' ) /*
1.使用企业管理器为CollegeMIS数据库中的Class表中ClassName字段
创建一个唯一约束,
约束名为IX_ClassName。并验证。
2.使用企业管理器删除为Class表中ClassName字段创建唯一约束, 然后在查询分析器中使用SQL语句重新创建,并验证。
语法:alter table table_name add constraint constraint_name
unique(column1,column2...) */
--增加唯一约束(唯一索引)
alter table Class add constraint IX_ClassName unique(ClassName) --删除唯一约束(唯一索引)
alter table Class drop constraint IX_ClassName /*
1.使用企业管理器为CollegeMIS数据库中的Class表中ClassName字段
创建一个检查约束,
约束名为CK_ClassName。检查条件是ClassName字段值的前两个字符必
须是数字,
且必须在-99或-11之间,并验证。
2.使用企业管理器删除检查约束CK_ClassName,然后在查询分析器中使
用SQL语句重新创建,并验证。
语法:alter table table_name add constraint constraint_name
check(expression) */ /*
alter table Class add constraint CK_ClassName check(
(left(ClassName,2)>='90' and left(ClassName,2)<='99') or
(left(ClassName,2)>='00' and left(ClassName,2)<='11') ) */
--增加check约束
alter table Class add constraint CK_ClassName check(
left(ClassName,2) like '9[0-9]' or
left(ClassName,2) like '[0-1]1' )
--删除check约束
alter table Class drop constraint CK_ClassName /*
1.使用企业管理器为CollegeMIS数据库中的Teacher表中Title字段创
建一个默认约束,
约束名为DF_Teacher_Title,默认值为”助教”。并验证。
2.使用企业管理器删除检查约束DF_Teacher_Title,然后在查询分析器
中使用SQL语句重新创建,并验证。
语法:alter table table_name add constraint constraint_name
default value for column */
--增加default约束
alter table Teacher add constraint DF_Teacher_Title default '助教' for Title --删除default约束
alter table Teacher drop constraint DF_Teacher_Title
--查询?王平?老师教过的所有学生姓名、成绩
select s.StuName,sc.CourseNo,sc.TeaNo,sc.Score from Student s join SelectCourse sc on s.StuNo=sc.StuNo where sc.TeaNo =( select TeaNo from Teacher )
--列出所有选修了”操作系统”课程的学生的学号、姓名 select s.StuNo,s.StuName,sc.CourseNo from Student s join SelectCourse sc on s.StuNo=sc.StuNo where sc.CourseNo =( select CourseNo from Course )
where CourseName='操作系统' where TeaName='王平'