I am trying to make a table in Oracle which is governed by several integrity constraints. The table consists of a hypothetical student database. One of the rules of the database is that for a student to be classified as a junior, they must have completed between 55 and 84 credit hours (inclusive).
I need to create an integrity constraint to enforce this rule but am not entirely sure how to go about doing it. I have a feeling that a CHECK constraint would be useful in this situation.
So far I have…
CONSTRAINT IC4 CHECK (hours >=55 AND hours <= 84),
This code is valid, however it does not determine if the student record is a junior.
The set up of my table is…
CREATE TABLE Students ( id INTEGER,
name CHAR(10) NOT NULL,
classification CHAR(10) NOT NULL,
hours INTEGER,
gpa NUMBER(3,2) NOT NULL,
mentor INTEGER);
So if we try to insert…
INSERT INTO Students VALUES (50, 'Kim', 'junior', 34, 3.5, 40);
…the integrity constraint would be violated because the record is trying to be stored as a ‘junior’ but the student has only completed 34 hours.
How would I go about writing a constraint which would enforce these rules?
You need to use the magic word
andagain:I suspect you’ll want to have other classifications too, and validate their ranges. Use parentheses and OR to do this. (And use BETWEEN to define the ranges for clarity)….
Make sure you have a complete set of ranges.