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
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.)
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.
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.
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.
Laravel Relationship at a glance: Try to understanding to figure and text:
One to one(hasOne) relationship: A user has(can have) one profile. So,A profile belongs to one user.
One to many(hasMany): A user has many(can have many) article. So, Many article belongs to one user;
Many to many(BelongsToMany): A User can belongs to many forum. So, A forum belongs to many user.
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?