473,786 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LINQ. Which tables relationships should I use?

Hello,

I need an advice:

I have 3 tables: Posts, Events and Files.
Each post, event and file can be a associated to one or many tags.

My idea was to create only one Tags table.
Note that each tag can have various associations.
It can be associate to various posts, events and files simultaneous.

My idea was to create a Tags table as follows:
[Tags] TagId (PK), PostId (FK), EventId (FK), FileId (FK).

- Is this the way to do this?
- Will I have problems with .NET 3.5 LINQ?

The other 2 options I see are:
1. Having only one FK in table Tags, i.e. TargetId, which could be
associated with PostId, EventId or FileId ...
This does seem right to me.
2. Have 3 Tags tables: for posts, for Events and for Files.
I would like to avoid having 3 tables but ...

I need to extend my decision to categories, ratings, etc.
So option 2 really seems bad idea.

Could, someone, please advice me on this?

Thanks,
Miguel

Oct 16 '07 #1
3 1442
shapper wrote:
Hello,

I need an advice:

I have 3 tables: Posts, Events and Files.
Each post, event and file can be a associated to one or many tags.

My idea was to create only one Tags table.
Note that each tag can have various associations.
It can be associate to various posts, events and files simultaneous.

My idea was to create a Tags table as follows:
[Tags] TagId (PK), PostId (FK), EventId (FK), FileId (FK).

- Is this the way to do this?
- Will I have problems with .NET 3.5 LINQ?

The other 2 options I see are:
1. Having only one FK in table Tags, i.e. TargetId, which could be
associated with PostId, EventId or FileId ...
This does seem right to me.
2. Have 3 Tags tables: for posts, for Events and for Files.
I would like to avoid having 3 tables but ...

I need to extend my decision to categories, ratings, etc.
So option 2 really seems bad idea.
Actually, option 1 is the bad idea :). The thing is that the FK of
option 1 can't point to a PK table, so you actually have no referential
integrity checking.

Having a tag-element table for each element is what you should do, as
it defines the relationship between tag and element, so option 2 is
better. Your initial idea of 1 table is actually a combination of all
tables resulting of option 2 into 1 table, but that has the downside
that if you want to add an element for tagging, it will force you to
update that table, instead of placing a new table into the db.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Oct 17 '07 #2
On Oct 17, 9:51 am, "Frans Bouma [C# MVP]"
<perseus.usenet NOS...@xs4all.n lwrote:
shapper wrote:
Hello,
I need an advice:
I have 3 tables: Posts, Events and Files.
Each post, event and file can be a associated to one or many tags.
My idea was to create only one Tags table.
Note that each tag can have various associations.
It can be associate to various posts, events and files simultaneous.
My idea was to create a Tags table as follows:
[Tags] TagId (PK), PostId (FK), EventId (FK), FileId (FK).
- Is this the way to do this?
- Will I have problems with .NET 3.5 LINQ?
The other 2 options I see are:
1. Having only one FK in table Tags, i.e. TargetId, which could be
associated with PostId, EventId or FileId ...
This does seem right to me.
2. Have 3 Tags tables: for posts, for Events and for Files.
I would like to avoid having 3 tables but ...
I need to extend my decision to categories, ratings, etc.
So option 2 really seems bad idea.

Actually, option 1 is the bad idea :). The thing is that the FK of
option 1 can't point to a PK table, so you actually have no referential
integrity checking.

Having a tag-element table for each element is what you should do, as
it defines the relationship between tag and element, so option 2 is
better. Your initial idea of 1 table is actually a combination of all
tables resulting of option 2 into 1 table, but that has the downside
that if you want to add an element for tagging, it will force you to
update that table, instead of placing a new table into the db.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website:http://www.llblgen.com
My .NET blog:http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Hi,

I ended up with the following structure:

[Posts] PostId (PK), ...
[Files] FileId (PK), ...
[Events] EventId (PK), ...

[PostsTags] PostId (PK), TagId (PK)
[FilesTags] FileId (PK), TagId (PK)
[EventsTags] EventId (PK), TagId (PK)

[Tags] TagsId (PK), ...

I added the tables as this to LINQ (I used a dbml file)

What do you think?

I am starting to move all my projects from .NET 2.0 to .NET 3.5 using
VS 2008 Beta 2 and would appreciate advice on this.

Thanks,
Miguel

Oct 17 '07 #3
shapper wrote:
On Oct 17, 9:51 am, "Frans Bouma [C# MVP]"
<perseus.usenet NOS...@xs4all.n lwrote:
shapper wrote:
Hello,
I need an advice:
I have 3 tables: Posts, Events and Files.
Each post, event and file can be a associated to one or many tags.
My idea was to create only one Tags table.
Note that each tag can have various associations.
It can be associate to various posts, events and files
simultaneous.
My idea was to create a Tags table as follows:
[Tags] TagId (PK), PostId (FK), EventId (FK), FileId (FK).
- Is this the way to do this?
- Will I have problems with .NET 3.5 LINQ?
The other 2 options I see are:
1. Having only one FK in table Tags, i.e. TargetId, which could be
associated with PostId, EventId or FileId ...
This does seem right to me.
2. Have 3 Tags tables: for posts, for Events and for Files.
I would like to avoid having 3 tables but ...
I need to extend my decision to categories, ratings, etc.
So option 2 really seems bad idea.
Actually, option 1 is the bad idea :). The thing is that
the FK of option 1 can't point to a PK table, so you actually have
no referential integrity checking.

Having a tag-element table for each element is what you
should do, as it defines the relationship between tag and element,
so option 2 is better. Your initial idea of 1 table is actually a
combination of all tables resulting of option 2 into 1 table, but
that has the downside that if you want to add an element for
tagging, it will force you to update that table, instead of placing
a new table into the db.

FB

--
--------------------------------------------------------------------
---- Lead developer of LLBLGen Pro, the productive O/R mapper for
.NET LLBLGen Pro website:http://www.llblgen.com
My .NET blog:http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
--------------------------------------------------------------------
----

Hi,

I ended up with the following structure:

[Posts] PostId (PK), ...
[Files] FileId (PK), ...
[Events] EventId (PK), ...

[PostsTags] PostId (PK), TagId (PK)
[FilesTags] FileId (PK), TagId (PK)
[EventsTags] EventId (PK), TagId (PK)

[Tags] TagsId (PK), ...

I added the tables as this to LINQ (I used a dbml file)

What do you think?
Looks OK. If you now simply want all tags, you can, if you want all
posts based on one or more tags, you can etc. If you want to enable
other elements for tagging, you can too as it's very easy to add
support for that: just add a table.

FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Oct 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
3897
by: Mariusz Jedrzejewski | last post by:
Hi, I'll be very grateful if somebody can explain me why my Opera 7.23 (runing under linux) doesn't show me inner tables. Using below code I can see only "inner table 1". There is no problem with other browsers (I checked it under Konqueror). Thank you in advance for your help. Regards. /Mariusz <HTML>
1
1262
by: shapper | last post by:
Hello, I have been reading a few articles about LINQ and I have a few questions: 1. What do I need to start using it in my ASP.NET 2.0 / SQL 2005 / Visual Studio 2005 web sites? 2. Is there a way to convert my existing SQL Stored Procedures to LINQ code?
11
3682
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night) and the 'employee name'. There is another table which assigns an ID to the Shifts, i.e. 1,2 and 3 for morn, eve & night shifts respectively. From the mother table, the incentive is calculated datewise for each employee as per his shift duty. In...
2
1361
by: shapper | last post by:
Hello, I created 3 few tables that have Many to Many relationships: Posts (PostId PK) Files (FileId PK) PostsTags (PostId PK, TagId PK) FilesTags (FileId PK, TagId PK)
10
4457
by: Richard | last post by:
Hi folks, thanks for taking the time to read this (and hopefully point our where I'm going wrong). The scenario: I have a local Access2007 database which links in several read only mySql tables via ODBC. The problem:
10
1297
by: Nils Magnus | last post by:
Hello, I've just started using SQL to LINQ to access my SQL Server tables, and it's a wonderful tool. Are there any equally elegant ways to store and access historic values for the objects? For instance, I'm managing a Person table with mail addresses, phone numbers etc. I would like to be able to store the earlier values of these fields, so I can see what mail address/phone number a given Person had at a specific point in time. Any...
2
1305
by: shapper | last post by:
Hello, I have 3 tables: Posts, Tags and PostsTags. PostsTags relates Posts with Tags and has 3 fields: PostID and TagID. I have all the relationships between tables well defined! I have the following query:
9
2262
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity Framework. Though I am quite comfortable with ADO.NET and VB.NET. In your opinion, should I take a few days and learn it and utilize these technologies in this new project (I'm starting from scratch)? Are the benefits worth it in your opinion?...
2
3157
by: Chris | last post by:
Trying to get my head around linq. Here is what I have: Tables: OrderHeader OrderLineItem OrderSubLineItem The result set I need is: OrderHeader.Description, Count(OrderSubLineItem) as CountAllItems
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.