当两个桌子包含相同的记录时,抛出例外
我试图实现两个表的呼叫的ER模式:
create table enrolled_in (
student_id number(6) not null,
course_crn number(6) not null,
constraint registers_for_pk primary key (student_id, course_crn),
constraint registers_for_s_fk foreign key (student_id) references student(id),
constraint registers_for_c_fk foreign key (course_crn) references course(crn)
);
create table registered_for (
student_id number(6) not null,
course_crn number(6) not null,
constraint preregisters_for_pk primary key (student_id, course_crn),
constraint preregisters_for_s_fk foreign key (student_id) references student(id),
constraint preregisters_for_c_fk foreign key (course_crn) references course(crn)
);
我必须创建某种完整性约束,以防止两个表中存在的相同记录。也就是说,学生不得注册他们参加的课程。
我的假设是我需要使用触发器,因为一个简单的约束无法调用UDF(根据支票约束文档)。我还假设如果找到重复记录,我应该抛出例外。到目前为止,这是我到目前为止的:
create or replace trigger reg_duplicate_check
BEFORE insert on registers_for or enrolled_in
declare
duplicate_registration exception;
begin
select * into reg from registers_for;
FOR r in reg
LOOP
-- check for a duplicate record in enrolled_in?
END LOOP;
exception
when duplicate_registration then
raise_application_error(-20004,'Duplicate record (enrolled_in and registers_for)');
end;
我问两个问题:
我在正确的轨道上/这是最好的方法吗?
如果是这样,那么我该如何检查重复记录?如果不是,那么实施这种约束的最佳方法是什么?
我无法将其合并到一个表中。
The ER schema I am trying to implement calls for two tables:
create table enrolled_in (
student_id number(6) not null,
course_crn number(6) not null,
constraint registers_for_pk primary key (student_id, course_crn),
constraint registers_for_s_fk foreign key (student_id) references student(id),
constraint registers_for_c_fk foreign key (course_crn) references course(crn)
);
create table registered_for (
student_id number(6) not null,
course_crn number(6) not null,
constraint preregisters_for_pk primary key (student_id, course_crn),
constraint preregisters_for_s_fk foreign key (student_id) references student(id),
constraint preregisters_for_c_fk foreign key (course_crn) references course(crn)
);
I must create some sort of integrity constraint that prevents the same record from existing in both tables. That is, a student may not register for a course they are enrolled in.
My assumption is that I will need to use a trigger because a simple constraint cannot call a UDF (according to the CHECK constraints documentation). I am also assuming that I should throw an exception if a duplicate record is found. Here is what I have so far:
create or replace trigger reg_duplicate_check
BEFORE insert on registers_for or enrolled_in
declare
duplicate_registration exception;
begin
select * into reg from registers_for;
FOR r in reg
LOOP
-- check for a duplicate record in enrolled_in?
END LOOP;
exception
when duplicate_registration then
raise_application_error(-20004,'Duplicate record (enrolled_in and registers_for)');
end;
I am asking two questions:
Am I on the right track/is this the best way to do this?
If so, then how can I go about checking for the duplicate records? If not, what is the best way to implement such a constraint?
I am not able to combine these into one table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对您的问题的回答:
what_is_it
(行#4),然后检查约束(第11行),其中e表示“注册”,而r表示“注册”。如果您愿意,那也可能是外键。类似的事情:
这样做,其主要键将防止使用相同的[Student_ID,Course_crn]组合的两行。
为了学习目的(如您必须使用两个表),然后使用两个触发器 - 一个用于每个桌子。他们非常相似:
My answers to your questions:
what_is_it
(line #4) and check constraint (line #11) where E represents "enrolled in", while R represents "registered for". That could also be a foreign key, if you want.Something like this:
Doing so, its primary key would prevent two rows with the same [student_id, course_crn] combination.
For studying purposes (as you must use two tables), then use two triggers as well - one for each table. They are very much alike:
创建仅用于执行约束的第三个表:
然后创建一个触发器来从父表中插入带有匹配行的子表中插入/删除/更新行,然后让
primary
/unique unique
父表上的密钥执行约束。或者,只需创建子表,然后使用
实现的视图
与Union
一起使用。像:Create a third table that exists only to enforce the constraint:
Then create a triggers to insert/delete/update rows from the child tables with matching rows in the parent table and let the
PRIMARY
/UNIQUE
keys on the parent table enforce the constraints.Or, just create the child tables and then use a
MATERIALIZED VIEW
toUNION
them together. Something like: