1-to-many relationship

Hello,

I'm having a hard time understanding the 1-to-many relationship.

I have 2 entities called Chair and Kitchen and I want model the existence of a chair inside the kitchen.

I understand that the right thing would be to add a new attribute ChairId, of type Chair Identifier, to Kitchen.

Because chairs are inside the kitchen and kitchen have chairs.

Am I right or should I Add a new Attribute KitchenId, of type Kitchen Identifier, to Chair.


Why?

Hi,

I would create an entity furniture with KitchenId and ChairId. And then create an unique index over the 2 attributes.

This way you can create new furniture records like chair, table, cabinet, etc. And add them to your kitchen without the need to update your datamodel with every new piece of furniture you want to add to the kitchen.

Regards,

Daniek

Solution

Hello.

It is the opposite.

If Kitchen has ChairId, it can only have one chair in each kitchen.

If you create an attribute KitchenId in Chair, you can say in which kitchen the chair is. And place several chairs in the same kitchen.

Solution

Hi Karina,

I think you should go with Nuno's answer.

Regards,

Daniel

Karina, understand it with below sample data, below is what you have done. This allows only one chair per kitchen


Whereas if you go with below approach, you can manage to have more than one chair per kitchen i.e. one to many relation ship between Kitchen and Chair


Hope this makes it clear!

Nuno Reis wrote:

Hello.

It is the opposite.

If Kitchen has ChairId, it can only have one chair in each kitchen.

If you create an attribute KitchenId in Chair, you can say in which kitchen the chair is. And place several chairs in the same kitchen.

If I do it the way you say, it stays like this and I read Many Kitchen Chairs.
I understand that part.

So, because in a many-to-many relationship, the reading is different, in this case below, one Place to many Review.


Many to many is a different scenario.

You can use a Many-to-Many to represent 1-to-many or even 1-to-1. BUt for those you have to do the limits on your own.


Try to think like the user inserting data.

  • If you have a ChairDetail page, you can go to dropdown Kitchen and change Kitchen. Chair can't be in more than one so it makes sense.
  • If you have a Kitchen and you edit Chair dropdown... it doesn't work as you intended., does it?
  • Now, if you have a Many-to-Many table in ChairDetail you can say: "Match Chair and Kitchen. Limit it to one entry".
  • And you can have KitchenDetail and say "match Kitchen with Chair. Don't Limit".


(Databases are the key part of all developments and the trickiest part of OS development. If it isn't clear or you have more questions, keep them coming.)

thank you all