I was learning Sqlalchemy and noticed that a foreign key relation was defined by relating the table names without identifying the actual foreign key connecting the two tables, so I was thinking, if there are more than one foreign keys between the two tables(if it ever makes sense), sqlalchemy would fail to determine the key used to link the two tables. Anyway, my question is: does it ever make sense to have more than one foreign keys between two tables
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
Yes, there are certainly cases where a table can have multiple foreign keys to different rows of a parent table.
Here's an example: A table in a bug-tracking database, which references three different users. The user who reported the bug, the engineer who is assigned to fix the bug, and a tester who is assigned to verify the bug is fixed.
If each of those users might be different people, you need a distinct foreign key for each one.
Here's another example: A shipping application tracks each shipment and records the origin country and the destination country.
You should know the fact that how many primary keys a table can have and thats one for one table representation. So, the fact that two tables in between them you can have one foreign key as a representation of a unique not null called primary key of the other. But that doesnt mean a table can have only one foreign key Foreign keys might be several in one table to many tables primary keys.