ORACLE: Constraint to restrict number of issues an article may be in
-
12-11-2019 - |
Вопрос
Stuck on this question in a revision paper. I've left the other relations out as they're not relevant to the question.
Consider the following relation: Article(arID, title, journal, issue, year, startpage, endpage)
How would I go about making a constraint that makes sure no more than 5 articles may be published in one particular issue?
Would I do something like:
CREATE ASSERTION ArticlesInIssue
CHECK(
(SELECT COUNT(Issue) FROM Article) <= 5);
I think this wouldn't work fully as it wouldn't be counting for the specific issue, right? So would I need a where clause, or am I going in the totally wrong direction here? Thanks, Mo.
Решение
write a trigger that fires upon insert. You can write SQL in the trigger to query the table about-to-be-inserted and do that check. Your code will need to explicitly rollback the insert.
I hope you have an index on the issue column!
Другие советы
One possible solution is to add an articleNo NUMBER(1) NOT NULL
column and the constraints:
UNIQUE (issue, articleNo)
and
CHECK (articleNo BETWEEN 1 AND 5)