问题
With the following entity relationship structure I'm struggling to figure out if the relationship between the LOAN and ITEM entities is valid?
The weak entity of LOAN uses a partial key of 'loan_dateLeant' and the primary keys from CUSTOMER and ITEM to form LOANs primary key. However LOAN has a 'one to many' relationship with ITEM as a loan can consist of more than one item. But surely this means that if more than one item is loaned, then the loan record will have two item_id values for part of its primary key?
回答1:
You're right, that's not valid - you can't use a to-many relationship as a defining part of a primary key. What you might consider doing instead is adding a unique loan_id
that you make part of your primary key, instead of the items the loan contains; then a single loan is defined by its customer and loan ID (or customer, date, and loan ID).
If that doesn't work, make date_leant
a datetime field, and increase the precision on it until you can't possibly (within the constraints of your system) have two loans occur at the same time - how likely is it that a single customer transacts two separate loans within milliseconds of each other?
Note this doesn't prevent the LOAN entity from taking part in the relationship as the "one"; it just means that you can't define the weak entity using the "many".
回答2:
Actually, you can do this.
Remember, the primary key of LOAN will only depend on the date+customer identifier. As long as there is a maximum cardinality of "1" on the "can make" relationship (i.e., a loan is related to only 1-customer), you're fine on the primary key with LOAN.
The ITEM table, will instead take the (single) identifier of the loan (customerID + date) as a foreign key within ITEM. The Primary Key of LOAN will not be affected by this modeling.
来源:https://stackoverflow.com/questions/2464094/entity-relationships-can-a-weak-entity-take-part-in-a-one-to-many-relationsh