Can a Foreign Key Reference a Foreign Key?

  • Thread starter JOZ
  • Start date
  • Tags
    Sql
In summary, when referencing values in a modified form of Oracle, the number of referencing columns must match the referenced columns. This means that for the tables <code>employee</code>, <code>project</code>, and <code>allocation</code>, the primary keys must also be included in the foreign keys. In the case of <code>timeLog</code>, this would mean adding <code>EmpID</code> and <code>ProjID</code> to the foreign key referencing <code>allocation</code>.
  • #1
JOZ
8
0
Hi I am trying to create the tables below and having problems with the timeLog table referencing allocation, when I put it into the modified form of Oricle my University uses it dosent like me refrencing the refrenced values it spits out the error:
Code:
ORA-02256: number of referencing columns must match referenced columns
Is it even possible to refrence a value that's already being refrenced?
Is there a way to refrence both the EmpID and ProjID together from allocation so that the EmpID, ProjID pair has to exist in allocation to be used in timeLog

Code:
CREATE TABLE employee (
	EmpID		number PRIMARY KEY,
	EmpName		varchar2(30),
	Gender		varchar2(1));
	
CREATE TABLE project (
	ProjID		 varchar2(2) PRIMARY KEY,
	Description	 varchar2(30),
	Budget	number);
	
CREATE TABLE allocation (
	EmpID		number references employee,
	ProjID		varchar2(2) references project,
	HourlyRate	number(5,2),
	PRIMARY KEY (EmpID, ProjID));
	
CREATE TABLE timeLog (
	EmpID		number references allocation,
	ProjID		varchar2(2) references allocation,
	WeekNo		number,
	HrsWorked	number,
	PRIMARY KEY (EmpID, ProjID, WeekNo));
 
Technology news on Phys.org
  • #2
</code>A:You need to add the same columns that you have in the primary key to the foreign key. So, for <code>allocation</code>, the primary key is <code>(EmpID, ProjID)</code>. For <code>timeLog</code>, you need to add <code>EmpID</code> and <code>ProjID</code> to the foreign key:<code>CREATE TABLE timeLog ( EmpID number references allocation (EmpID, ProjID), ProjID varchar2(2) references allocation (EmpID, ProjID), WeekNo number, HrsWorked number, PRIMARY KEY (EmpID, ProjID, WeekNo));</code>
 

Related to Can a Foreign Key Reference a Foreign Key?

What is a foreign key in SQL?

A foreign key in SQL is a field or set of fields that references a primary key in another table. It is used to establish a relationship between two tables and maintain data integrity.

Why do we use foreign keys in SQL?

We use foreign keys in SQL to establish relationships between tables and ensure data integrity. This allows us to perform operations such as JOINs and maintain consistency in our data.

How do you reference a foreign key in SQL?

To reference a foreign key in SQL, you need to use the keyword "REFERENCES" followed by the name of the table and the column or columns that make up the primary key. For example, "FOREIGN KEY (column_name) REFERENCES table_name (primary_key_column_name)".

Can a foreign key be null in SQL?

Yes, a foreign key can be null in SQL. This means that the value in the foreign key column can be empty, indicating that there is no corresponding value in the referenced table. However, it is generally recommended to avoid having null values in foreign keys.

What happens when you delete a record with a foreign key in SQL?

When you delete a record with a foreign key in SQL, the corresponding records in the referenced table will also be deleted. This is known as a cascade delete and is a feature of foreign keys that helps maintain data integrity and prevent orphaned records.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
690
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top