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

-5 points

You also have NoSQL databases, where you can have arrays, embedding and you can often save yourself the CPU hit of joins.

permalink
report
reply
1 point

I am familiar with NoSQL databases. they are generally easy to use and don’t require much effort. however we use postgresql where I am doing my internship. It’s a Odoo based company and Odoo uses postgresql as backed database.

permalink
report
parent
reply
2 points

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.

no, the other way around

When B has a foreign key to A, many B records may relate to one A record. That’s the many2one part.

The fact that different B records can point to different A records is irrelevant to that.

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

It’s the same, mirrored. Or mirrored interpretation / representation to be more specific. (No logical change.)

If you had B --> A for many2one, then the foreign key relationship is still B --> A. But if you want to represent it from A perspective, you can say one2many - even though A does not hold the foreign keys.

In relational database schemata, using foreign keys on a column means the definition order is always one to one, and only through querying for the shared id will you identify the many.

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.

Notably, we still make use of foreign keys. But because one record does not necessarily have only one FK value we don’t store it in a column but have to save it in a separate table.

This association table AB_rel will then hold the foreign keys to both sides.

permalink
report
reply
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
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