开发者

Entity Relationships - Can a weak entity take part in a 'one to many' relationship as the 'one'

开发者 https://www.devze.com 2022-12-23 00:54 出处:网络
With the following entity relationship structure I\'m struggling to figure out if the relationship between the LOAN and ITEM entities is valid?

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' relationsh开发者_运维问答ip 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?

Entity Relationships - Can a weak entity take part in a 'one to many' relationship as the 'one'


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".


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消