Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
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

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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