4

After making little progress in web development and php I figured out if I can make my grip strong on database designing, I will be able to reduce much code and time in developing the application. But I happen to be a dumb in database designing so can anyone please help to differentiate between has many and belongs to relationship. They seems quite same to me

13

has many

Useful for describing 1:n or n:n relationships. Using the 1:n relationship, we are looking at a single object and looking at the 1 or more other objects related to the first object.

A parking garage has many cars parked within it.
The primary object has many secondary objects.
The primary key has many foreign keys (linking the primary entry to many other entries in other tables.)

belongs to

Useful for describing 1:1 or n:1 relationships (note that n:1 is the reverse of 1:n, so you're looking at the relationship in the opposite direction.)

A car belongs to the parking garage.
The secondary object belongs to the primary object.
In the n:n case:
The foreign key is referenced by / belongs to the primary key.

  • 1
    so the it is the direction which makes the difference. Can we say that the primary object usually have HAS_MANY and the secondary object will have BELONGS_TO? which makes sense – Umair Abid Jun 14 '12 at 8:57
  • Yes, the direction of the relationship dictates which of the terms you would use. This still holds for a 1:1 case, the primary HAS A secondary obj (so it's "has a" not "has many") but we still have the secondary belongs to the primary. – GlenH7 Jun 14 '12 at 13:43
1

A database relationship usually has 2 tables that are linked together. One table (the "belongs to"-side) contains a foreign key to the other table (the "has many"-side). So most relations have both sides.

1

can anyone please help to differentiate between has many and belongs to relationship. They seems quite same to me

It is good that you are learning about database design and data modeling. In data modeling, one has to name relationships carefully so that the meaning of the association is reflected on the model and so that the model can be checked for competence and accuracy.

The association/relationship name "has many" is very generic. For example, one could say that a BookStore has many Books (this is reading the association name left to right). If you read the association name from right to left, you should name the association in such a way that preserves the left-to-right name. So, you would say A book belongs to one Bookstore. The phrase has many is composed of 2 words. Has (which describes the relationship) and the many part which describes the cardinality.

As you can see the naming above is inaccurate. A more accurate naming would be:

  • A Bookstore Stocks zero, One or More BookCopy.

  • A BookCopy is stocked in a BookStore.

(assuming the bookstore has 1 branch).

In general the many side indicates a physical column called Foreign key to be defined. In the above example, that column would belong to the BookCopy table.

in UML the phrase "has a" indicates aggregation whereas the phrase "is a" indicates composition. You may want to take a look at a good book in ERD or UML or check the web for that.

0

Laravel Relationship at a glance: Try to understanding to figure and text:

  1. One to one(hasOne) relationship: A user has(can have) one profile. So,A profile belongs to one user.

  2. One to many(hasMany): A user has many(can have many) article. So, Many article belongs to one user;

  3. Many to many(BelongsToMany): A User can belongs to many forum. So, A forum belongs to many user.

In graphical view enter image description here

protected by gnat Jan 8 '17 at 10:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.