Hii,

I am new to database thing so I am trying to wrap my head around it.

  1. many2one: so in this relationship you will have more than one record in one table which matches to only one record in another table. something like A <-- B. where (<–) is foreign key relationship. so B will have a column which will be mapped to more than one record of A.

  2. one2many: same as many2one but instead now the foreign key constrain will look something like A --> B.

  3. many2many: this one is interesting because this relationship doesn’t make use of foreign key directly. to have this relationship between A and B you have to make a third database something like AB_rel. AB_rel will hold values of primary key of A and also primary key of B. so that way we can map those two using AB_rel table.

tell me if I got something wrong :) give me some suggestion <3

27 points

Usually we don’t distinguish between many2one and one2many, since it’s the same just viewed from the other entity.

There is one more class though, which is one2one. That is, the entities have a direct relationship. Sometimes this also includes the case where you have zero or one, i.e. the relation is optional on one side. This can be accomplished with an FK plus unique constraint or by merging the tables.

permalink
report
reply
15 points

One to many: my primary key is other table’s foreign keys.

Many to one: other table’s primary key is my foreign keys.

Many to many: my primary key and other table’s primary key are all foreign keys that connect each other in some other table.

So yeah, you’ve got it right. :)

permalink
report
reply
3 points

This is probably the most straightforward explanation. In many-to-many, I usually have a helper table standing in between which holds the foreign keys

permalink
report
parent
reply
13 points

I find real-world examples help grasp concepts for me. I’ll use an example like a video platform (like Youtube) as an example for this:

One-to-one: for each one User, you maybe only allow one [content] Channel (or, could be one-or-none, meaning a User may have one Channel, or may use use their User account to be a viewer and have no Channel. You might decide to change this later to be one-to-many, but for the example let’s say you don’t). All the data could be stored just on the User entity, but maybe you decided to separate it into two tables so you don’t need to query the User entity which has sensitive info like a password, email, or physical address, when you just need a Channel. For each Channel, there is a foreign key pointing to the (owning_)user_id, and this is “unique”, so no duplicate id’s can exist in this column.

One-to-many (or Many-to-one): for each one Channel, there may be many Videos. You could use a relational table (channel_x_video), but this isn’t necessary. You can just store the (owning_)channel_id on the Video, and it will not be unique like the prior example, since a Channel will have many Videos, and many Videos belong to one Channel. The only real difference between “one-to-many” and “many-to-one” is semantic - the direction you look at it.

Many-to-many: many Users will watch many Videos. Maybe for saving whether a Video was watched by a User, you store it on a table (by the way, this example might not be the best example, but I was struggling to find a good example for many-to-many to fit the theme). Although each video-to-user relation may be one-to-one, many Videos will be watched by one User, and many Users will watch one Video. This would be not possible to store on both the Video or the User tables, so you will need a relational table (video_x_user, or include “watched” in the table name for clarity of purpose). The id column of each row is irrelevant, but each entry will need to store the video_id and the user_id (and neither will be unique), as well as maybe the percent watched as a float, the datetime it was watched, etc. Many-to-many relationships get very complicated often, so should be used carefully, especially when duplicate combinations of a_x_b can occur (here, we shouldn’t store a new row when a user watches a video a second time)

permalink
report
reply
7 points

Thank you for the in depth explanation. It improved my understanding of relationship between tables and the YouTube example was awesome😊

permalink
report
parent
reply
2 points

Another Many-to-many example within this usecase would be “subscriptions”. Users can subscribe to multiple channels and channels can have multiple users subscribed to them. You would use another relational table that stores the channel_id & user_id, with uniqueness for both together, since “being subscribed to one specific channel multiple times” doesn’t make sense and perhaps put a column to store “hitting the bell” in there too.

permalink
report
parent
reply
3 points

Laravel’s documentation does a very good job of describing these types of relationships. Even if you’re not a PHP developer, their docs cover the basics.

permalink
report
reply
3 points

to have this relationship between A and B you have to make a third database

Probably just a mistake here, but you make a third table, not a new database.

Apart from that (and the fact that one to many and many to one is the same thing), yeah, looks correct.

permalink
report
reply

Programming

!programming@programming.dev

Create post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Community stats

  • 3.5K

    Monthly active users

  • 1.7K

    Posts

  • 28K

    Comments