I am here to show you who we can save data in many to many relationship table. First of all i will tell you how to create many to many relationship in database tables.
follows the following steps to create many to many relationship
Step 1:
Create a new database named as "Students" by right click on the Database in Object Explorer of SQL Management Server
then Click on OK and Save table with name StudentSubjects.
follows the following steps to create many to many relationship
Step 1:
Create a new database named as "Students" by right click on the Database in Object Explorer of SQL Management Server
Step 2:
Give the of database "Students" and press OK.
Step 3:
Refresh the database in object explorer and expand the Students database then right click on Tables and hit New Table...
Step 4:
On the right hand side you will get create table window. Write the Column Name and Data Type of Column here . Right Click on StudentID and Set Primary Key , Also set Identity Specification = Yes from Column Properties.
Step 5:
I have created four columns named as StudentID, StudentName, DOB, RegistrationNumber. Now right click on dbo.Table_1* and hit Save Table_1.
Step 6:
Give the Name of your Table "StudentMaster" and press OK
Step 7:
In the same way create a table for Subjects.
Step 8:
Now it turns to link Subjects with students.(One subject can be linked with many students or one student can be linked with many subjects that is the reason we need many to many relationship).
Create a table with Column "StudentID","SubjectID" with datatype int and right click on it then hit Relationship...
Step 9:
Click on Add Button and then click on Tables and Columns Specific.(...) in right panel.
Step 10
Select Primary Key Table as StudentMaster and its StudentID Column .
In Foreign Key table , Table is pre-selected, Select Column as StudentID Then Press OK.
Step 11:
Again click on Add button and set relation ship for Subjects as shown in image. Then Press OK and then Press Close.
Step 12:
Right click on StudentID and set Primary Key and then again right click on StudentID and hit Indexex/Keys...
Step 13:
In right panel click Columns(...) Highlighted button.
Step 14:
You will see StudentID only click in blow row you will get SubjectID and click Sort Order = Ascending.then Click on OK and Save table with name StudentSubjects.
Now we are ready to use this this database in ASP.NET MVC4. Please go to next post.
Thanks for the post sir
ReplyDelete