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
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
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.
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.
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.
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:
(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.