HowTo: Create SQL Table Relationships via Drag´n´Drop

image_thumb3If you create a relationship between to SQL tables, you get many benefits. The most important benefit (for me) is the integrity of your data. Besides the database-world there is another huge benefit: The relationships are used by many O/R mappers to create a structured object model. You could create such realtionships via different dialogs in the SQL Management Studio / Visual Studio or just do a "Drag´n´Drop" from one table to another one.

Relationships & O/R Mappers 
Linq2Sql & ADO.NET Entity Framework are both O/R mappers built in the .NET Framework (but you could use NHibernate or others as well)  and create entities based on a database. If you have relationships in your database, you will find these relationships later in you object model. To create such realationships in you SQL Database you have 2 (or 3) options:

Option A: With dialogs

Select table -> column -> modify:

image_thumb4

*right click* -> Relationships…

image_thumb5

Add -> Tables and Column Specification:

image_thumb7

Select table/column:

image_thumb9

Tada: Relationship created – but you need some clicks to do it. Now option B:

Option B: With an database diagram

image_thumb10

2 tables and the red line marks the relationship we wants to add:

image_thumb13

… just click on the primary key and drag it on the foreign key::

image_thumb16

Now you´ll see a dialog to specify the relationship.

Result:
Option B saves more time and is much more "visual" – option C would be: Write the SQL queries manual.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Currently there is no additional info about this author.

8 Responses

  1. Thanks for this article, I’ve always gone with the visual approach.  Think this is namely due to my bringing up on Microsoft Access which supported the visual approach although it’s good to know how to manually create the relationships.
    Thanks

    Reply
  2. You can also use this simple and easy to use application: http://bstdownload.com/reviews/dbschema-5/

    Reply
  3. That was helpful
    Thanks for your nice working

    Reply
  4. Appreciate your spending some time to talk about this specific, I feel firmly over it and also actually like reading through on this kind of topic.

    Reply
  5. Incredible ! Outstanding in depth tutorial. I’ll make use of that can ! tm unifi

    Reply
  6. You just gained a new brand new audience

    Reply
  7. Outstanding bit of details!

    Reply

Comment on this post

Recent Posts

  • image1825-439x194_thumb.png
    Change the WebDeploy Port or why do I need port 8172?

      If you use WebDeploy on a server operation system you would usually use Port 8172. But what is this Port for and can I change it? Hint: For installing the WebDeploy I recommend this Blogpost. Port 8172 = IIS Management Service Default Port The Port 8172 is the default port of the IIS Management ...

  • Windows Phone Fonts & what if Visual Studio lies

    Today I was confronted with a little Problem: my Windows Phone App refused to show me the Font I choose – also other thinks didn’t work. Although the Visual Studio Designer did show the Fonts: Unfortunately there isn’t much left in the Emulator: Reason for this: Windows Phone doesn’t include all the typos Windows does ...

  • Json-Online-Tools: Viewer & Json2Csharp generator

      Wherever APIs are mentioned the JSON format I not far away. Since I’m using two tools regularly I would like to introduce you to them. JSON Viewer If you only see the JSON-Text you are usually not able to see the structure. With the help of JSON Viewer you can have an easy overview: ...

  • Windows Phone SDK & „System“-Icons

      Although the Metro Design focuses a lot on Typography Icons are still quite important. If you install Windows Phone SDK you will receive 36 Icons. You can find them here: C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons Unfortunately many Icons you might know from the common applications are not integrated. Pedro Lamas extracted 99 additional Icons ...

  • image1830-570x194.png
    How can I figure out if my ADFS 2.0 works?

      I was working with ADFS 2.0 (“Active Directory Federation Services”) for a while when this simple question crossed my mind: How can I figure out if the connection between ADFS and AD “works”? Here is a simple test… What is ADFS? If you need some “position of trusts” beneath the AD-boarders you choose an ...

Support us