Reputation: 32065
A Report can have multiple charts. The Chart table looks as follows:
The ChartId above can map to the ChartId to either one of the following Chart Types:
I am using SQL Server and unfortunately alternate solutions like using CouchDB etc are not viable options. I also intend to map these tables to entities using Entity Framework.
This design is my first stab and I see multiple problems with this design:
I would imagine this is a common enough problem that there are prescribed solutions and established ways to handle it.
Am I on the right path with the design? And if so, how to overcome the above problems (FK, join, unique id across multiple tables, and mapping).
This answer is quite comprehensive about the strategies. And SQL Team's article on implementing table inheritance in SQL Server got me where I wanted.
Upvotes: 3
Views: 931
Reputation: 18940
Line charts. pie charts, and bar charts are specialized forms of charts.
Lookup "generalization specialization relational modeling" for good articles on this pattern.
Upvotes: 1
Reputation: 85645
I'd tackle this with a table inheritance scheme:
Chart (ChartId, ChartTypeId)
PK (ChartId, ChartTypeId)
CHECK ChartTypeId IN ('Line', 'Bar', 'Pie')
LineChart (ChartId, ChartTypeId)
PK (ChartId, ChartTypeId)
FK (ChartId, ChartTypeId) -> Chart
CHECK ChartTypeId IN ('Line')
To add a chart, you first add to the "base" Chart table, and then to the appropriate "subtype" chart table. The check constraints keep everything in line.
Upvotes: 2
Reputation: 26849
Have you considered putting the Charts in the same table and using a Table per heirarchy inheritance pattern? The link there shows how to implement this in Entity Framework. Whether or not it's viable depends on just how different all those chart types will be and how many fields each will need.
There is also Table per type inheritance which is more similar to what you are already describing.
Upvotes: 2
Reputation: 15769
Or, I'll have to run a join against all the tables to get the chart data.
Yes, and that is fine.
Upvotes: 0