Foreign Key to Composite Primary Key - sql

I have a problem with foreign keys.
The master table is:
Course (courseName, courseId)
and PK is courseId
The second table is:
Section (sectionId, courseId)
and PK is (sectionId, courseID), FK is courseID reference Course(courseId)
And the problem table is:
GradeReport (studentId, sectionId, courseId, grade)
and PK is (studentID, sectionID, courseID)
The FK's are
courseId reference Course(courseID)
sectionId reference Section(sectionID)
(no permisson for (courseId, sectionId) reference Section(courseId, sectionID))
The first FK is OK! But the second one gives me an error:
ORA-02270: no matching unique or primary key for this column-list
How can I solve this problem?

Since the primary key of Section is (sectionId, courseID), the foreign key constraint of GradeReport must be (sectionID, courseID).
In fact you will not need a foreign key constraint on GradeReport referencing Course, since the courseID will be in this foreign key constraint.

I don't understand the question fully, but if I had these tables my foreign keys would be:
Section (which I think is OK for you already)
FOREIGN KEY ..name.. REFERENCES Course(CourseID)
GradeReport (which is where I think you have the problem)
FOREIGN KEY ..name.. REFERENCES Section(SectionID, CourseID)
You don't need a separate Foreign Key for GradeReport.CourseID because it's already part of the SectionID, CourseID foreign key to Section, and Section.CourseID can be trusted because of the Section table's foreign key with Course.

Related

How do I create a table whereby the Foreign Keys (2) are part of a Composite Primary Key in another table (Oracle)? [duplicate]

This question already has an answer here:
ORA-02270: no matching unique or primary key for this column-list
(1 answer)
Closed 2 years ago.
I have two tables.
Section (SectionNo, Semester, CourseID) <- I made all 3 attributes a Composite PK
Registration (Registration-ID, StudentID, SectionNo, Semester) <- Registration_ID is a Surrogate Key and the remaining 3 attributes are supposed to be my Foreign Keys.
I have trouble creating the registration specifically when I want to reference the attributes 'SectionNo' and 'Semester' which are FK in the Registration table.
My code is as such :
CREATE TABLE REGISTRATION(
Registration_ID Int NOT NULL,
StudentID Int NOT NULL,
SectionNo Int NOT NULL,
Semester Varchar(25) NOT NULL,
CONSTRAINT REGISTRATION_PK PRIMARY KEY(Registration_ID),
CONSTRAINT REGISTRATION_FK1 FOREIGN KEY(StudentID)
REFERENCES STUDENT(StudentID) ON DELETE CASCADE,
CONSTRAINT REGISTRATION_FK2 FOREIGN KEY(SectionNo,Semester)
REFERENCES SECTION(SectionNo,Semester) ON DELETE CASCADE
);
However, when I run it on Oracle, the error I get is :
ORA-02270: no matching unique or primary key for this column-list
I have already created the Section table as :
CREATE TABLE SECTION(
SectionNo Int NOT NULL,
SEMESTER Varchar(25) NOT NULL,
CourseID Varchar(25) NOT NULL,
CONSTRAINT SECTION_PK PRIMARY KEY(SectionNo,Semester, CourseID),
CONSTRAINT SECTION_FK1 FOREIGN KEY(CourseID)
REFERENCES Course(CourseID)
ON DELETE CASCADE
);
There is no error creating the Section Table. Hence, I'm confused as to how am I to create the Registration table.
Would appreciate some help from you all.
PRIMARY KEY(SectionNo, Semester, CourseID)
FOREIGN KEY(SectionNo, Semester)
The problem is that you have incorrect column list in REGISTRATION_FK2 foreign key constraint. The column list of the foreign key must exactly match with the column list of the primary key it is referencing to.
Primary Key SECTION_PK has column list SectionNo, Semester, CourseID, therefore, your foreign key must have the same columns.

Oracle error building table from 2 foreign keys

I'm in the process of building a database and have already successfully created 2 primary key tables however when I try to bring them in as 2 foreign keys to another table I am running into a
"CLIENTID" invalid identifier
Unsure how to resolve as not the best at this.
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
);
You are missing the columns on which your primary key and foreign keys are created; you need something like the following, to be edited with the right type for your columns:
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
ClientId number, -- missing
CourseID number, -- missing
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
)

Database SQL Error: ORA-02270: no matching unique or primary key for this column-list

I am creating a simple database and I keep encountering this error, ORA-02270: no matching unique or primary key for this column-list. I have been trying to solve this for more than a day and I don't understand what I'm doing wrong. I have looked at other posts regarding this error but don't understand what I'm doing wrong here.
The tables that create without any issue are the TEAMS, PLAYERS, and GAMES tables. When I try to create the TEAM_GAME_STATS or the PLAYER_GAME_STATS tables, I get this error. I noticed that if I remove the foreign key constraints from both of these tables, then there is no issue creating them; but I'm thinking that might cause issues with the data? I would appreciate if someone could look over my code and inform me of what I might be doing wrong or point me in the right direction. I have simplified the tables to only the primary/foreign keys, so there isn't an excessive amount of code.
CREATE TABLE teams (
team VARCHAR2(50),
CONSTRAINT teams_pk PRIMARY KEY (team)
);
CREATE TABLE players (
player_id NUMBER,
team VARCHAR2(50),
CONSTRAINT players_pk PRIMARY KEY (player_id),
CONSTRAINT players_fk_team FOREIGN KEY (team)
REFERENCES teams (team)
);
CREATE TABLE games (
game_number NUMBER,
player_id NUMBER,
team VARCHAR2(50),
CONSTRAINT games_pk PRIMARY KEY (game_number, player_id, team),
CONSTRAINT games_fk_player_id FOREIGN KEY (player_id)
REFERENCES players (player_id),
CONSTRAINT games_fk_team FOREIGN KEY (team)
REFERENCES teams (team)
);
CREATE TABLE team_game_stats (
game_number NUMBER,
team VARCHAR2(50),
CONSTRAINT team_game_stats_pk PRIMARY KEY (game_number, team),
CONSTRAINT team_game_stats_fk_game_number FOREIGN KEY (game_number)
REFERENCES games (game_number),
CONSTRAINT team_game_stats_fk_team FOREIGN KEY (team)
REFERENCES games (team),
);
CREATE TABLE player_game_stats (
game_number NUMBER,
player_id NUMBER,
CONSTRAINT player_game_stats_pk PRIMARY KEY (game_number, player_id),
CONSTRAINT player_game_stats_fk_game_number FOREIGN KEY (game_number)
REFERENCES games (game_number),
CONSTRAINT player_game_stats_fk_player_id FOREIGN KEY (player_id)
REFERENCES games (player_id),
);
CREATE INDEX players_team_idx
ON players (team);
CREATE INDEX games_player_id_idx
ON games (player_id);
CREATE INDEX games_team_idx
ON games (team);
CREATE INDEX team_game_stats_game_number_idx
ON team_game_stats (game_number);
CREATE INDEX team_game_stats_team_idx
ON team_game_stats (team);
CREATE INDEX player_game_stats_game_number_idx
ON player_game_stats (game_number);
CREATE INDEX player_game_stats_player_id_idx
ON player_game_stats (player_id);
A foreign key is a reference to another table's primary key.
Oracle SQL: Receiving 'no matching unique or primary key' error and don't know why
Change
CONSTRAINT games_pk PRIMARY KEY (game_number, player_id, team)
to
CONSTRAINT games_pk PRIMARY KEY (game_number, team)
Also change
CONSTRAINT team_game_stats_fk_game_number FOREIGN KEY (game_number)
REFERENCES games (game_number),
CONSTRAINT team_game_stats_fk_team FOREIGN KEY (team)
REFERENCES games (team),
to be
CONSTRAINT team_game_stats_game_fk FOREIGN KEY (game_number, team)
REFERENCES games (game_number, team)
and do something similar to player_game_stats.
You may need to fiddle around with the keys of each table. The important thing is the foreign key in a table should contain same columns as the primary key of the table it references.

Can a foreign key reference another foreign key

Is it possible to have a foreign key that references another foreign key in a different table, or can it only reference primary and unique keys?
A foreign key can reference any field defined as unique. If that unique field is itself defined as a foreign key, it makes no difference. A foreign key is just to enforce referential integrity. Making a field a foreign key doesn't change the field itself in any way. If it is a unique field, it can also be the target of another FK.
For example:
create table Table1(
PK int identity primary key,
...
);
create table Table2( -- 1-1 relationship with Table1
PKFK int primary key,
...,
constraint FK_Table2_1 foreign key( PKFK ) references Table1( PK )
);
create table Table3( -- relates to Table2
PKFKFK int primary key,
...,
constraint FK_Table3_2 foreign key( PKFKFK ) references Table2( PKFK )
);
I know of no DBMS where this is not the case. And I agree with Horse, there is nothing wrong with the practice.
Is it possible to have a foreign key that references another foreign key in a different table
Yes. In fact contrary to accepted answer, the referenced FK column doesn't even have to be unique! - at least in MySQL. see https://www.db-fiddle.com/f/6RUEP43vYVkyK2sxQQpBfj/0 for a demo of the same.
which brings up the question that if the FK is not unique in the parent table, then who is the parent row? The purpose of FKs is to establish parent-child relationship.

SQL Script Foreign Key Error

I am getting the error
"Number of referencing columns in foreign key differs from number of referenced columns, table 'StudentGrade'" when trying to execute the following SQL script
CREATE TABLE StudentGrade
(
StudentID INT NOT NULL
CONSTRAINT FK_SG_StudentID FOREIGN KEY (StudentID)
REFERENCES Student(StudentID),
ClassID VARCHAR (6) NOT NULL
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
REFERENCES Class(ClassID),
CourseID VARCHAR (64) NOT NULL
CONSTRAINT FK_Course FOREIGN KEY (CourseID)
REFERENCES Course(CourseID),
FacultyID INT NOT NULL
CONSTRAINT FK_Faculty FOREIGN KEY (FacultyID)
REFERENCES Faculty(FacultyID),
Grade NUMERIC NULL,
CONSTRAINT PK_StudentID PRIMARY KEY (StudentID, ClassID, CourseID, FacultyID)
)
I know that there is something I am doing wrong with the foreign keys though I can't find anywhere where it explains how to use foreign keys and composite keys together. Any help would be greatly appreciated. Thank you so much!
change your second foreign key from
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
to
CONSTRAINT FK_Class FOREIGN KEY (ClassID)
In FK_Class you are referencing the columns StudentGrade.ClassID and StudentGrade.CourseID into a single one Class.ClassID and this cannot work.
Your FK_Course already refers CourseID, so you can simply delete CourseID from FK_Class as I said above.
Edit
Add CourseID to your definition of FK_Class as
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
REFERENCES Class(ClassID, CourseID)