NET and Sql Server interview questions – Can you design one to one, one to many and many to many relationships in sql server?

Answer:
This is one of the most typical question asked by the interviewers.Let’s us assume that we have the following two table of Product and
Vendor with their respective fields like below.ProductId ProductName Cost //Product tableVendorID VendorName ProductId //Vendor tableNow let’s begin with how you can design one to one relationship between these two tables.One to One relationship:-

Below diagram show one to one relationship between Product and Vendor table.

R1

In the above Product table “ProductId” is defined as a primary key and in the Vendor table “ProductId” is defined as foreign key with reference to the “ProductId” of the Product table.

In this way you can achieve one to one relationship.

One to Many relationship:-

Now we want to define one to many relationship on Vendor and Product table.

In this many records in one table correspond to the one record in another table.

Example: Every one vendor can have multiple products. So there exist one-to-many relationships between Vendor and Product table.

In order to define one to many relationship you have to add one column name as VendorId in Product table which is foreign key reference to VendorId of Vendor table.

Below diagram show one to many relationship on Product and Vendor table.
R2

Many to Many relationship:-

In this, one record in one table corresponds to many rows in another table and also vice-versa.

For instance: In a company, one vendor can sale many products and also one product can be sold by many Vendors.

Given below diagram is a sample of many-to-many relationship. For defining many to many we have to add one more table VendorProduct which is linked to the primary key of Product and Vendor tables.

r3

In the following video, view samples of various questions asked in C#, .NET and Sql Server interview.

Please click here to see more .NET/SQL Server interview questions
Regards,
Visit Authors blog for more .NET and Sql Server interview questions
Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s