有两个表:
课程( CourseNo,SectionNo,Year,Semester ,容量)(粗体表示一个键)。
注册( CourseNo,Year,Semester,SectionNo,StudentNo ,年级)
我如何使用SQL来确保每个班级的注册人数少于班级人数?
我知道如何单独完成这些部分,但是每个班级注册的学生的count()与该课程的能力之间的比较是我迷失的地方。我不知道如何确保它与正确的值相比。
由于
编辑:我正在使用SQL Server 2008来创建这个断言,但我想知道如何在SQL Server中创建一个触发器(因为它不支持断言),如何做常规SQL答案 0 :(得分:0)
select courseno, capacity, count(*) as enrollments
from classes c
join enrollments e on e.courseno = c.courseno
group by c.courseno , capacity
答案 1 :(得分:0)
select c.CourseNo, c.SectionNo, c.[Year], c.Semester, Capacity, count(*) as Enrolled
from classes as c
join enrollments as e
on c.CourseNo = e.CourseNo
and c.SectionNo = e.SectionNo
and c.[Year] = e.[Year]
and c.Semester = e.Semester
group by c.CourseNo, c.SectionNo, c.[Year], c.Semester, Capacity
having count(StudentNo) > Capacity