Let's assume I have 3 tables and sorted from parents to child:
Table A(columnAId)Table B(columnsBId,AId)Table C(columnsCId,AId,BId)
Table B.AId is referencing Table A.AId.
Table C.BId is linked to Table B.BId and Table C.AId is linked to Table B.AId - not Table A.AId.
Can I know whether this is correct standard or not? Explain with more information.
Answer might depend on database you actually use.
I'm on Oracle, and - from its perspective - your approach wouldn't work because foreign key must reference primary (or unique) key in another table.
It means that the first two tables can be created with no problems:
However,
table_cwould fail becauseaidis trying to referencetable_b.aid, but that column is not primary key intable_b:However, if
table_c.aidreferencestable_a.aid(a primary key column), everything is OK:Alternative approach (although this is NOT what you described) would be a composite primary (and then foreign) key:
Composite primary key:
table_cstill can't referencetable_b's columns separately:Composite foreign key is OK:
If your database supports foreign keys that reference any column (which doesn't have to be primary/unique key), you'd probably be able to create such a data model. Is it correct? Can't tell, you should know why you want to reference columns that way. If it were me, I'd reference
table_a.aidfromtable_c.aid.