Hii,
I am new to database thing so I am trying to wrap my head around it.
-
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.
-
one2many: same as many2one but instead now the foreign key constrain will look something like A --> B.
-
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
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.
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. :)
You also have NoSQL databases, where you can have arrays, embedding and you can often save yourself the CPU hit of joins.
In addition to 1:many, many:many, and many:1 (which is just 1:many but looking at it in the other direction), you also occasionally see 1:1, for example if you want to augment a table with additional data. This might be done by having your foreign key also be your primary key in the augmenting table, since that would also enforce a uniqueness constraint on the FK as a result.
Also, probably unnecessary to mention, but you can also have “0 or 1” relationship (meaning one side is optional but capped at 1). These are technically separated from “1” relationships usually when you get into all the theory. An example of this might be a “0:1” relationship using the above augment table, but where the augmenting table isn’t required to have a row for every row in the augmented table. (A 1:1 constraint can be enforced, for example, by having an additional FK in the augmented table pointing to the augmenting table.)
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.