SQL Script Foreign Key Error - sql

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)

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.

How to solve ORA-00907: missing right parenthesis error?

I am unable to find an error in this code, it shows ORA-00907: missing right parenthesis for both. I'm doing this on Oracle live SQL.
CREATE table Final_chart
(
T_id int FOREIGN KEY REFERENCES Train(T_id),
User_id varchar(10) FOREIGN KEY REFERENCES Passenger(User_id),
Seat_id int FOREIGN KEY REFERENCES Train_Seats(Seat_id),
CONSTRAINT PNR PRIMARY KEY (T_id,User_id,Seat_id)
)
CREATE table Train_seats
(
T_id int FOREIGN KEY REFERENCES Train(T_id),
Seat_id int PRIMARY KEY,
Waiting int NOT NULL,
Available int NOT NULL,
Booked_seat int NOT NULL
)
Working example for one table. And please use varchar2 instead on varchar in oracle.
CREATE table Final_chart
(T_id integer,
User_id varchar2(10),
Seat_id integer,
CONSTRAINT t2_fk FOREIGN KEY (T_id) REFERENCES Train(T_id),
CONSTRAINT t1_fk FOREIGN KEY (User_id) REFERENCES Passenger(User_id),
CONSTRAINT t3_fk FOREIGN KEY (Seat_id) REFERENCES Train_Seats(Seat_id),
CONSTRAINT Pkr PRIMARY KEY (T_id, User_id, Seat_id)
)
The problem is your reference to the foreign key. this is the way you should do it
CONSTRAINT FK_Train FOREIGN KEY (T_id) REFERENCES Train(T_id)

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)
)

Foreign Key to Composite Primary Key

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.

Invalid FK and PK reference

Unable to create table as oracle shows ' no matching unique or primary key for this column-list' when I did label the primary key reference for the required table.
First table created successfully:
CREATE TABLE TEST
(
TESTno VARCHAR2(6) NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TEST_Date DATE NOT NULL,
ACTUAL DATE,
PREDICTED Date,
CONSTRAINT TESTPKs PRIMARY KEY (TEST_Date, TESTno, ExamNo),
CONSTRAINT TTESTNO_Fk FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
Here's the table i want to create and gives me error:
CREATE TABLE Assignment
(
TEST_Date DATE NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TestNo VARCHAR2(6) NOT NULL,
Type VARCHAR2(20),
Hours_Spent Decimal(4,2),
CONSTRAINT ASSIGNPKS PRIMARY KEY (TEST_Date, TestNo , ExamNo),
CONSTRAINT ASSIGNTESTDATE_FK FOREIGN KEY (TEST_Date) REFERENCES TEST(TEST_Date) ON
DELETE CASCADE,
CONSTRAINT ASSIGNTESTNO_FK FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
May i know where's the issue that it keeps giving me no matching unique primary keys? I already tried to recreate and labelled the 'test_Date' as my primary key. But oracle can't seems to find.
Thanks
The PK you refer to is PRIMARY KEY (TEST_Date, TESTno, ExamNo) — hence the foreign key should be FOREIGN KEY (TEST_Date, TESTno, ExamNo) as well. The error you're getting is due to your attempt to refer to a part of TEST's PK.
See also http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c22integ.htm
Check the tables you are referencing in your foreign keys. Those columns must be the primary key or otherwise unique on the foreign table.