我有 2 张桌子:
User(LoginID,Password,Email,UserType) -- Primary key(LoginID)
Class(CourseCode, Semester,Year,ClassTime,ProID) -- Primary key(CourseCode, Semester,Year) -- Foreign key(ProID)
UserType 可以是教师、助教 (TA) 或学生。现在我不知道在哪里可以添加TA属性,以便一个班级有一个TA和一个Teacher。我正在考虑将 TAID 作为外键放入 User 表中,这样可以:
Class(CourseCode, Semester,Year,ClassTime,ProID,TAID)
但我不确定它是否正确。因为当我想获得教授特定课程的老师和助教的名字时,它不起作用。我试过下面的查询:
SELECT dbo.[User].Name
FROM dbo.Class INNER JOIN
dbo.[User] ON dbo.Class.ProID = dbo.[User].LoginID AND dbo.Class.TAID = dbo.[User].LoginID
WHERE CrsCode=@CrsCode and Semester=@Semester and Year=@Year
问题:有人对这个问题有任何想法吗?提前致谢。
编辑: 我写道:
alter proc SelectFacultyNameByID
@CrsCode nvarchar(5),
@Semester nvarchar(20),
@Year int
as
SELECT
u1.Name as Teacher,
u2.Name as Assistant
FROM
dbo.Class c
JOIN [User] u1
ON c.ProID = u1.LoginID
JOIN [User] u2
ON c.TAID = u2.LoginID
WHERE
c.CrsCode = @CrsCode
AND c.Semester = @Semester
AND c.Year = @Year
但它不起作用!