我很难创建一个触发器来更新注册学生的数量。这是我的桌子的设置方式。第一个代码块是由我的导师提供的。每当一个学生注册或被删除时,register_count 表都需要用适当的学生注册数量来更新。
Create table Registered
(
ssn numeric,
code char(10),
year int,
semester char(10),
foreign key (ssn) references Student(ssn),
foreign key (code) references Course(code),
primary key (ssn,code,year,semester)
);
create table Register_Count
(
code char(10),
year int,
semester char(10),
count int default 0,
primary key (code, year, semester)
);
到目前为止,这是我对触发器的尝试。我不断收到“无法绑定多部分标识符错误”。
CREATE TRIGGER updateCount ON registered
AFTER UPDATE, DELETE, INSERT
AS
UPDATE register_count SET
code = (select code from inserted),
year = (select year from inserted),
semester = (select semester from inserted),
count = (select count(*) from registered)
WHERE Register_Count.code = inserted.code
GO