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.
Related
The screenshots from sql-server :
your students table has a composite primary key, so the foreign key must include all of them
The Foreign Key is on the wrong table. Instead of a FK on Department refering to Student, put the Foreign Key on Student, referring to Department, which should have a Primary Key of DepartmentId (and don't put . in your column names, and don't abbreviate).
Is there a way to create a foreign key on two columns that reference the same PK in another table?
Both of these columns are related to the PK.
Here is what I have:
CONSTRAINT some_FK FOREIGN KEY (col1, col2) REFERENCES table(col_PK);
But I keep getting the following error:
ORA-02256: number of referencing columns must match referenced columns
You want two different foreign keys:
CONSTRAINT old_player_fk FOREIGN KEY (old_player_id) REFERENCES player(id),
CONSTRAINT new_player_fk FOREIGN KEY (new_player_id) REFERENCES player(id)
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]
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.
can we define a primary key in a table as a foreign key in that table . I mean,
PRIMARY KEY(ssn),
FOREIGN KEY (ssn) REFERENCES Cust(cust_ssn)
And If we have a table that has some parameters that refers to some other table parameters and to some other 3rd table too. Then Do we need to define those parameters as foreign key referencing to both the tables or Just only one.
Yes. Any field or combination of fields can be a foreign key.