所有答案都验证过3遍,符合题意且结果正确,如有疑问可留言
select s_no,class_no,s_name,s_sex,s_birthday from Student
或
select * from Student
select s_no,s_name from Student
select s_no,s_name,s_birthday from Student where s_sex='男'
select s_no,s_name,s_sex,s_birthday from Student where s_sex='女' and s_birthday<'1980-01-01'
select s_no,s_name,s_birthday from Student Where s_sex='男' and s_name like '李%'
select s_no,s_name from Student where s_name like '%一%'
select t_no,t_name,t_title from Teacher where t_title <> '讲师'
select s_no from Choice where score IS NULL
select s_no,score from Choice where score<60 or score IS NULL order by score DESC
select course_no,course_name from Course where course_no in('01001','02001','02003')
-- 这里需要注意是使用双%,具体原因我也不清楚,总之用双%结果就没错
select t_no,t_name,t_birthday from Teacher Where t_birthday like '%1970%'
select course_no,COUNT(course_no) AS 选课人数 from Choice GROUP BY course_no
-- 注意,这里原表中并没有教授2门以上的教师,可自己加一位教授3门课的教师上去
select t_no from Teaching GROUP BY t_no HAVING COUNT(t_no)>2
select AVG(score) AS 平均分,MIN(score) AS 最低分数,MAX(score) AS 最高分数
from Choice where course_no='01001'
select t_name,t_birthday from Teacher where t_title='讲师' and t_birthday>'1960-12-31' order by ASC
select s.s_no,s_name,course_no,score
from Student s left JOIN Choice ch ON s.s_no=ch.s_no
order by s_no
select s_name,course_name,score into new_table
from Student s LEFT JOIN Choice ch ON s.s_no=ch.s_no
LEFT JOIN Course c ON ch.course_no=c.course_no
-- 结果存在new_table 表中,查询结果
select * from new_table
select s.s_no,s_name,ch.course_no,course_name,score
from Student s LEFT JOIN Class c
ON s.class_no=c.class_no
LEFT JOIN Choice ch ON s.s_no=ch.s_no
LEFT JOIN Course co ON ch.course_no=co.course_no
where class_name='计算机99-1'
select s.s_no,s_name,sum(co.course_score) as total_score
from Student as s
left join Choice as c on c.s_no=s.s_no
left join Course as co on co.course_no=c.course_no
and c.score>='60'
group by s.s_no,s.s_name
select s.s_no,s_name,AVG(score) AS average_score,COUNT(score) AS choice_num
from Student s LEFT JOIN Choice ch ON s.s_no=ch.s_no
GROUP BY s.s_no,s_name
select s.s_no,s_name,ch.course_no,course_name
from Student s inner JOIN Choice ch ON s.s_no=ch.s_no
inner JOIN Course c ON ch.course_no=c.course_no
where score IS NULL
select s.s_no,s_name,co.course_no,course_name,course_score
from Student s inner JOIN Choice ch ON s.s_no=ch.s_no
inner JOIN Course co ON ch.course_no=co.course_no
where score < 60
select s_name,score
from Student s,Choice ch
where s.s_no=ch.s_no AND
ch.course_no=ANY(
select c.course_no from Course c where course_name='程序设计语言')
select s.s_no,s_name,class_name,co.course_no,course_name,score
from Student s left JOIN Choice ch ON s.s_no=ch.s_no
left JOIN Class c ON s.class_no=c.class_no
left JOIN Course co ON ch.course_no=co.course_no
where class_dept='计算机系'
select t_name,course_name
from Teacher t left JOIN Teaching ti ON t.t_no=ti.t_no
left JOIN Course co ON ti.course_no=co.course_no
select t_name,count(course_no) AS course_number
from Teacher t left JOIN Teaching ti ON t.t_no=ti.t_no
group by t.t_no,t_name
select s_name from Student
where class_no=(
select class_no from Student
where s_name='李建国'
) AND s_name<>'李建国'
select s_name from Student s
where NOT EXISTS (
select * from Course co inner JOIN Choice ch ON co.course_no=ch.course_no
where course_name='计算机基础' AND s.s_no=ch.s_no
)
select t_name from Teacher t
inner JOIN Teaching ti ON t.t_no=ti.t_no
inner JOIN Course co ON ti.course_no=co.course_no
where course_name='数据库原理与应用'
UNION
select t_name from Teacher t
inner JOIN Teaching ti ON t.t_no=ti.t_no
inner JOIN Course co ON ti.course_no=co.course_no
where course_name='数据结构'
本题需要自己插入一位教授所有课程的老师
insert into Teacher
values('000011','测试员','男','1970-2-3','讲师')
还需要在任课情况表Teaching添加任课信息
insert into Teaching values
('01001','000011'),
('01002','000011'),
('01003','000011'),
('02001','000011'),
('02002','000011'),
('02003','000011')
做法一:使用双重NOT EXISTS
select t_name from Teacher t
where NOT EXISTS (
select * from Course co
where NOT EXISTS (
select * from Teaching ti
where t.t_no=ti.t_no
AND co.course_no=ti.course_no
)
)
做法二:使用group by + count。注意,count函数只能用在having中而不能在where中
select t_name from Teacher t,Teaching ti
where t.t_no=ti.t_no
group by t_name
having COUNT(ti.course_no)=(
select COUNT(co.course_no) from Course co
)
CREATE PROCEDURE Insert_student
(
@s_no CHAR(10),
@s_class CHAR(6),
@s_name VARCHAR(10),
@s_sex CHAR(2),
@s_birthday datetime
)
AS
INSERT INTO Student(s_no,class_no,s_name,s_sex,s_birthday)
VALUES (@s_no,@s_class,@s_name,@s_sex,@s_birthday)
EXEC Insert_student @s_no='000001',@s_class='xx0002',@s_name='测试1',
@s_sex='男',@s_birthday='1980-10-03'
CREATE PROCEDURE Insert_course
(
@course_no char(5),
@course_name char(20),
@course_score numeric(6,2)=2
)
AS
INSERT INTO Course(course_no,course_name,course_score)
VALUES(@course_no,@course_name,@course_score)
EXEC Insert_course @course_no='03001',@course_name='测试课程1',@course_score=6
EXEC Insert_course @course_no='03002',@course_name='测试课程2'
select * from Course
-- 查询结果为,测试课程1的学分为6,而测试课程2的学分为2
GO
CREATE PROCEDURE query_student
(
@s_no CHAR(10)
)
AS
SELECT s_name,class_no,s_sex,s_birthday
from Student where s_no=@s_no
GO
CREATE TRIGGER Display_trigger
ON Student AFTER INSERT
AS SELECT * FROM Student
GO
EXEC Insert_student @s_no='000002',@s_name='触发器',@s_class='xx0002',
@s_sex='男',@s_birthday='1979-07-30'