SIGN UP MEMBER LOGIN:    
ARTICLE

Database Table Relationship

Posted by Sivaraman Dhamodaran Articles | SQL Server 2012 November 20, 2010
This article talks about the Data Table relationship and explains the types with example. Also gives a guideline of how do you model each relationships.
Reader Level:

Introduction


You may be disappointed to read this article if you do not know about database Constaints. But, just a basic knowledge is required. Just refer to C# corner for Constraints and then proceed.

 

The relationship between the tables are very important for the modern database systems like Oracle, SQL Server etc. Relationships are created by linking the column in one table with the column in another table. When we do so there are four different type of relationship that can be created. The relationships are listed below:

  1. One to One Relationship
  2. Many to One relationship
  3. Many to Many relationship
  4. One to One relationship

We will explore each relationship here.

 

One to Many & Many to One Relationship

 

For a One to many relationship, a single column value in one table has one or more dependent column values in another table. Look at the following diagram:

Pic1.JPG

 

Here there are two tables. One is Customer table, which has all the customers. The other table is Order table which has all the orders uniquely identified by the OrderID. The CustID column is showing which customer placed the order. In this case, there will be multiple values for the same custid such as X in the Order table. If customer X has placed 4 Orders, then there are 4 custid (X) in the Order table, and there is only one corresponding CustID X in the customer table.

 

So if you look at the relationship from left to right (Customer to Order), it is a One To Many relationship. If you look at the relationship from right to left  (i.e.) Order to Customer, it is a Many to One relationship.

 

Modeling the relation


In the relational database system, you can model this with a foreign key and unique Key. In our Example, CustID in the Customer table is Primary or Unique Key. CustID in the Order table is foreign key that refers the column CustID in the Customer table. Note that if you modeled One to Many relationship between two tables then a many to one relationship automatically exists. The professional way of symbolic representation is placing a Â¥ on the left side point and 1 in the right side point.  I do it in the reverse way for easy understanding.

 

Many to Many Relationship

 

Hope you have an idea of what I am going to say here as you already read the previous section. The question is how do we model it in the relational database system. For a perfect Many to Many relationship, a third table is required. The third table acts as bridge between the tables that want to establish a Many to Many relationship. The bridge table stores the common information between the Many to Many relationship tables. Have a look at the following diagram:

 

Pic2.JPG

In the above example, the Transaction table acts as a bridge table. It has information common to both product and customer. I can say the transaction information is suitable for entering as columns to this table. The Transaction table has two One to Many relationships. One is between Transaction and Customer and other one is between Product and Transaction. One Customer Id in the Customer table maps to multiple customer ids (Of course same custid repeated) in the transaction table. Similarly, One product id in the Product table has multiple entries in the transaction table.

 

So how do "Many to Many" relationship exists between Product and Transaction table? 

  1. First let us look at the Linkage shown in the diagram from left to right. A single customer id XYZ is mapped to multiple repeated customer ids (XYZ) in the Transaction table. All this transaction table's repeated XYZ customer id has different Product Ids p1, p2,p3...Pn. All these products a unique row in the Product table. So the whole result is "A single customer XYZ purchased Many Products"

  2. Let us have a look at Right to left. A single product ABC in the product table has repeated entries in the Transaction table. All these repeated products (ABC) has different customer ids C1,C2,C3…Cn. These a corresponding unique row for each Customers like c1,c2 etc., in the Customer Table. The whole result is "A single product ABC is purchased by Many Customer"

Hence, the relationship between Product and Customer is Many to Many via the intermediate bridge Transaction.

 

Modeling the Relation

  1. ProdId and CustId in the transaction table together forms a Primary key. That is; both the Prodid and CustId column uniquely identify a row in this table. Prodid refers the Product id in the Product table using a foreign key. CusId refers the customer id in the Customers table. So ProdId and CustId together forms a Primary key and as individual they are foreign keys.
  2. ProdId in the Product table is a Primary Key.
  3. CustId in the Customer table is a Primary Key.

One to One Relationship

 

One to one relationship can exist between two tables. I hope you already guessed it. You are right if your guess is "The second table column that refers the first table column is a primary key as well as foreign key". Consider the following diagram:

 

Pic3.JPG

 

It looks like an inheritance for Front-end developers. OK. That holds true for our example also. The member id in the CAshCornerMembers table is a Primary key. Two tables VIPMember as well as MVPMember refers this primary key column in the table CAshCornerMembers. Both MemberId fields in the tables VIPMember and MVPMember are primary keys and at the same time they point to the MemberId field of the other table CAshCornerMembers using the Foreign key. So in this example there exists two One to One relations.

 

How it is One-One?

 

The answer is simple! The columns involved in the Mappings are Unique in the table they present. Also not that a member can be a VIP member as well as MVP like our Mahesh Chand. In that case, both the table has his MemberId (Once) and refers the Single entry in the table CAshCornerMembers.

 

See you all in the next article.

Login to add your contents and source code to this article
share this article :
post comment
 

the article is quite self explanatory. i like that.

Posted by dominic ijale Mar 10, 2011

I AM GETTING ERROR WHILE RETRIEVING IMAGES AS PARAMETER IS NOT VALID...............HELP ME ....THANK U INADV

Posted by kishore kumar Jan 21, 2011

Thanks Deepak and abosaleh

Posted by Sivaraman Dhamodaran Dec 07, 2010

Thank you for  this lesson,and realy is very important

Posted by abosaleh alnahdi Dec 06, 2010

Nice article on relationships.

Thanks!!!

Posted by Deepak Sharma Nov 25, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor