How to have more than one foreign key? - sql

I'm having trouble adding more than 1 foreign key to my tables, I am getting the error:
"Error report -
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view"
I tried making the table without the third foreign key and it worked so I think its something to do with the last key but not really sure. (I put spaces in between the SQL to make it easier to read.)
CREATE TABLE ORDER(
ORDERID CHAR(4) NOT NULL,
HOUSETYPE CHAR(2),
CHAIR CHAR(2),
PERSON CHAR(2),
PAYDAY DATE,
MENUPRICE NUMBER(4,2),
CONSTRAINT CONFERENCESESSION_PK PRIMARY KEY(ORDERID),
CONSTRAINT CONFERENCESESSION_FK1 FOREIGN KEY(HOUSETYPE) REFERENCES
HOUSE(HOUSETYPE),
CONSTRAINT CONFERENCESESSION_FK2 FOREIGN KEY(PAYDAY) REFERENCES PERSON(PAYDAY),
CONSTRAINT CONFERENCESESSION_FK3 FOREIGN KEY(MENUPRICE) REFERENCES
MENU(MENUPRICE)
);

A foreign key must reference another table primary key, my guess is that some of you foreign keys don't reference the primary key of the other table.
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and
the table containing the candidate key is called the referenced or
parent table.
https://www.w3schools.com/sql/sql_foreignkey.asp

A foreign key must reference another table primary key so make sure you are doing that with the right name of the primary key. For example:
CONSTRAINT CONFERENCESESSION_FK2 FOREIGN KEY(PAYDAY) REFERENCES PERSON(PAYDAY)
Make sure that in table PERSON you have PRIMARY KEY with name PAYDAY.

Related

keep getting the error code no matching unique or primary key for this column-list

I am just starting out with SQL and I have been attepmting to see where the issue is with my SQL below. ive etaken out "Date_Reserved" and was allowed to create the table. However I have made sure to create a primary for "Date Reserved" and it still dosent work
create table reservation
( Booking_Number varchar(8) not null,
Room_Number number(1,50) not null,
Date_Reserved date not null,
primary key (Booking_Number, Room_Number, Date_Reserved),
foreign key (Booking_Number) references booking(Booking_Number),
foreign key (Room_Number) references room(Room_Number)
);
create table additional_extra
( Booking_Number varchar(8) not null,
Room_Number number(1,50) not null,
Extra_ID varchar(8) not null,
Date_Reserved date not null,
primary key (Booking_Number, Room_Number, Extra_ID),
foreign key (Booking_Number) references booking(Booking_Number),
foreign key (Date_Reserved) references reservation(Date_Reserved),
foreign key (Room_Number) references room(Room_Number),
foreign key (Extra_ID) references extra(Extra_ID)
);
It keeps resulting in the Error code ""
Error report -
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
The foreign key for the additional_extra table
foreign key (Date_Reserved) references reservation(Date_Reserved)
does not reference all of the columns in the reservation table
primary key (Booking_Number, Room_Number, Date_Reserved)
Foreign keys may be defined as multiple columns. However, a composite foreign key must reference a composite primary or unique key with the same number of columns and the same data types.

Oracle no matching unique or primary key for this column-list

Error starting at line : 25 in command -
CREATE TABLE Note (
Note_ID NUMBER,
Engineer_ID NUMBER,
Project_ID NUMBER,
Bug_ID NUMBER,
Bug_Name varchar2(30),
CONSTRAINT Note_PK PRIMARY KEY (Note_ID),
FOREIGN KEY (Engineer_ID) REFERENCES Engineer(Engineer_ID),
FOREIGN KEY (Project_ID) REFERENCES Project(Project_ID),
FOREIGN KEY (Bug_ID) REFERENCES Bug(Bug_ID),
FOREIGN KEY (Bug_Name) REFERENCES Bug(Bug_Name)
)
Error report -
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
Cannot seem to find any way to fix this... could someone help? Need for an assignment soon. I have some more code beforehand but I just cant seem to find any sort of problem with it :?
The error message is clear enough. For each foreign key defined in your table, the referenced table column needs to be unique or primary key.
So you want to ensure that all the following table columns do match this requirement:
Engineer(Engineer_ID)
Project(Project_ID)
Bug(Bug_ID)
Bug(Bug_Name)
Likely, the problem is with column Bug_Name; I would guess that this information is functionaly dependent of Bug_ID, that is already referenced by a foreign key: if so, then you just do not need to store it in the Note table (you can access it through the Bug_ID foreign key).

Create a table in SQL where the Composite Primary Key is also a Foreign Key

I need to create a table called LFM_Enroll in SQL that has a composite primary key of Student_ID and Section_Number. Student_ID is also a foreign key, it references Student_ID in the LFM_Student table and Section_Number is also a foreign key, it references Section_Number in the LFM_Section table. How do I write the constraints and foreign keys? I've attached an image of the tables and below is what I have done so far. After the LFM_Enroll table is created I need to update one row. I tried doing so but kept getting the below error.
: Error starting at line : 173 in command -
UPDATE LFM_Enroll
SET Student_ID = 1234567,
Section_Number = 01234
WHERE Student_ID = 900000 AND Section_Number = 4138
Error report -
ORA-02291: integrity constraint (SYSTEM.FK_LFM_ENROLL_SECTION_NUMBER) violated - parent key not found.
Tables Thanks in advance for all your help.
CREATE TABLE LFM_Enroll (
Student_ID char(7),
Section_Number char(4),
constraint PK_LFM_Enroll Primary Key (Student_ID,Section_Number),
constraint FK_LFM_Enroll_Student_ID
Foreign Key (Student_ID,Section_Number) references LFM_Student (Student_ID),
constraint FK_LFM_Enroll_Section_Number
Foreign Key (Student_ID,Section_Number) references LFM_Section (Section_Number)
);
Your foreign key constraints are not right. You are trying to map two columns {Student_ID,Section_Number} to a single column LFM_Student.Student_ID.
The number of columns in the principal key must match the number of columns in the foreign key. In other words, the key LFM_Student is one column (Student_ID), so the foreign key also needs to be a single matching column - in this case LFM_Enroll.Student_ID. Correct DDL would be:
constraint FK_LFM_Enroll_Student_ID
Foreign Key (Student_ID) references LFM_Student (Student_ID),
constraint FK_LFM_Enroll_Section_Number
Foreign Key (Section_Number) references LFM_Section (Section_Number)
I'm not quite sure why your RDBMS is allowing what you have, but it may be using the first column and simply ignoring the second. In which case FK_LFM_Enroll_Section_Number is creating a foreign key LFM_Enroll.Student_ID => LFM_Section.Section_Number.
The error indicates that the values with which you are trying to update the two columns may not exist in Student and / or Sections tables i.e. 1234567 doesn't exists in the student table and / or 01234 doesn't exist in your section table . You should try inserting new rows or updating existing ones with the new values you are trying to update your foreign keys with.
[Edit: For defining constraints refer lc.'s post]

Primary & Foreign Key Constraints Confusion

currently trying to apply foreign keys to my created tables but I'm getting the SQL error:
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I'm at a massive loss on where to go from here, I think everythings in order but I just can't point out what's wrong.
Here are the relavent create and alter scripts i'm using if anybody can point out where i'm going wrong:
Creation of Results & DopingTest:
CREATE TABLE Results
(
RaceID NUMBER,
HorseID NUMBER,
JockeyID NUMBER,
Position numeric(2)
);
CREATE TABLE DopingTest
(
RaceID NUMBER,
HorseID NUMBER,
TakenBy varchar2(60)
);
And the adding of constraints:
ALTER TABLE Results
ADD(
CONSTRAINT pk_ResultsID
PRIMARY KEY (RaceID,HorseID));
ALTER TABLE DopingTest
ADD(
CONSTRAINT pk_DopingTest
PRIMARY KEY (RaceID, HorseID));
ALTER TABLE Results
ADD(
CONSTRAINT fk_raceID
FOREIGN KEY (RaceID)
REFERENCES Race(RaceID),
CONSTRAINT fk_horseID
FOREIGN KEY (HorseID)
REFERENCES Horse(HorseID),
CONSTRAINT fk_JockeyID
FOREIGN KEY (JockeyID)
REFERENCES Jockey(JockeyID));
ALTER TABLE DopingTest
ADD(
CONSTRAINT fk_RaceIDDT
FOREIGN KEY (RaceID)
REFERENCES Results(RaceID),
CONSTRAINT fk_HorseIDDT
FOREIGN KEY (HorseID)
REFERENCES Results(HorseID));
Any help would be greatly appreciated, thanks.
If results has a composite primary key, the foreign key would have to reference both components of the key.
ALTER TABLE DopingTest
ADD(
CONSTRAINT fk_RaceIDDT
FOREIGN KEY (RaceID, HorseID)
REFERENCES Results(RaceID, HorseID)
);

Foreign Key Problem

I have two tables:
DeptMast
EmpMast
Both of tables have a column called DeptName and I have primary keys on DeptName in both tables.
Now when I go to create a foreign key on column DeptName of EmpMast, SQL Server gives me this error:
There are no primary or candidate keys in the referenced table
'DEPTMAST' that match the referencing column list in the foreign key
'FK_Key'.
If I create unique index on deptmast.Deptname then I don't get any error. Why does this happen?
You marked it as a primary key, so it cannot be a foreign key as well. A foreign key can appear multiple times in the table, while a primary key cannot.