Is it allowed in SQL to have a foreign key that is not a primary key but a (multiple) unique key? To be specific:
[Table 1]
CREATE TABLE Session_Record(
c_id CHAR (3) NOT NULL
REFERENCES Club_Record(c_id),
sess_id NUMBER (4) NOT NULL CHECK (sess_id >0)
room CHAR (4) NOT NULL,
UNIQUE(c_id, sess_id)
);
[Table 2]
CREATE TABLE SessionDuration__Record(
c_id CHAR (3) NOT NULL
REFERENCES Club_Record(c_id),
sess_id NUMBER (4) NOT NULL CHECK (sess_id >0)
REFERENCES Session_Record(sess_id),
day CHAR(9) NOT NULL,
duration NUMBER(3) NOT NULL,
UNIQUE(c_id, sess_id)
);
“c_id” is a primary key in the table Club_Record. But sess_id is not a primary key. It is a multiple candidate key in combination with c_id. Is this form of declaration allowed in SQL?. Because Oracle 10g is returning: “no matching unique or primary key for this column-list” for the (sess_id) foreign key statement.
Help is much appreciated!
Your SQL is invalid in a few different ways (missing a comma after the definition of
Session_Record.sess_id; usingdayas an identifier without quoting it), but sticking just to the problem that you explicitly asked about — I’m guessing what you really want is this:That is, you want to be sure that each
(SessionDuration__Record.c_id, SessionDuration__Record.sess_id)corresponds to an existent(Session_Record.c_id, Session_Record.sess_id).